Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use Data::Dumper;
  5. use Getopt::Long;
  6. use Net::STOMP::Client;
  7. $Data::Dumper::Indent = 1;
  8.  
  9. my ($host, $message, $file, $queue);
  10. my ($port, $username, $password, $vhost) = (61613, "", "", "/");
  11.  
  12. GetOptions(
  13. "host:s" => \$host,
  14. "port:i" => \$port,
  15. "file:s" => \$file,
  16. "vhost:s" => \$vhost,
  17. "message:s" => \$message,
  18. "queue:s" => \$queue,
  19. "username:s" => \$username,
  20. "password:s" => \$password,
  21. );
  22.  
  23. usage("a queue is required") unless $queue;
  24. usage("a host is required") unless $host;
  25.  
  26. if( $file && $message ) {
  27. usage("do not specify both file and message");
  28. } elsif( $file && ! -f $file ) {
  29. usage("file '$file' cannot be found");
  30. } elsif( ! $file && ! $message ) {
  31. usage("either message of file must be specified");
  32. }
  33.  
  34. print "connecting to $host:$port\n";
  35. my $stomp = Net::STOMP::Client->new(host => $host, port => $port);
  36.  
  37. print "logging in...\n";
  38. $stomp->connect(login => $username, passcode => $password, host => $vhost);
  39.  
  40. if( $message ) {
  41. print "sending message...\n";
  42. $stomp->send( destination => $queue, body => $message );
  43. } else {
  44. print "sending contents of file '$file'...\n";
  45. open my $fh, '<', $file;
  46. $stomp->send( destination => $queue, body => do { local $/; <$fh>; } );
  47. close $fh;
  48. }
  49. print "disconnecting...\n";
  50. $stomp->disconnect;
  51. print "finished\n";
  52.  
  53. sub usage {
  54. my $msg = shift || "";
  55. print <<EOF;
  56. $msg
  57.  
  58. Send a message to a queue or topic
  59. Usage: $FindBin::Script
  60. --host message server
  61. --port message server port (default 61613)
  62. --message string to send
  63. --vhost vhost to login to (default /)
  64. --file read from a file and send the contents
  65. --username optional
  66. --password optional
  67. --queue queue to send message to
  68. EOF
  69. exit;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement