In this EJB Video, we will see how to consume the JMS Queue message via Message Driven Bean (MDB). In the previous video, we already written the code to send TextMessage to the JMS Queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package tube.codingexamples.ejb.MDBs; import javax.ejb.ActivationConfigProperty; import javax.ejb.EJB; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import tube.codingexamples.ejb.statelessbean.HelloBeanRemote; //Sample 9.03: Check Wizard Added Code. @MessageDriven( activationConfig = { @ActivationConfigProperty( propertyName = "destination", propertyValue = "java:/jms/queue/FreeShipmentReqQ"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "java:/jms/queue/FreeShipmentReqQ") public class MDBFreeShipment implements MessageListener { public MDBFreeShipment() { } // Sample 9.04: Add Reference to the Existing Bean @EJB(beanName = "HelloBean") HelloBeanRemote HBean; public void onMessage(Message message) { // Sample 9.15: Print Message Taken For Processing System.out.println("Message Taken for Processing"); try { // Sample 09.16: Read Message TextMessage msg = (TextMessage) message; String str = msg.getText(); System.out.println("Requested By " + str.substring(0, str.indexOf('^'))); System.out.println("Net Weight to be Shipped " + str.substring(str.indexOf('^') + 1, str.length())); // Sample 09.17: Call EJB to Get Shipment No (Log Running Process) int SNo = HBean.getFreeShipmentNumber(); System.out.println("Shipment Id: " + SNo + " For " + str.substring(0, str.indexOf('^'))); } catch (JMSException e) { e.printStackTrace(); } } } |
Categories: JavaEE-EJB-Tube