EJB消息驅動Bean

一個消息驅動bean是一種類型的企業Bean,這是由EJB容器調用,當它接收到一個消息隊列或主題。消息驅動bean是一個無狀態的bean是用來做異步任務。

爲了演示如何使用消息驅動bean,我們將利用EJB持久章節的內容,我們要做好以下幾項工作。

  • Step 1. 在數據庫中創建表(請參閱EJB持久章節)。

  • Step 2. 創建實體類對應的表 (請參閱EJB持久章節).

  • Step 3. 創建數據源和持久性單元 (請參閱EJB持久章節).

  • Step 4. 創建一個無狀態EJB EntityManager實例 (請參閱EJB持久章節).

  • Step 5. 更新無狀態EJB。添加添加記錄的方法,並通過實體管理器,從數據庫中獲取記錄 (請參閱EJB持久章節).

  • Step 6. 創建隊列名爲 BookQueue 在JBoss default 應用目錄.

  • Step 7. 一個基於控制檯應用程序客戶端發送消息到這個隊列

  • Step 8. 創建消息驅動bean將使用無狀態的bean持久化客戶數據。

  • Step 9. JBoss的EJB容器將調用上面的消息驅動bean,並把它傳遞的消息將要發送給客戶端。

創建隊列

創建一個文件名爲jbossmq目的地service.xml中,如果不存在  > server > default > deploy 目錄.

在這裏,我們創建名爲BookQueue一個隊列

jbossmq-destinations-service.xml

<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.mq.destination:service=Queue,name=BookQueue"> <depends optional-attribute-name="DestinationManager"> jboss.mq:service=DestinationManager

當你啓動JBoss,你會看到類似的內容在JBoss日誌

...
10:37:06,167 INFO [QueueService] Queue[/queue/BookQueue] started, fullSize=200000, pageSize=2000, downCacheSize=2000
...

創建消息驅動bean

@MessageDriven( name = "BookMessageHandler", activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty( propertyName = "destination", propertyValue ="/queue/BookQueue") } ) public class LibraryMessageBean implements MessageListener { @Resource private MessageDrivenContext mdctx; @EJB LibraryPersistentBeanRemote libraryBean; public LibraryMessageBean(){ } public void onMessage(Message message) { } }

  • LibraryMessageBean annoatated@MessageDriven註解,把它標記爲消息驅動bean。 

  • Its properties are defined as destinationType - Queue and destination - /queue/BookQueue.

  • It implements MessageListener interface which exposes onMessage method.

  • It has MessgeDrivenContext as resource.

  • LibraryPersistentBeanRemote stateless bean is injected in this bean for persistence purpose.

構建EjbComponent項目,並將其部署在JBoss上。構建和部署EJB模塊後,我們需要一個客戶端發送一個消息到JBoss隊列。

示例應用程序

讓我們創建一個測試EJB應用程序來測試消息驅動bean。

Step

Description

1

Create a project with a name EjbComponent under a package com.tutorialspoint.entity as explained in the EJB - Create Application chapter. You can also use the project created in EJB - Create Application chapter as such for this chapter to understand ejb persistence concepts.

2

Create Book.java under package com.tutorialspoint.entity as created in EJB-Persistencechapter

3

Create LibraryPersistentBean.java and LibraryPersistentBeanRemote as created in EJB-Persistence chapter

4

Create jboss-ds.xml in EjbComponent > setup folder and persistence.xml in EjbComponent > src > conf folder. These folder can be seen in files tab in Netbeans as created in EJB-Persistence chapter

5

Create LibraryMessageBean.java under a package com.tutorialspoint.messagebean and modify it as shown below.

6

Create BookQueue queue 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 Application chapter under topic Create Client to access EJB. Modify it as shown below.

EJBComponent (EJB Module)

LibraryMessageBean.java

