Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use FindBin;
  5. use Data::Dumper;
  6. use Getopt::Long;
  7. use DateTime;
  8. use Net::STOMP::Client;
  9. use Sys::Hostname;
  10. $Data::Dumper::Indent = 1;
  11.  
  12. my ($host, $message, $file, $queue, $client_ack);
  13. my ($port, $username, $password, $vhost) = (61613, "", "", "/");
  14. my $subscriber_id = "$FindBin::Script.$$" . '@' . hostname;;
  15.  
  16. GetOptions(
  17. "host:s" => \$host,
  18. "port:i" => \$port,
  19. "queue:s" => \$queue,
  20. "client-ack" => \$client_ack,
  21. "vhost:s" => \$vhost,
  22. "username:s" => \$username,
  23. "password:s" => \$password,
  24. "id:s" => \$subscriber_id,
  25. );
  26.  
  27. usage("a queue is required") unless $queue;
  28. usage("a host is required") unless $host;
  29.  
  30. my $stomp = Net::STOMP::Client->new(host => $host, port => $port);
  31.  
  32. my @login_args = (host => $vhost);
  33. push(@login_args, login => $username) if $username;
  34. push(@login_args, passcode => $password) if $password;
  35. $stomp->connect(@login_args);
  36.  
  37. my @sub_args = ( destination => $queue, id => $subscriber_id );
  38. push(@sub_args, ack => 'client') if $client_ack;
  39. $stomp->subscribe( @sub_args );
  40.  
  41. while(1) {
  42. my $frame = $stomp->receive_frame;
  43. if( ! defined $frame ) {
  44. print "network error - received a null frame\n";
  45. next;
  46. }
  47. $stomp->ack(frame => $frame) if $client_ack;
  48. my $dt = DateTime->now(time_zone => "America/New_York");
  49. print "=== Time: " . $dt->ymd . ' ' . $dt->hms . "\n\n";
  50. print "=== Command: " . $frame->command . "\n";
  51. print "=== Headers:\n";
  52. foreach my $h (sort keys %{$frame->headers} ) {
  53. printf "%15s : %s\n", $h, $frame->headers->{$h};
  54. }
  55.  
  56. print "\n";
  57. print "=== Body:\n";
  58. print $frame->body . "\n\n";
  59. print "-" x 75 . "\n\n";
  60. }
  61.  
  62. END {
  63. if( $stomp ) {
  64. $stomp->unsubscribe(id => $subscriber_id);
  65. $stomp->disconnect;
  66. }
  67. }
  68.  
  69. sub usage {
  70. my $msg = shift || "";
  71. print <<EOF;
  72. $msg
  73.  
  74. Subscribe to a queue or topic
  75. Usage: $FindBin::Script
  76. --host message server
  77. --port message server port (default 61613)
  78. --queue queue or topic to subscribe to
  79. --client-ack use client acknowledgement instead of auto
  80. --vhost vhost to login to (default /)
  81. --username optional
  82. --password optional
  83. EOF
  84. exit;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement