上一篇已经知道了JMS的基本操作,今天来看一下ejb3中的一种重要bean:Message Drive Bean(mdb)

如果要不断监听一个队列中的消息,通常我们需要写一个监听程序,这需要一定的开发量,而且如果要实现高并发处理,也不易扩展,而MDB则自动实现了该功能,简单点讲,MDB的应用部署到jboss后,能自动监听目标队列,一旦有消息接收,会触发onMessage事件,开发人员可以在该事件处理中扩展自己的业务逻辑.

 

一、定义一个MDB

 1 package mdb;
 2 
 3 
 4 
 5 import javax.ejb.ActivationConfigProperty;
 6 import javax.ejb.MessageDriven;
 7 import javax.jms.JMSException;
 8 import javax.jms.Message;
 9 import javax.jms.MessageListener;
10 import javax.jms.TextMessage;
11 
12 import util.LoggerUtil;
13 
14 @MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
15         @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
16         @ActivationConfigProperty(propertyName = "destination", propertyValue = "jms/queue/mytest"),
17         @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
18 public class HelloWorldMDB implements MessageListener {
19 
20     @Override
21     public void onMessage(Message msg) {
22         TextMessage txtMsg = null;
23         try {
24             if (msg instanceof TextMessage) {
25                 txtMsg = (TextMessage) msg;
26                 String msgContent = txtMsg.getText();
27                 LoggerUtil.info("Received Message from queue: " + msgContent);
28             } else {
29                 LoggerUtil.warning("Message of wrong type: "
30                         + txtMsg.getClass().getName());
31             }
32         } catch (JMSException e) {
33             throw new RuntimeException(e);
34         }
35 
36     }
37 
38 }
HelloWorldMDB

相关文章:

  • 2021-07-21
  • 2022-01-17
  • 2021-06-22
  • 2022-12-23
  • 2021-05-28
  • 2022-01-09
  • 2021-12-07
  • 2021-09-13
猜你喜欢
  • 2021-09-05
  • 2022-12-23
  • 2022-02-24
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案