我有一个 java ActiveMQ 生产者,它将整数消息生成到 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)