📜  EJB-消息驱动Bean

📅  最后修改于: 2020-11-16 06:16:35             🧑  作者: Mango


消息驱动Bean是一种企业Bean,当EJB容器从队列或主题接收到消息时,它将被EJB容器调用。消息驱动Bean是无状态Bean,用于异步执行任务。

为了演示消息驱动bean的使用,我们将使用EJB-persistence一章,我们需要执行以下任务-

  • 步骤1-在数据库中创建表(请参阅EJB-Persistence一章)。

  • 步骤2-创建与表相对应的Entity类(请参阅EJB-Persistence一章)。

  • 步骤3-创建数据源和持久性单元(请参阅EJB持久性一章)。

  • 步骤4-创建具有EntityManager实例的无状态EJB(请参阅EJB-Persistence一章)。

  • 步骤5-更新无状态ejb.Add方法以添加记录并通过实体管理器从数据库获取记录(请参阅EJB-Persistence一章)。

  • 步骤6-在JBoss默认应用程序目录中创建一个名为BookQueue的队列。

  • 步骤7-基于控制台的应用程序客户端将消息发送到此队列。

  • 步骤8-创建一个消息驱动Bean,它将使用无状态Bean来保存客户端数据。

  • 步骤9 -EJB容器的jboss将调用上述消息驱动的bean,并将客户端将发送到的消息传递给它。

创建队列

如果>服务器>默认>部署文件夹中不存在,请创建一个名为jbossmq-destinations-service.xml的文件。

在这里,我们创建一个名为BookQueue的队列-

jbossmq-destinations-service.xml

  
   
      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带有@MessageDriven注释,以将其标记为消息驱动的Bean。

  • 其属性定义为destinationType-队列和destination-/ queue / BookQueue。

  • 它实现了MessageListener接口,该接口公开了onMessage方法。

  • 它具有MessgeDrivenContext作为资源。

  • 为了持久性目的,将LibraryPersistentBeanRemote无状态bean注入到该bean中。

生成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-Persistence chapter.

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

EJBComponent(EJB模块)

LibraryMessageBean.java

package com.tutorialspoint.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客户端)

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 \n1. Add Book\n2. Exit \nEnter 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 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对象。

  • 在testStatefulEjb()方法中,使用名称“ / queue / BookQueue”完成jndi查找,以获得Jboss中可用队列的引用。然后使用队列会话创建发件人。

  • 然后,向用户显示一个库存储用户界面,并要求他/她输入选择。

  • 如果用户输入1,则系统要求输入书名,发送方将书名发送到队列。当JBoss容器在队列中接收到此消息时,它将调用消息驱动bean的onMessage方法。然后,我们的消息驱动bean使用有状态会话bean addBook()方法保存书籍。会话Bean通过EntityManager调用将书籍保留在数据库中。

  • 如果用户输入2,则将使用名称“ LibraryStatefulSessionBean / remote”进行另一个jndi查找,以再次获取远程业务对象(有状态EJB),并完成了书的清单。

运行客户端以访问EJB

在项目资源管理器中找到EJBTester.java。右键单击EJBTester类,然后选择运行文件

在Netbeans控制台中验证以下输出-

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

上面显示的输出表明,我们的消息驱动bean正在接收消息并将书存储在持久性存储中,并且从数据库中检索了书。