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.
  • 相关阅读:
    css绘制各种图形,三角形,长方形,梯形
    函数中,对形参做不加var的全局溢出赋值,可改变形参所指向的实参的本身值
    求数组中最大值,最小值
    placeholder 效果的实现,input提示字,获取焦点时消失
    js里apply用法
    jquery.lazyload.js-v1.9.1延时加载插件,已兼容ie6和各大浏览器
    移动端 元素外面使用伪类after加边框 导致其内部元素无法选中
    element组件知识点总结
    常用样式总结
    深入理解iframe
  • 原文地址:https://www.cnblogs.com/jjj250/p/2524031.html
Copyright © 2011-2022 走看看