2012-08-07 172 views
0

我有一个java的ActiveMQ生产者,它产生一个ObjectMessage实例的整数消息。Java生产者,蟒蛇消费者,ActiveMQ

在python方面,我使用stomp python监听队列。但是,我收到空邮件正文,虽然所有标题都收到正确。而且,如果我将消息类型更改为java端的TextMessage,则在python-consumer端会得到正确的消息。

我也试图与PyactiveMQ但具有相同的效果

任何建议,可以理解!

编辑:这是我写的关于蟒蛇

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践踏监听测试跺脚一个样板的java制作的代码和Python用户代码

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) 
+0

给我们一些代码片段,您在哪里创建消息以及在哪里阅读消息。 – 2012-08-07 05:23:37

回答

3

为了发通过Stomp接收ObjectMessage类型,您需要使用ActiveMQ的消息transformation feature以STOMP客户端可以理解的形式交付对象负载。 ActiveMQ提供XML和JSON转换支持,但您可以添加自己的转换器以获取您想要的任何格式的内容。

+0

感谢您的回答蒂姆。发送textMessage并完全消除ObjectMessage是不是更容易? ObjectMessage需要一个可序列化的对象,而textMessage需要一个toString()和fromString()方法。国际海事组织的textMessage应该足以应付各种有效载荷数据,不是吗? – 2012-08-07 11:59:51

+0

另外我有一个虚拟队列和3个物理队列。因此,有三个消费者,2个java和1个python。转换消息是否有意义因为java消费者正在使用Openwire协议? – 2012-08-07 12:05:14

+0

您应该使用任何消息类型对您的应用程序有意义。消息转换机制仅影响STOMP客户端之间的消息,如果您将消息从STOMP客户端转换为ObjectMessage,则openwire客户端将获得ObjectMessages,如果您将对象转换为XML,则只有STOMP客户端获得XML形式的openwire客户端得到ObjectMessage。 – 2012-08-07 13:02:01

2

问题:将ObjectMessage从java生产者发送到ActiveMQ代理。践踏Python的消费客户是越来越空消息体

SOLUTION:使用转换头,而订阅的Python客户端ActiveMQ代理,

例如:

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

让经纪人知道以什么形式将消息发送给stomp客户端