gmarler

xmit_kstat

Oct 30th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.72 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use v5.18.1;
  4. use strict;
  5. use warnings;
  6.  
  7. use Net::Async::AMQP;
  8. use IO::Async::Loop;
  9. use Data::Dumper;
  10. use Future::Utils qw( repeat try_repeat );
  11.  
  12. # Detect disconnection from AMQP
  13. my $DISCONNECTED;
  14. # The channel we're publishing on
  15. my $CHANNEL;
  16. # The exchange we're publishing on
  17. my $EXCHANGE;
  18.  
  19. my $loop = IO::Async::Loop->new;
  20.  
  21. $loop->add(
  22.   my $mq = Net::Async::AMQP->new()
  23. );
  24.  
  25. my $true = Net::AMQP::Value->true;
  26. my $msg  = 'info MESSAGE';
  27.  
  28. $mq->connect(
  29.   #host      => '192.168.56.1',
  30.   host      => 'localhost',
  31.   user      => 'solperf',
  32.   pass      => 'solperf',
  33.   vhost     => '/',
  34.   client_properties => {
  35.     capabilities => {
  36.       'consumer_cancel_notify' => $true,
  37.       'connection.blocked'     => $true,
  38.     },
  39.   },
  40. )->then(
  41.   sub {
  42.     shift->open_channel;
  43.   }, sub { say "CONNECT FAILED!" }
  44. )->on_done(sub { say "CONNECTED!"; })->then(
  45.   sub {
  46.     #my ($channel)  = shift;
  47.     $CHANNEL = shift;
  48.     #say "GOT CHANNEL: " . Dump($CHANNEL);
  49.     say "GOT CHANNEL: $CHANNEL";
  50.  
  51.     $EXCHANGE = 'logs';
  52.  
  53.     Future->needs_all(
  54.       # This is subscriber code
  55.       $CHANNEL->queue_declare(
  56.         queue     => '',  # Let the queue name be generated
  57.         exclusive => 1,
  58.       ),
  59.       # This is publisher code
  60.       $CHANNEL->exchange_declare(
  61.         type     => 'fanout',
  62.         exchange => $EXCHANGE,
  63.       )
  64.     )->on_done(sub { say "EXCHANGE DECLARED!"; })->then(
  65.       sub {
  66.         my ($q) = shift;
  67.         say "QUEUE IS: $q";
  68.  
  69.         # Register for events of interest
  70.         $CHANNEL->bus->subscribe_to_event(
  71.           connected => sub {
  72.             say "CONNECTED TO AMQP";
  73.           },
  74.           close => sub {
  75.             say "DETECTED AMQP CLOSING OF CONNECTION";
  76.             $DISCONNECTED = 1;
  77.           },
  78.           heartbeat_failure => sub {
  79.             say "HEARTBEAT FAILURE DETECTED";
  80.           },
  81.           unexpected_frame => sub {
  82.             say "UNEXPECTED FRAME";
  83.           },
  84.         );
  85.  
  86.         $q->bind_exchange(
  87.           channel  => $CHANNEL,
  88.           exchange => $EXCHANGE,
  89.           routing_key => '',
  90.         )->on_done( sub { say "EXCHANGE BOUND"; } );
  91.       },
  92.       sub { say "EXCHANGE DECLARE FAILED"; }
  93.     );
  94.   },
  95.   sub { say "OPEN CHANNEL FAILED"; }
  96. )->then(
  97.   sub {
  98.     my ($c) = 0;
  99.     try_repeat {
  100.       say "PUBLISHING $msg $c";
  101.       $c++;
  102.       $CHANNEL->publish(
  103.         exchange    => $EXCHANGE,
  104.         routing_key => '',
  105.         #type        => 'application/json',
  106.         type        => 'text/plain',
  107.         payload     => $msg,
  108.       )->get;
  109.       sleep 1;
  110.     } until => sub { $DISCONNECTED };
  111.   },
  112.   sub { say "Problem with Exchange Declare or Publish"; }
  113. )->get;
  114.  
  115.  
  116. $loop->run;
Advertisement
Add Comment
Please, Sign In to add comment