1 package Catalyst::Engine::Stomp;
3 use List::MoreUtils qw/ uniq /;
6 use namespace::autoclean;
8 extends 'Catalyst::Engine::Embeddable';
10 our $VERSION = '0.06';
12 has connection => (is => 'rw', isa => 'Net::Stomp');
13 has conn_desc => (is => 'rw', isa => 'Str');
17 Catalyst::Engine::Stomp - write message handling apps with Catalyst.
24 $ENV{CATALYST_ENGINE} = 'Stomp';
25 require Catalyst::Engine::Stomp;
28 MyApp->config->{Engine::Stomp} =
30 hostname => '127.0.0.1',
33 transformation => 'jms-to-json',
38 # In a controller, or controller base class:
39 use base qw/ Catalyst::Controller::MessageDriven /;
41 # then create actions, which map as message types
42 sub testaction : Local {
45 # Reply with a minimal response message
46 my $response = { type => 'testaction_response' };
47 $c->stash->{response} = $response;
52 Write a Catalyst app connected to a Stomp messagebroker, not HTTP. You
53 need a controller that understands messaging, as well as this engine.
55 This is single-threaded and single process - you need to run multiple
56 instances of this engine to get concurrency, and configure your broker
57 to load-balance across multiple consumers of the same queue.
59 Controllers are mapped to Stomp queues, and a controller base class is
60 provided, Catalyst::Controller::MessageDriven, which implements
61 YAML-serialized messages, mapping a top-level YAML "type" key to
68 App entry point. Starts a loop listening for messages.
73 my ($self, $app, $oneshot) = @_;
75 die 'No Engine::Stomp configuration found'
76 unless ref $app->config->{'Engine::Stomp'} eq 'HASH';
78 my @queues = grep { length $_ }
79 map { $app->controller($_)->action_namespace } $app->controllers;
82 my %template = %{$app->config->{'Engine::Stomp'}};
83 my $add_header = delete $template{subscribe_header};
84 if (ref($add_header) ne 'HASH') {
87 $self->connection(Net::Stomp->new(\%template));
88 $self->connection->connect();
89 $self->conn_desc($template{hostname}.':'.$template{port});
91 # subscribe, with client ack.
92 foreach my $queue (@queues) {
93 my $queue_name = "/queue/$queue";
95 destination => $queue_name,
99 # add the additional headers - yes I know it overwrites but
100 # thats the dev's problem?
101 if (keys %{$add_header}) {
102 foreach my $key (keys %{$add_header}) {
103 $header_hash->{$key} = $add_header->{$key};
107 $self->connection->subscribe($header_hash);
112 my $frame = $self->connection->receive_frame();
113 $self->handle_stomp_frame($app, $frame);
114 last if $ENV{ENGINE_ONESHOT};
119 =head2 prepare_request
121 Overridden to add the source broker to the request, in place of the
126 sub prepare_request {
127 my ($self, $c, $req, $res_ref) = @_;
129 $self->next::method(@_);
130 $c->req->address($self->conn_desc);
133 =head2 finalize_headers
135 Overridden to dump out any errors encountered, since you won't get a
136 "debugging" message as for HTTP.
140 sub finalize_headers {
142 my $error = join "\n", @{$c->error};
144 $c->log->debug($error);
146 return $self->next::method($c);
149 =head2 handle_stomp_frame
151 Dispatch according to Stomp frame type.
155 sub handle_stomp_frame {
156 my ($self, $app, $frame) = @_;
158 my $command = $frame->command();
159 if ($command eq 'MESSAGE') {
160 $self->handle_stomp_message($app, $frame);
162 elsif ($command eq 'ERROR') {
163 $self->handle_stomp_error($app, $frame);
166 $app->log->debug("Got unknown Stomp command: $command");
170 =head2 handle_stomp_message
172 Dispatch a Stomp message into the Catalyst app.
176 sub handle_stomp_message {
177 my ($self, $app, $frame) = @_;
179 # queue -> controller
180 my $queue = $frame->headers->{destination};
181 my ($controller) = $queue =~ m|^/queue/(.*)$|;
184 my $config = $app->config->{'Engine::Stomp'};
185 my $url = 'stomp://'.$config->{hostname}.':'.$config->{port}.'/'.$controller;
186 my $req = HTTP::Request->new(POST => $url);
187 $req->content($frame->body);
188 $req->content_length(length $frame->body);
192 $app->handle_request($req, \$response);
194 # reply, if header set
195 if (my $reply_to = $response->headers->header('X-Reply-Address')) {
196 my $reply_queue = '/remote-temp-queue/' . $reply_to;
197 $self->connection->send({ destination => $reply_queue, body => $response->content });
200 # ack the message off the queue now we've replied / processed
201 $self->connection->ack( { frame => $frame } );
203 =head2 handle_stomp_error
205 Log any Stomp error frames we receive.
209 sub handle_stomp_error {
210 my ($self, $app, $frame) = @_;
212 my $error = $frame->headers->{message};
213 $app->log->debug("Got Stomp error: $error");
216 __PACKAGE__->meta->make_immutable;
220 =head2 subscribe_header
222 Add additional header key/value pairs to the subscribe message sent to the