gmarler

rcv_kstat

Oct 30th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.59 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 receiving on
  15. my $CHANNEL;
  16. # The exchange we're receiving 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->bus->subscribe_to_event(
  29.   connected => sub {
  30.     say "CONNECT EVENT DETECTED";
  31.   },
  32.   message => sub {
  33.     say "MESSAGE EVENT DETECTED";
  34.   },
  35. );
  36.  
  37. $mq->connect(
  38.   #host      => '192.168.56.1',
  39.   host      => 'localhost',
  40.   user      => 'solperf',
  41.   pass      => 'solperf',
  42.   vhost     => '/',
  43.   client_properties => {
  44.     capabilities => {
  45.       'consumer_cancel_notify' => $true,
  46.       'connection.blocked'     => $true,
  47.     },
  48.   },
  49.   on_stream => sub {
  50.     say "STREAM OPEN";
  51.   },
  52.   on_read   => sub {
  53.     say "DATA AVAILABLE";
  54.   },
  55. )->then(
  56.   sub {
  57.     shift->open_channel;
  58.   }, sub { say "CONNECT FAILED!" }
  59. )->on_done(sub { say "CONNECTED!"; })->then(
  60.   sub {
  61.     #my ($channel)  = shift;
  62.     $CHANNEL = shift;
  63.     #say "GOT CHANNEL: " . Dump($CHANNEL);
  64.     say "GOT CHANNEL: $CHANNEL";
  65.  
  66.     $EXCHANGE = 'logs';
  67.  
  68.     Future->needs_all(
  69.       $CHANNEL->queue_declare(
  70.         queue     => '',  # Let the queue name be generated
  71.       ),
  72.       $CHANNEL->exchange_declare(
  73.         type       => 'fanout',
  74.         exchange   => $EXCHANGE,
  75.         autodelete => 1,
  76.       )
  77.     )->on_done(sub { say "EXCHANGE DECLARED!"; })->then(
  78.       sub {
  79.         my ($q) = shift;
  80.         say "RECEIVE QUEUE NAME IS: " . $q->queue_name;
  81.  
  82.         # Register for events of interest
  83.         $CHANNEL->bus->subscribe_to_event(
  84.           message => sub {
  85.             my ($event, @details) = @_;
  86.             say "RECEIVED MESSAGE";
  87.           }
  88.         );
  89.  
  90.         $q->bind_exchange(
  91.           channel  => $CHANNEL,
  92.           exchange => $EXCHANGE,
  93.           routing_key => '',
  94.         )->on_done( sub { say "EXCHANGE BOUND"; } )->then(
  95.           sub {
  96.             $q->listen(
  97.               channel => $CHANNEL,
  98.               ack     => 0,
  99.             );
  100.           }
  101.         )->on_done( sub { say "LISTENING ON CHANNEL"; } )->then(
  102.           sub {
  103.             my ($q, $ctag) = @_;
  104.             say "Queue $q has ctag $ctag";
  105.             Future->done;
  106.           }
  107.         );
  108.       },
  109.       sub { say "EXCHANGE DECLARE FAILED"; }
  110.     );
  111.   },
  112.   sub { say "OPEN CHANNEL FAILED"; }
  113. )->get;
  114.  
  115.  
  116. $loop->run;
Advertisement
Add Comment
Please, Sign In to add comment