使消息驱动的bean侦听外部主题

时间:2012-11-15 18:08:24

标签: jboss7.x message-driven-bean

美好的一天,

我正在尝试配置消息驱动的bean来订阅外部主题。我和我的同事一起成功地用JSE客户端听这个主题,看起来像这样。


    public static void main(String[] args) {
        TopicSession    sess     =  null;
        TopicConnection conn     = null;
        TopicSubscriber consumer = null;

        Properties props = new Properties();
        p.put("java.naming.provider.url",         "remote://#server-name#:#server-port(4747)#");
        p.put("java.naming.factory.initial",      "org.jboss.naming.remote.client.InitialContextFactory");
        p.put("java.naming.security.principal",   "#username#");
        p.put("java.naming.security.credentials", "#password#");

        InitialContext context = new InitialContext(props);

        TopicConnectionFactory factory = (TopicConnectionFactory)context.lookup("jms/RemoteConnectionFactory");
        conn = factory.createTopicConnection("#username#", "#password#");
        conn.setClientID("#client-id#");

        sess = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

        Topic topic = (Topic)context.lookup("jms/topic/#topic-name#");
        consumer = sess.createSubscriber(topic);
        consumer.setMessageListener(new MyTopicListener());

        try {
            conn.start();
            // ... loop until done.
        } finally {
            consumer.close();
            sess.close();
            conn.close();
        }
    }

如何将其移至MDB?

据我所知,我们可以使用MessageDriven注释的activationConfig设置destinationType,connectionFactoryJndiName,destinationJndiName,initialContextFactory。但远程URL,用户名和密码怎么样?

仅供参考,我使用的是JBoss AS 7.1.1,这适用于MDB 3.0。

非常感谢您提供任何答案和评论。

2 个答案:

答案 0 :(得分:1)

HornetQ有一个入站和出站资源适配器。入站JCA RA用于通过消息驱动Bean(MDB)消费消息。可以使用MDB的激活配置属性配置资源适配器,如以下示例所示:

@MessageDriven(name = "ExampleMDB", activationConfig = {
   @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
   @ActivationConfigProperty(propertyName = "destination", propertyValue = "testQueue"),
   @ActivationConfigProperty(propertyName = "connectorClassName", propertyValue ="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory"),
   @ActivationConfigProperty(propertyName = "connectionParameters", propertyValue = "host=172.168.1.137;port=5445")})
public class ExampleMDB implements MessageListener {
   public void onMessage(Message recvMsg) {
   ... 
   }
}

有关配置属性的完整列表,请参阅HornetQ文档:http://docs.jboss.org/hornetq/

答案 1 :(得分:1)

你还可以查看新的大黄蜂{{3p>
相关问题