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.
  • 相关阅读:
    LVS基于DR模式负载均衡的配置
    Linux源码安装mysql 5.6.12 (cmake编译)
    HOSt ip is not allowed to connect to this MySql server
    zoj 3229 Shoot the Bullet(无源汇上下界最大流)
    hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
    poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
    URAL 1430 Crime and Punishment
    hdu 2048 神、上帝以及老天爷(错排)
    hdu 3367 Pseudoforest(最大生成树)
    FOJ 1683 纪念SlingShot(矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/jjj250/p/2524031.html
Copyright © 2011-2022 走看看