1 package Catalyst::Controller::MessageDriven;
4 use namespace::autoclean;
6 BEGIN { extends 'Catalyst::Controller' }
10 Catalyst::Controller::MessageDriven
14 package MyApp::Controller::Queue;
16 BEGIN { extends 'Catalyst::Controller::MessageDriven' }
18 sub some_action : Local {
19 my ($self, $c, $message) = @_;
23 # Reply with a minimal response message
24 my $response = { type => 'testaction_response' };
25 $c->stash->{response} = $response;
30 A Catalyst controller base class for use with Catalyst::Engine::Stomp,
31 which handles YAML-serialized messages. A top-level "type" key in the
32 YAML determines the action dispatched to.
38 Deserializes the request into C<< $c->stash->{request} >>
42 Dispatches to method named by the key C<< $c->stash->{request}->{type} >>
46 Serializes the response from C<< $c->stash->{response} >>
50 __PACKAGE__->config( serializer => 'YAML' );
55 # Deserialize the request message
57 my $serializer = $self->config->{serializer};
58 my $s = Data::Serializer->new( serializer => $serializer );
60 my $body = $c->request->body;
61 open my $IN, "$body" or die "can't open temp file $body";
62 $message = $s->raw_deserialize(do { local $/; <$IN> });
65 # can't reply - reply_to is embedded in the message
66 $c->error("exception in deserialize: $@");
69 $c->stash->{request} = $message;
76 # Engine will send our reply based on the value of this header.
77 $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} );
83 my $serializer = $self->config->{serializer};
84 my $s = Data::Serializer->new( serializer => $serializer );
86 # Custom error handler - steal errors from catalyst and dump them into
87 # the stash, to get them serialized out as the reply.
88 if (scalar @{$c->error}) {
89 my $error = join "\n", @{$c->error};
90 $c->stash->{response} = { status => 'ERROR', error => $error };
91 $output = $s->serialize( $c->stash->{response} );
93 $c->response->status(400);
96 # Serialize the response
98 $output = $s->raw_serialize( $c->stash->{response} );
101 my $error = "exception in serialize: $@";
102 $c->stash->{response} = { status => 'ERROR', error => $error };
103 $output = $s->serialize( $c->stash->{response} );
104 $c->response->status(400);
107 $c->response->output( $output );
110 sub default : Private {
113 # Forward the request to the appropriate action, based on the
115 my $action = $c->stash->{request}->{type};
116 if (defined $action) {
117 $c->forward($action, [$c->stash->{request}]);
120 $c->error('no message type specified');
124 __PACKAGE__->meta->make_immutable;