深入掌握JMS(十二):MDB

本文介绍了如何在EJB3中使用MDB(消息驱动Bean)和StatelessBean进行消息监听与消息发送。通过实现@MessageDriven注解的POJO作为MDB监听指定Destination,同时展示了如何在StatelessBean中通过@Resource注入资源,完成消息的发送过程。此外,还演示了客户端通过JNDI查找并调用EJB的方法。

在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.

转载于:https://www.cnblogs.com/guthing/archive/2010/06/12/1757189.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值