1 package Catalyst::Controller::MessageDriven;
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.
35 __PACKAGE__->config( serializer => 'YAML' );
40 # Deserialize the request message
42 my $serializer = $self->config->{serializer};
43 my $s = Data::Serializer->new( serializer => $serializer );
45 my $body = $c->request->body;
46 open my $IN, "$body" or die "can't open temp file $body";
47 $message = $s->raw_deserialize(do { local $/; <$IN> });
50 # can't reply - reply_to is embedded in the message
51 $c->error("exception in deserialize: $@");
54 $c->stash->{request} = $message;
61 # Engine will send our reply based on the value of this header.
62 $c->response->headers->header( 'X-Reply-Address' => $c->stash->{request}->{reply_to} );
68 my $serializer = $self->config->{serializer};
69 my $s = Data::Serializer->new( serializer => $serializer );
71 # Custom error handler - steal errors from catalyst and dump them into
72 # the stash, to get them serialized out as the reply.
73 if (scalar @{$c->error}) {
74 my $error = join "\n", @{$c->error};
75 $c->stash->{response} = { status => 'ERROR', error => $error };
76 $output = $s->serialize( $c->stash->{response} );
78 $c->response->status(400);
81 # Serialize the response
83 $output = $s->raw_serialize( $c->stash->{response} );
86 my $error = "exception in serialize: $@";
87 $c->stash->{response} = { status => 'ERROR', error => $error };
88 $output = $s->serialize( $c->stash->{response} );
89 $c->response->status(400);
92 $c->response->output( $output );
95 sub default : Private {
98 # Forward the request to the appropriate action, based on the
100 my $action = $c->stash->{request}->{type};
101 if (defined $action) {
102 $c->forward($action, [$c->stash->{request}]);
105 $c->error('no message type specified');
109 __PACKAGE__->meta->make_immutable;