In this JavaEE EJB Tutorial, we will create MDB Bean to consume the JMS Queue which we created in the Previous Video. We will also introduce the long running task in the stateless session bean.
Remote Interface
1 2 3 4 5 6 7 8 9 10 11 12 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.Remote; @Remote public interface HelloBeanRemote { //Sample 01: Declare the Remote Interface Method public String sayHello(); //Sample 9.01: Declare Method public int getFreeShipmentNumber(); } |
Long Running Method
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 |
package tube.codingexamples.ejb.statelessbean; import javax.ejb.LocalBean; import javax.ejb.Stateless; /** * Session Bean implementation class HelloBean */ @Stateless(mappedName = "HelloBean") @LocalBean public class HelloBean implements HelloBeanRemote { /** * Default constructor. */ public HelloBean() { // TODO Auto-generated constructor stub } @Override public String sayHello() { //Sample 02: Return Greeting Message return "EJB Says: Hello Coding-Example Friends!"; } @Override public int getFreeShipmentNumber() { //Sample 9.02: Let us pretend this as a //Long Running Task as getting free vehicle by querying //from different shipment company int ANumber = (int) (Math.random() * 10000); try { Thread.sleep(30000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ANumber; } } |
Message Driven Bean (Dummy onMessage)
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 |
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) { } } |
Categories: JavaEE-EJB-Tube