Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- use strict;
- use FindBin;
- use Data::Dumper;
- use Getopt::Long;
- use DateTime;
- use Net::STOMP::Client;
- use Sys::Hostname;
- $Data::Dumper::Indent = 1;
- my ($host, $message, $file, $queue, $client_ack);
- my ($port, $username, $password, $vhost) = (61613, "", "", "/");
- my $subscriber_id = "$FindBin::Script.$$" . '@' . hostname;;
- GetOptions(
- "host:s" => \$host,
- "port:i" => \$port,
- "queue:s" => \$queue,
- "client-ack" => \$client_ack,
- "vhost:s" => \$vhost,
- "username:s" => \$username,
- "password:s" => \$password,
- "id:s" => \$subscriber_id,
- );
- usage("a queue is required") unless $queue;
- usage("a host is required") unless $host;
- my $stomp = Net::STOMP::Client->new(host => $host, port => $port);
- my @login_args = (host => $vhost);
- push(@login_args, login => $username) if $username;
- push(@login_args, passcode => $password) if $password;
- $stomp->connect(@login_args);
- my @sub_args = ( destination => $queue, id => $subscriber_id );
- push(@sub_args, ack => 'client') if $client_ack;
- $stomp->subscribe( @sub_args );
- while(1) {
- my $frame = $stomp->receive_frame;
- if( ! defined $frame ) {
- print "network error - received a null frame\n";
- next;
- }
- $stomp->ack(frame => $frame) if $client_ack;
- my $dt = DateTime->now(time_zone => "America/New_York");
- print "=== Time: " . $dt->ymd . ' ' . $dt->hms . "\n\n";
- print "=== Command: " . $frame->command . "\n";
- print "=== Headers:\n";
- foreach my $h (sort keys %{$frame->headers} ) {
- printf "%15s : %s\n", $h, $frame->headers->{$h};
- }
- print "\n";
- print "=== Body:\n";
- print $frame->body . "\n\n";
- print "-" x 75 . "\n\n";
- }
- END {
- if( $stomp ) {
- $stomp->unsubscribe(id => $subscriber_id);
- $stomp->disconnect;
- }
- }
- sub usage {
- my $msg = shift || "";
- print <<EOF;
- $msg
- Subscribe to a queue or topic
- Usage: $FindBin::Script
- --host message server
- --port message server port (default 61613)
- --queue queue or topic to subscribe to
- --client-ack use client acknowledgement instead of auto
- --vhost vhost to login to (default /)
- --username optional
- --password optional
- EOF
- exit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement