Commit | Line | Data |
0a663589 |
1 | package Catalyst::Controller::MessageDriven; |
2 | use Moose; |
3 | |
4 | BEGIN { extends 'Catalyst::Controller' } |
5 | |
d78bb739 |
6 | =head1 NAME |
7 | |
8 | Catalyst::Controller::MessageDriven |
9 | |
10 | =head1 SYNOPSIS |
11 | |
12 | package MyApp::Controller::Queue; |
13 | use Moose; |
14 | BEGIN { extends 'Catalyst::Controller::MessageDriven' } |
15 | |
16 | sub some_action : Local { |
17 | my ($self, $c) = @_; |
18 | # Reply with a minimal response message |
19 | my $response = { type => 'testaction_response' }; |
20 | $c->stash->{response} = $response; |
21 | } |
22 | |
23 | =head1 DESCRIPTION |
24 | |
25 | A Catalyst controller base class for use with Catalyst::Engine::Stomp, |
26 | which handles YAML-serialized messages. A top-level "type" key in the |
27 | YAML determines the action dispatched to. |
28 | |
29 | =cut |
30 | |
0a663589 |
31 | __PACKAGE__->config( |
32 | 'default' => 'text/x-yaml', |
33 | 'stash_key' => 'response', |
34 | 'map' => { 'text/x-yaml' => 'YAML' }, |
35 | ); |
36 | |
37 | sub begin :ActionClass('Deserialize') { } |
38 | |
39 | sub end :ActionClass('Serialize') { |
40 | my ($self, $c) = @_; |
41 | |
42 | # Engine will send our reply based on the value of this header. |
43 | $c->response->headers->header( 'X-Reply-Address' => $c->req->data->{reply_to} ); |
44 | |
45 | # Custom error handler - steal errors from catalyst and dump them into |
46 | # the stash, to get them serialized out as the reply. |
47 | if (scalar @{$c->error}) { |
48 | my $error = join "\n", @{$c->error}; |
49 | $c->stash->{response} = { status => 'ERROR', error => $error }; |
50 | $c->error(0); # clear errors, so our response isn't clobbered |
51 | } |
52 | } |
53 | |
54 | sub default : Private { |
55 | my ($self, $c) = @_; |
56 | |
57 | # Forward the request to the appropriate action, based on the |
58 | # message type. |
59 | my $action = $c->req->data->{type}; |
60 | $c->forward($action, [$c->req->data]); |
61 | } |
62 | |
63 | __PACKAGE__->meta->make_immutable; |
64 | |
65 | 1; |