1 package Catalyst::Controller::MessageDriven;
3 use YAML::XS qw/ LoadFile Dump /;
5 BEGIN { extends 'Catalyst::Controller' }
9 Catalyst::Controller::MessageDriven
13 package MyApp::Controller::Queue;
15 BEGIN { extends 'Catalyst::Controller::MessageDriven' }
17 sub some_action : Local {
18 my ($self, $c, $message) = @_;
22 # Reply with a minimal response message
23 my $response = { type => 'testaction_response' };
24 $c->stash->{response} = $response;
29 A Catalyst controller base class for use with Catalyst::Engine::Stomp,
30 which handles YAML-serialized messages. A top-level "type" key in the
31 YAML determines the action dispatched to.
38 # Deserialize the request message
42 my $body = $c->request->body;
43 $message = LoadFile( "$body" );
46 # can't reply - reply_to is embedded in the message
47 $c->error("exception in deserialize: $@");
50 $c->stash->{request} = $message;
57 # Engine will send our reply based on the value of this header.
58 $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} );
60 # Custom error handler - steal errors from catalyst and dump them into
61 # the stash, to get them serialized out as the reply.
62 if (scalar @{$c->error}) {
63 my $error = join "\n", @{$c->error};
64 $c->stash->{response} = { status => 'ERROR', error => $error };
65 $c->error(0); # clear errors, so our response isn't clobbered
68 # Serialize the response
71 $output = Dump( $c->stash->{response} );
74 my $error = "exception in serialize: $@";
76 $c->stash->{response} = { status => 'ERROR', error => $error };
77 $output = Dump( $c->stash->{response} );
80 $c->response->output( $output );
83 sub default : Private {
86 # Forward the request to the appropriate action, based on the
88 my $action = $c->stash->{request}->{type};
89 $c->forward($action, [$c->stash->{request}]);
92 __PACKAGE__->meta->make_immutable;