徒涉春冰

导航

用Net::Stomp连接RabbitMQ STOMP Adapter

RabbitMQ的perlbinding有点问题,我在fc18上make test时就爆出memory leak,可能是rabbitmq c库的问题,这让我不太放心,还好The STOMP plugin adds support for the STOMP protocol to RabbitMQ. The adapter supports STOMP 1.0, STOMP 1.1 and STOMP 1.2. with some extensions and restrictions。STOMP是极其简单的文本协议,应该是比较稳定的。废话不多说,看代码。

 

The STOMP adapter is included in the RabbitMQ distribution. To enable it, use rabbitmq-plugins: 

rabbitmq-plugins enable rabbitmq_stomp

 

echo > /etc/rabbitmq/rabbitmq.conf<<EOF

[
{rabbitmq_stomp, [{tcp_listeners, [61613]},{ssl_listeners, [61614]}, {ssl_cert_login, true}]}
].

EOF

 

 

The RabbitMQ STOMP adapter supports a number of different destination types:

/exchange -- SEND to arbitrary routing keys and SUBSCRIBE to arbitrary binding patterns;
/queue -- SEND and SUBSCRIBE to queues managed by the STOMP gateway;
/amq/queue -- SEND and SUBSCRIBE to queues created outside the STOMP gateway;
/topic -- SEND and SUBSCRIBE to transient and durable topics;
/temp-queue/ -- create temporary queues (in reply-to headers only).

 

看示例

use Net::Stomp;
my $stomp = Net::Stomp->new({ hostname => '10.10.85.29', port => '61613' });

$stomp->connect({login => 'foo', passcode => 'bar', 'host' => '/foo'});
#$stomp->connect({login => 'foo', passcode => 'bar'});

while (1) {
    my ($now_time, $day_of_month) = currenttime();
    my $json_text = encode_json({day => $day_of_month, now=>$now_time});
    $stomp->send({destination => '/topic/foo2', body => $json_text });
}


$stomp->disconnect;

Net::Stomp的文档里面没有说,但是

The CONNECT (or STOMP) frame in STOMP 1.1 has a mandatory host header (to select the virtual host to use for the connection). The RabbitMQ adapter allows this to be optional.

When omitted, the default virtual host (/) is presumed.

If a host header is specified it must be one of the virtual hosts known to the RabbitMQ server, otherwise the connection is rejected. The host header is respected even if the STOMP 1.0 version is negotiated at the time of the connect.

 

 

 

posted on 2013-01-27 11:25  徒涉春冰  阅读(1973)  评论(0编辑  收藏  举报