Site icon Dipin Krishna

Perl: Send a message to a queue or topic using Stomp

perl

Net::Stomp module allows you to write a Stomp client. Stomp is the Streaming Text Orientated Messaging Protocol (or the Protocol Briefly Known as TTMP and Represented by the symbol :ttmp). It’s a simple and easy to implement protocol for working with Message Orientated Middleware from any language. Net::Stomp is useful for talking to Apache ActiveMQ, an open source (Apache 2.0 licensed) Java Message Service 1.1 (JMS) message broker packed with many enterprise features.

A Stomp frame consists of a command, a series of headers and a body see Net::Stomp::Frame for more details.

For details on the protocol see http://stomp.codehaus.org/Protocol.

Below is a sample script to send a message via Stomp.

#! /usr/bin/perl -w
 
## # # # # # # # # # # # # # # # # # # # # # # # # # #
## Program:  sendstompmessage.pl                     #
## Author:   Dipin Krishna (mail@dipinkrishna.com)   #
## # # # # # # # # # # # # # # # # # # # # # # # # # #                      
 
use Net::Stomp;          
 
my $StompHost = "192.168.3.237";
my $StompPort = "61613";
my $stomp;               
 
{
    local $@;
    print "Creating a stomp instanace for hostname: $StompHost, port: '61614'\n";
    eval { 
		$stomp = Net::Stomp->new( {
			hostname => $StompHost, 
			port => $StompPort
			}) 
	};
    while ( ($k,$v) = each %{$stomp} ) {
    	print "$k => $v\n";
    }
    print ("Unable to create a stomp instance\n")  if $@;
}    
 
if( $stomp ) {
    print "Connecting to stomp using login:'guest' passcode:'guest'\n";
    my $conn = $stomp->connect( { login => 'guest', passcode => 'guest' } );
 
    # Lets print the headers to see what is returned
    my $headers = ${$conn}{headers};
    while ( ($k,$v) = each %{$headers} ) {
      print "$k => $v\n";
    }
 
    if( ${$conn}{command} ne "CONNECTED") {
      print  "Error: Cannot connect to stomp server.\n";
    }
    else {
      print( "Connected to stomp server.\n");
      $stomp->send( { destination=>'testqueue', body=>"Test msg"})
    }
}
Exit mobile version