惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
I
InfoQ
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
B
Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
雷峰网
雷峰网
Recent Announcements
Recent Announcements
量子位
B
Blog RSS Feed

博客园 - wolfman

正则表达式Pattern 字符串分割成一维数组、二维数组,一维数组与二维数组之间的转换 ActiveMQ在C#中的应用 深入掌握JMS(十一):TemporaryQueue和TemporaryTopic 深入掌握JMS(十):JMSCorrelationID与Selector 深入掌握JMS(九):Selector 深入掌握JMS(八):JMSReplyTo 深入掌握JMS(七):DeliveryMode例子 深入掌握JMS(六):消息头 深入掌握JMS(四):实战Queue 深入掌握JMS(五):实战Topic 深入掌握JMS(三):MessageListener 深入掌握JMS(二):一个JMS例子 深入掌握JMS(一):JSM基础 oracle 分区表(转) Application,Session,Cookie,ViewState,Cache的区别(转) 网络编程基础 VS2008+Oracle92 网站发布注意问题 Windows Service开发应用
深入掌握JMS(十二):MDB
wolfman · 2010-06-12 · via 博客园 - wolfman

在EJB3中,一个MDB(消息驱动Bean)就是一个实现了MessageListener接口的POJO。下面就是一个简单的MDB。
            @MessageDriven(activationConfig={
                    @ActivationConfigProperty(propertyName="destinationType",
                            propertyValue="javax.jms.Queue"),
                    @ActivationConfigProperty(propertyName="destination",
                            propertyValue="queue/testQueue")})
            public class SimpleMDB implements MessageListener {
                public void onMessage(Message message) {
                    try {
                        System.out.println("Receive Message : " +
            ((TextMessage)message).getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }

            它要求必须标注为@MessageDriven。它所监听Destination通过标注属性来注入。

            下面是一个发送消息的StatelessBean:
            @Remote
            public interface IMessageSender {
                public void sendMessage(String content) throws Exception;
            }

            @Stateless
            @Remote
            public class MessageSender implements IMessageSender {
                @Resource(mappedName="ConnectionFactory")
                private ConnectionFactory factory;
                @Resource(mappedName="queue/testQueue")
                private Queue queue;
                public void sendMessage(String content) throws Exception {
                    Connection cn = factory.createConnection();
                    Session session = cn.createSession(false,
            Session.AUTO_ACKNOWLEDGE);
                    MessageProducer producer = session.createProducer(queue);
                    producer.send(session.createTextMessage(content));
                }
            }
            这个EJB只有一个方法SendMessage。ConnectionFactory和Queue通过标注注入。

            接下来是客户端:
            public class MessageSenderClient {
                public static void main(String[] args) throws Exception {
                    Properties props = new Properties();
                    props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory");
                    props.setProperty(Context.PROVIDER_URL, "localhost:2099");
                    Context context = new InitialContext(props);
                    IMessageSender messageSender = (IMessageSender)
            context.lookup("MessageSender/remote");
                    messageSender.sendMessage("Hello");
                }
            }
            它通过JNDI查找到上面的EJB,然后调用sengMessage.