Catalyst::Controller::JMS - controller base class to simplify usage of Catalyst::ActionRole::JMS
version 1.1
package MyApp::Controller::Something; use Moose; BEGIN { extends 'Catalyst::Controller::JMS' } __PACKAGE__->config( namespace => 'queue/my_queue', ); sub my_message_type :MessageTarget { my ($self,$c) = @_; my $body = $c->req->data; my $headers = $c->req->headers; # do something $c->res->header('X-Reply-Address' => 'temporary-queue-name'); $c->stash->{message} = { some => [ 'reply', 'message' ] }; return; }
This controller base class makes it easy to handle JMS-style messages in your Catalyst application. It handles deserialisation and serialisation transparently (thanks to Catalyst::Action::Deserialize and Catalyst::Action::Serialize) and sets up the attributes needed by Catalyst::ActionRole::JMS. It also sets up some sensible default configuration.
__PACKAGE__->config( stash_key => 'message', default => 'application/json', map => { 'application/json' => 'JSON', 'text/x-json' => 'JSON', }, );
See Catalyst::Action::Deserialize and Catalyst::Action::Serialize for what this means.
If you set the MessageTarget attribute on an action, it will be marked for dispatch based on the JMSType of incoming messages. More precisely:
MessageTarget
sub my_message_type :MessageTarget { }
is equivalent to:
sub my_message_type : Does('Catalyst::ActionRole::JMS') JMSType('my_message_type') { }
And:
sub my_action :MessageTarget('my_type') { }
sub my_action : Does('Catalyst::ActionRole::JMS') JMSType('my_type') { }
If you want to have a default action to catch requests not matching any other action, you have to declare it as:
default
sub default :Default { }
otherwise dispatch may not work properly, see http://lists.scsys.co.uk/pipermail/catalyst/2012-March/028261.html for some attempts at an explanation
begin
De-serialises the body of the request into $ctx->req->data. See Catalyst::Action::Deserialize for details.
$ctx->req->data
end
Serialises $ctx->stash->{message} into the response body. See Catalyst::Action::Serialize for details.
$ctx->stash->{message}
NOTE: if $ctx->stash->{message} is not a reference, it will be wrapped in an arrayref. Some Catalyst::Action::Serialize plugins don't like serialising plain scalars.
You can find examples of use in the tests, or at https://github.com/dakkar/CatalystX-StompSampleApps
Gianni Ceccarelli <gianni.ceccarelli@net-a-porter.com>
This software is copyright (c) 2011 by Net-a-porter.com.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Catalyst::ActionRole::JMS, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::ActionRole::JMS
CPAN shell
perl -MCPAN -e shell install Catalyst::ActionRole::JMS
For more information on module installation, please visit the detailed CPAN module installation guide.