perl与php通过消息队列通信的小问题

最近在写网站后台的php程序时,需要从activemq这个消息队列读取消息进行处理。为了测试的需要,自己手动写了一个perl程序模拟发送消息。在实际操作中发现,按照cpan上Net::STOMP::Client模块的事例代码进行消息发送时,会造成消息无法从消息队列中取走的问题。即当消息被正确读取后,消息仍停留在消息队列中。自己google了半天,发现了这个帖子:

http://activemq.2283324.n4.nabble.com/php-ack-problem-td4262920.html

 

经过研究一番发现因为perl发送消息队列时会默认在消息头添加content-length字段,而php在读取消息时会因为设置了这个字段而无法将消息取走。汗一个........

在CPAN上有这样一段:

If you do not supply a "content-length" header, following the protocol recommendations, a "content-length" header will be added if the frame has a body.

If you do supply a numerical "content-length" header, it will be used as is. Warning: this may give unexpected results if the supplied value does not match the body length. Use only with caution!

Finally, if you supply an empty string as the "content-length" header, it will not be sent, even if the frame has a body. This can be used to mark a message as being a TextMessage for ActiveMQ.

 

附上perl的发送代码的例子,消息队列用的是activemq,perl用到了Net::STOMP模块

perl的:

#!/usr/bin/perl

use 5.010;

use Net::STOMP::Client;

$ACTIVEMQ_IP = '192.168.0.22'; 
$ACTIVEMQ_PORT = '12345';

$ACTIVEMQ_USER     = 'asdf';
$ACTIVEMQ_PASSWORD = 'asdf';

$ACTIVEMQ_CLIENT_QUEUE = '/queue/aaaaaaaaaaaaaaaaaa';

$stomp = Net::STOMP::Client->new(host => $ACTIVEMQ_IP, port => $ACTIVEMQ_PORT);
$stomp->connect(login => $ACTIVEMQ_USER, passcode => $ACTIVEMQ_PASSWORD);

$json = qq({"name":"moon","id":2,"message":"peace and love"});

$stomp->send(destination => $ACTIVEMQ_CLIENT_QUEUE, body => $json, "content-length" => '');    #关键
$stomp->disconnect();

say 'send ok';

Over~

posted @ 2012-06-19 20:44  akawhy  阅读(1377)  评论(0编辑  收藏  举报