Java Producer,Stompy Python Consumer,ActiveMQ

时间:2012-08-07 05:10:21

标签: java python activemq stomp

我有一个java ActiveMQ生成器,它将Integer消息生成到ObjectMessage实例中。

在python方面,我使用stomp python来侦听队列。但是,虽然所有标题都是正确的,但我收到空邮件正文。

此外,如果我在java端将消息类型更改为TextMessage,我会在python-consumer端获得正确的消息。

我也尝试过使用PyactiveMQ,但效果相同

任何建议都将受到赞赏!!!

编辑:这是一个样板java生成器代码和python订阅者代码,我写的是在python上测试stomp

public class App 
{
Connection conn;
Session session;
MessageProducer producer;

public void registerPublisher(String queueName, String url) throws JMSException {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("system", "manager" ,url);
    conn = cf.createConnection();
    conn.start();
    session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue(queueName);
    producer = session.createProducer(destination);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);

}

public void send(int c) {

    for (int i=0; i<c; ++i) {

        try {
            TextMessage tm = session.createTextMessage(new Integer(i).toString());
//              ObjectMessage tm = session.createObjectMessage();
            producer.send(tm);
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}

public static void main(String []arg) {
    App app = new App();
    try {
        app.registerPublisher(arg[0], arg[1]);
        System.out.println(app.session);
    } catch (JMSException e) {
        e.printStackTrace();
    }
    app.send(1000);
}


}

和Python Stomp监听器

import time
import sys
import logging
import stomp
from stomp import ConnectionListener

queuename = sys.argv[1]

logging.basicConfig( level=logging.DEBUG)

class MyListener(ConnectionListener):
    def on_error(self, headers, message):
        print 'received an error %s' % message

    def onMessage(self, headers, message):
        print headers
        print str(message)
        print type(message)
        print 'received a message ...%s...' % message


conn = stomp.Connection([('localhost', 61613)])                                                                                               
conn.set_listener('', MyListener())
conn.start()
conn.connect()


conn.subscribe(destination='/queue/'+queuename, ack='auto')


while 1:
    time.sleep(2)

2 个答案:

答案 0 :(得分:4)

为了通过Stomp发送接收ObjectMessage类型,您需要使用ActiveMQ的消息transformation feature来使对象有效负载以STOMP客户端可以理解的形式传递。 ActiveMQ提供开箱即用的XML和JSON转换支持,但是您可以添加自己的转换器以获得您想要的内容格式。

答案 1 :(得分:2)

问题:将ObjectMessage从java生产者发送到ActiveMQ Broker。 Stomp Python消费者客户端正在获取空消息体

解决方案:订阅python客户端中的activemq代理时使用转换标头,

例如:

connection.subscribe(destination='/queue/'+queuename, ack='auto', transformation="jms-json")

这样代理就知道消息将以什么形式发送到stomp客户端

相关问题