Tuesday, April 6, 2010

Configuring ActiveMQ with Perl

By default ActiveMQ is configured to start the browser for C++, Java, C# etc by using Messaging protocol as Openwire. But to make it work with Perl we need couple of things to do as below:

1. First of all we need a client to handle the requests [Both pushing and retrieving]. We can download Net::Stomp library from CPAN. This is a very simple implementation to send and receive messages using STOMP Protocol.

2. We need to configure transport Connectors in file activemq.xml to use STOMP instead of openwire which is used by default as follows:

<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616"/>
</transportConnectors>

to

<transportConnectors>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61616"/>
</transportConnectors>

3. If you are using activeMQ version 5.3.1 and above, there is a strong probability that your webconsole for admin [http://localhost:8161/admin] may not work as explained at following link [http://activemq.apache.org/web-console.html], so you need to modify the file [apache-activemq-5.3.1\webapps\camel\WEB-INF\applicationContext.xml] as follows :

<!-- configure the camel activemq component to use the current broker -->
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent" >
<property name="brokerURL" value="vm://localhost?create=false&waitForStart=10000" />
<property name="userName" value="${activemq.username}"/>
<property name="password" value="${activemq.password}"/>
</bean>

Now you are ready to start the activeMQ, just double click on the activeMQ app file at bin location, and at console you can notice that activeMQ has started.

Lets write a simple Perl script to push messages in the queue:

use Net::Stomp;
my $stomp = Net::Stomp->new( { hostname => 'QuickSilver-PC', port => '61616' } );
$stomp->connect( { login => 'system', passcode => 'manager' } );
$stomp->send(
{ destination => '/queue/example.A/', body => 'This is my first Stomp Message' } );
$stomp->disconnect;

# subscribe to messages from the queue 'example.A'
use Net::Stomp;
my $stomp = Net::Stomp->new( { hostname => 'localhost', port => '61616' } );
$stomp->connect( { login => 'system', passcode => 'manager' } );
$stomp->subscribe(
{ destination => '/queue/example.A',
'ack' => 'client',
'activemq.prefetchSize' => 1
}
);
while (1) {
my $frame = $stomp->receive_frame;
warn $frame->body; # do something here
$stomp->ack( { frame => $frame } );
}
$stomp->disconnect;

# write your own frame
my $frame = Net::Stomp::Frame->new(
{ command => $command, headers => $conf, body => $body } );
$stomp->send_frame($frame);

You can notice in your webconsole that message "This is my first stomp Message" is pushed to the queue example.A

Hope it helps.

Thanks,
QuickSilver1183
QuickSilver1183@gmail.com

No comments:

Post a Comment