EJB Message Driven Beans - EJB(Enterprise JavaBeans)

What is EJB Message Driven Beans?

A message driven bean is a form of enterprise bean, which is invoke by EJB container when it receives a message from queue or theme. Message driven bean is a stateless bean and is used to do task asynchronously.

To display use of message driven bean, we will make use of EJB-persistence section and we need to do the following responsibilities −

  • Step 1 − form table in database (Refer to EJB-Persistence chapter).
  • Step 2 − Create Entity class matching to table (Refer to EJB-Persistence chapter).
  • Step 3 − Create Data Source and Persistence Unit (Refer to EJB-Persistence chapter).
  • Step 4 − Create a stateless EJB having Entity Manager Instance (Refer to EJB-Persistence chapter).
  • Step 5 − Update stateless ejb.Add methods to add records and get records from database via entity manager (Refer to EJB-Persistence chapter).
  • Step 6 − Create a Queue named Book Queue in Jobs default application directory.
  • Step 7 − A console based application client will send message to this queue.
  • Step 8 − Create a Message driven bean, which will use the stateless bean to persist the client data.
  • Step 9 − EJB Container of jobs will call the above message driven bean and pass it the message that client will be sending to.

Create Queue

Create a file named jbossmq-destinations-service.xml if not exist in <Jobs Installation Folder> > server > default > deploy folder.

Here we are creating a queue named Book Queue −


When you begin the Jobs, you will see a similar entry in jobs log.

Create Message Driven Bean

LibraryMessageBean is annotating with @Message Driven annotation to mark it as message driven bean.

  • Its properties are defined as destination Type - Queue and destination - /queue/Book Queue.
  • It implements Message Listener interface, which exposes on Message method.
  • It has MessgeDrivenContext as a resource.
  • LibraryPersistentBeanRemote stateless bean is injected in this bean for persistence purpose.

Construct the EjbComponent scheme and deploy it on Jobs. After building and deploying the EJB element, we need a client to send a message to jobs queue.

Example Application

Let us generate a test EJB application to test Message Driven Bean.

Step Description
1 Create a project with a nameEjbComponentunder a packagecom.wisdomjobs.entityas explained in theEJB - Create Applicationchapter. You can also use the project created inEJB - Create Applicationchapter as such for this chapter to understand EJB persistence concepts.
2 CreateBook.javaunder packagecom.wisdomjobs.entityas created inEJB-Persistencechapter.
3 CreateLibraryPersistentBean.javaandLibraryPersistentBeanRemoteas created inEJB-Persistencechapter.
4 Createjboss-ds.xmlinEjbComponent > setupfolder andpersistence.xmlinEjbComponent > src > conffolder. These folders can be seen in files tab in Netbeans as created inEJB-Persistencechapter.
5 CreateLibraryMessageBean.javaunder a packagecom.wisdomjobs.messagebeanand modify it as shown below.
6 CreateBookQueuequeue in Jboss as described above.
7 Clean and Build the application to make sure business logic is working as per the requirements.
8 Finally, deploy the application in the form of jar file on JBoss Application Server. JBoss Application server will get started automatically if it is not started yet.
9 Now create the EJB client, a console based application in the same way as explained in theEJB - Create Applicationchapter under topicCreate Client to access EJB. Modify it as shown below.

EJBComponent (EJB Module)


EJBTester (EJB Client)


EJBTester performs the following tasks −

  • Load properties from jndi.properties and initialize the Initial Context object.
  • In testStatefulEjb () scheme, jade lookup is done with the name - "/queue/Book Queue" to obtain reference of queue available in Jobs. Then sender is created using queue session.
  • Then user is exposed a library store User Interface and he/she is asked to enter choice.
  • If user enters 1, the system asks for book name and sender sends the book name to queue. When Jobs container receives this message in queue, it calls our message driven beans on Message method. Our message driven bean then saves book using tasteful session bean add Book () method. Session Bean is persisting the book in database via Entity Manager call.
  • If user enters 2, then another jade lookup is done with name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (tasteful EJB) again and listing of books is done.

Run Client to Access EJB

Locate EJBTester.java in assignment explorer. Right click on EJBTester class and choose run file.

Prove the following output in Net beans console –

The output exposed above states that our Message driven bean is receipt the message and storing the book in persistent storage and books are retrieved from the folder.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

EJB(Enterprise JavaBeans) Topics