zoukankan      html  css  js  c++  java
  • 深入掌握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.
  • 相关阅读:
    SharePoint研究之表单登录配置
    asp.net自定义控件之加载层
    可替换参数在SharePoint和VS中的使用
    C#树类型及其遍历
    Asp.net下拉树实现(Easy UI ComboTree)
    驾校考试科目三心得
    c#pdf查看器
    jquery获取服务器控件Label的值
    [JLOI2014] 松鼠的新家 (lca/树上差分)
    洛谷P1445 [Violet] 樱花 (数学)
  • 原文地址:https://www.cnblogs.com/jjj250/p/2524031.html
Copyright © 2011-2022 走看看