PPJ has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I'm trying to use Net::Stomp as a simple means of consuming from an ActiveMQ queue.

If there are no frames on the queue, I want the consumer script to quit or move onto other things.

I tried this:

use Net::Stomp; my $hostname = 'localhost'; warn "Creating stomp object"; my $stomp = Net::Stomp->new( { hostname => $hostname, port => '61613' +} ); warn "Connecting"; $stomp->connect( { login => 'guest', passcode => 'password' } ); warn "Subscribing"; $stomp->subscribe( { destination => '/queue/example.A', 'activemq.prefetchSize' => 1 }); while ( $stomp->can_read ){ warn "Getting frame"; my $frame = $stomp->receive_frame ( { timeout => 0.5 } ) || die 'f +rame timeout'; warn 'Got frame: ' . $frame->body; } $stomp->disconnect; warn "Disconnected";
This gets frames while there are frames to be read (good). But then it waits indefinitely for the next frame (bad).
Got frame: This is message 10 at ./stomp_consumer.pl line 26. Getting frame at ./stomp_consumer.pl line 24.
According to the documentation, it should also be possible to set timeout behaviour with the can_read method
while ( $stomp->can_read ( {timeout => 0.5 } ) ){ # do stuff }
This is also ineffective (no timeout).

Am I missing something obvious? If so, please accept my apologies.

All suggestions gratefully received.

Replies are listed 'Best First'.
Re: Net::Stomp receive_frame method always blocks (ignores timeout parameter)
by Corion (Patriarch) on Aug 16, 2010 at 12:54 UTC

    From looking at the code of Net/Stomp.pm, it seems that the timeout will only ever apply to receiving the first byte of a frame, not to reading the whole frame.

    Have you looked at employing one of the event-based systems, like AnyEvent or POE (which has POE::Component::MessageQueue that claims to be STOMP)?

      Thanks Corion.

      Does that mean that the timeout parameter in Net::Stomp actually has no useful effect? Or if it does have an effect, how can it be put into practice?

      I'll look into the other modules you mention.

        Hi again,

        Found another CPAN package which also does the job: Net::STOMP::Client

        Timeout works on this with no drama.

        Thanks again.