深入掌握JMS(十二):MDB

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值