activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fulko Hew <fulko....@gmail.com>
Subject Re: need example for Perl STOMP access to ActiveMQ.Statistics.Broker
Date Fri, 02 Nov 2012 20:02:45 GMT
For posterity sake, here is my Perl sample code to obtain, decode and print
the information from the statisticsBrokerPlugin via STOMP.

use AnyEvent;
use AnyEvent::STOMP;
use JSON::PP;
use Data::Dumper;

my $host        = 'localhost';
my $port        = undef;
my $ack         = undef;
my $login       = 'system';
my $passcode    = 'manager';
my $brokerInfo  = '/queue/ActiveMQ.Statistics.Broker';
my $queueInfo   = '/queue/ActiveMQ.Statistics.Destination.*';
my $replyto     = '/queue/statsresults';

    # This routine returns a hashref to the (converted) JSON stuff that
comes in
    # (which looks like the following four examples):
    #    [
    #        { 'string'  => [ 'brokerName', 'localhost'
]                            },
    #        { 'long'    => 530492,               'string' =>
'memoryUsage'          },
    #        { 'double'  => '574662.290322581',   'string' =>
'averageEnqueueTime'   },
    #        { 'int'     => 0,                    'string' =>
'storePercentUsage'    },
    #    ]

sub json_to_hash {
    my ($inref) = @_;

    my %hash = ();
    foreach my $hr (@$inref) {                  # the incoming reference is
to an array of hashes containing the key-name and the key-values, but...
        my ($key, $value, $type);
        if (ref($hr->{string}) eq 'ARRAY') {    # if the 'string' entry in
the hash contains an array, the value array contains (in order):
            $key = $hr->{string}[0];            #    the actual 'key name'
            $val = $hr->{string}[1];            #    and its 'value as a
string'
            $type = 'string';
        } else {                                # otherwise the hash
contains two entries, is the value is a key/value-as-a-number
(int/long/double)
            $key = $hr->{string};               # the 'string' entry is the
actual 'key name'
            delete $hr->{string};               # we remove the 'string'
entry (the entry's ID) so the only other entry left must be the entry for
the value
            $type = (keys %$hr)[0];             # that hash entry's key
identifies the value's type (so far, only int/long/double has been seen)
            $val = (values %$hr)[0] + 0;        # and the hash entry's
value is the actual value (... and numericise it)
        }
        $hash{$key} = $val;                     # and store it
        #print "$key:$type:$val\n";
    }
    return \%hash;                              # and return the reference
to the resultant array
}

my $client = AnyEvent::STOMP->connect(
                $host, $port, $ssl, $replyto, $ack,
                { login          => $login,         passcode    =>
$passcode, },   #    { authentication_headers }
                { transformation => 'jms-map-json', receipt     =>
'',        }    #    { subscribe_headers }
            );

$client->reg_cb(CONNECTED => sub {
    my (undef, $body, $headers) = @_;
    #print "specific receive 'CONNECTED'...\n";

    $client->send('', $brokerInfo, { 'reply-to' => $replyto, receipt => '',
} );
    $client->send('', $queueInfo,  { 'reply-to' => $replyto, receipt => '',
} );
});

$client->reg_cb(MESSAGE => sub {
    my (undef, $body, $headers) = @_;

    #print "specific receive 'MESSAGE'...\n";
    my $hr = decode_json($body);
    my $outRef = json_to_hash(\@{$hr->{map}->{entry}});
    print Dumper($outRef);
});

AnyEvent->condvar->recv;                        # start the event loop

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message