package com.tuturialspoint.messagebean; import com.tutorialspoint.entity.Book; import com.tutorialspoint.stateless.LibraryPersistentBeanRemote; import javax.annotation.Resource; import javax.ejb.ActivationConfigProperty; import javax.ejb.EJB; import javax.ejb.MessageDriven; import javax.ejb.MessageDrivenContext; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; @MessageDriven( name = "BookMessageHandler", activationConfig = { @ActivationConfigProperty( propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty( propertyName = "destination", propertyValue ="/queue/BookQueue") } ) public class LibraryMessageBean implements MessageListener { @Resource private MessageDrivenContext mdctx; @EJB LibraryPersistentBeanRemote libraryBean; public LibraryMessageBean(){ } public void onMessage(Message message) { ObjectMessage objectMessage = null; try { objectMessage = (ObjectMessage) message; Book book = (Book) objectMessage.getObject(); libraryBean.addBook(book); } catch (JMSException ex) { mdctx.setRollbackOnly(); } } }

EJBTester (EJB Client)

EJBTester.java

package com.tutorialspoint.test; import com.tutorialspoint.entity.Book; import com.tutorialspoint.stateless.LibraryPersistentBeanRemote; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Properties; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.naming.InitialContext; import javax.naming.NamingException; public class EJBTester { BufferedReader brConsoleReader = null; Properties props; InitialContext ctx; { props = new Properties(); try { props.load(new FileInputStream("jndi.properties")); } catch (IOException ex) { ex.printStackTrace(); } try { ctx = new InitialContext(props); } catch (NamingException ex) { ex.printStackTrace(); } brConsoleReader = new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args) { EJBTester ejbTester = new EJBTester(); ejbTester.testMessageBeanEjb(); } private void showGUI(){ System.out.println("**********************"); System.out.println("Welcome to Book Store"); System.out.println("**********************"); System.out.print("Options

  1. Add Book
  2. Exit
    Enter Choice: "); } private void testMessageBeanEjb(){ try { int choice = 1; Queue queue = (Queue) ctx.lookup("/queue/BookQueue"); QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory"); QueueConnection connection = factory.createQueueConnection(); QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); QueueSender sender = session.createSender(queue); while (choice != 2) { String bookName; showGUI(); String strChoice = brConsoleReader.readLine(); choice = Integer.parseInt(strChoice); if (choice == 1) { System.out.print("Enter book name: "); bookName = brConsoleReader.readLine(); Book book = new Book(); book.setName(bookName); ObjectMessage objectMessage = session.createObjectMessage(book); sender.send(objectMessage); } else if (choice == 2) { break; } } LibraryPersistentBeanRemote libraryBean = (LibraryPersistentBeanRemote) ctx.lookup("LibraryPersistentBean/remote"); List<Book> booksList = libraryBean.getBooks(); System.out.println("Book(s) entered so far: " + booksList.size()); int i = 0; for (Book book:booksList) { System.out.println((i+1)+". " + book.getName()); i++; } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); }finally { try { if(brConsoleReader !=null){ brConsoleReader.close(); } } catch (IOException ex) { System.out.println(ex.getMessage()); } } } }

EJBTester做以下任務。

  • jndi.properties中加載和初始化的InitialContext對象。

  • In testStatefulEjb() method, jndi lookup is done with name - "/queue/BookQueue" to obtain treference of queue available in Jboss. Then sender is created using queue session.

  • Then user is shown a library store User Interface and he/she is asked to enter choice.

  • If user enters 1, system asks for book name and sender sends the book name to queue. When JBoss container receives this message in queue, it calls our message driven bean's onMessage method. Our message driven bean then saves book using stateful session bean addBook() method. Session Bean is persisting the book in database via EntityManager call.

  • If user enters 2, then another jndi lookup is done with name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (stateful ejb) again and listing of books is done.

運行客戶端訪問EJB

Locate EJBTester.java in project explorer. Right click on EJBTester class and select run file.

Verify the following output in Netbeans console.

run:
**********************
Welcome to Book Store
**********************
Options

  1. Add Book
  2. Exit
    Enter Choice: 1
    Enter book name: Learn EJB
    **********************
    Welcome to Book Store
    **********************
    Options
  3. Add Book
  4. Exit
    Enter Choice: 2
    Book(s) entered so far: 2
  5. learn java
  6. learn EJB
    BUILD SUCCESSFUL (total time: 15 seconds)

上面顯示的輸出狀態,我們的消息驅動bean接收消息和存儲在持久性存儲的書和書籍通過數據庫檢索。