/
Stomp.pm
657 lines (492 loc) · 17.2 KB
/
Stomp.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
package Catalyst::Engine::Stomp;
use Moose;
use List::MoreUtils qw/ uniq /;
use HTTP::Request;
use Net::Stomp;
use MooseX::Types::Moose qw/Str Int HashRef/;
use namespace::autoclean;
use Encode;
extends 'Catalyst::Engine::Embeddable';
our $VERSION = '0.17';
has connection => (is => 'rw', isa => 'Net::Stomp');
has conn_desc => (is => 'rw', isa => Str);
has destination_namespace_map => (is => 'rw', isa => HashRef, default => sub { { } } );
=head1 NAME
Catalyst::Engine::Stomp - write message handling apps with Catalyst.
=head1 SYNOPSIS
# In a server script:
BEGIN {
$ENV{CATALYST_ENGINE} = 'Stomp';
require Catalyst::Engine::Stomp;
}
MyApp->config(
Engine::Stomp' = {
tries_per_server => 3,
'servers' => [
{
'hostname' => 'localhost',
'port' => '61613'
connect_headers => {
login => 'myuser',
passcode => 'mypassword',
},
},
{
'hostname' => 'stomp.yourmachine.com',
'port' => '61613'
},
],
utf8 => 1,
subscribe_headers => {
transformation => 'jms-to-json',
}
},
);
MyApp->run();
# In a controller, or controller base class:
use base qw/ Catalyst::Controller::MessageDriven /;
# then create actions, which map as message types
sub testaction : Local {
my ($self, $c) = @_;
# Reply with a minimal response message
my $response = { type => 'testaction_response' };
$c->stash->{response} = $response;
}
=head1 DESCRIPTION
Write a Catalyst app connected to a Stomp messagebroker, not HTTP. You
need a controller that understands messaging, as well as this engine.
This is single-threaded and single process - you need to run multiple
instances of this engine to get concurrency, and configure your broker
to load-balance across multiple consumers of the same queue.
Controllers are mapped to Stomp queues or topics, and a controller
base class is provided, Catalyst::Controller::MessageDriven, which
implements YAML-serialized messages, mapping a top-level YAML "type"
key to the action.
=head1 QUEUES and TOPICS
The controller namespace (either derived from its name, or defined by
its C<action_namespace> attribute) defines what the controller is
subscribed to:
=over 4
=item C<queue/foo>
subscribes to the queue called C<foo>
=item C<topic/foo>
subscribes to the topic called C<foo>
=item C<foo>
subscribes to the queue called C<foo> (for simplicity and backward
compatibility)
=back
=head2 Connction and Subscription Headers
You can specify custom headers to send with the C<CONNECT> and
C<SUBSCRIBE> STOMP messages. You can specify them globally:
MyApp->config(
Engine::Stomp' = {
'servers' => [
{
'hostname' => 'localhost',
'port' => '61613'
},
],
subscribe_headers => {
transformation => 'jms-to-json',
},
connect_headers => {
login => 'myuser',
passcode => 'mypassword',
},
},
);
per server:
MyApp->config(
Engine::Stomp' = {
'servers' => [
{
'hostname' => 'localhost',
'port' => '61613'
subscribe_headers => {
strange_stuff => 'something',
},
connect_headers => {
login => 'myuser',
passcode => 'mypassword',
},
},
],
},
);
or per-controller (subscribe headers only):
package MyApp::Controller::Special;
use Moose;
BEGIN { extends 'Catalyst::Controller::MessageDriven' };
has stomp_destination => (
is => 'ro',
isa => 'Str',
default => '/topic/crowded',
);
has stomp_subscribe_headers => (
is => 'ro',
isa => 'HashRef',
default => sub { +{
selector => q{custom_header = '1' or JMSType = 'test_foo'},
} },
);
This is very useful to set filters / selectors on the subscription.
There are a few caveats, mostly summarized by "if you do confusing
things, the program may not work".
=over 4
=item *
you can have the C<stomp_destination> and the C<action_namespace>
different in a single controller, but this may become confusing if you
have more than one controller subscribed to the same destination; you
can remove some of the confusion by restricting the kind of messages
that each subscription receives
=item *
if you filter out some messages, don't be surprised if they are never
received by your application
=item *
you can set persistent topic subscriptions, to prevent message loss
during reconnects (the broker will remember your subscription and keep
the messages while you are not connected):
MyApp->config(
Engine::Stomp' = {
'servers' => [
{
'hostname' => 'localhost',
'port' => '61613'
},
],
connect_headers => {
'client-id' => 'myapp',
},
},
);
package MyApp::Controller::Persistent;
use Moose;
BEGIN { extends 'Catalyst::Controller::MessageDriven' };
has stomp_destination => (
is => 'ro',
isa => 'Str',
default => '/topic/important',
);
has stomp_subscribe_headers => (
is => 'ro',
isa => 'HashRef',
default => sub { +{
'activemq.subscriptionName' => 'important-subscription',
} },
);
According to the ActiveMQ docs, the C<client-id> must be globally
unique, and C<activemq.subscriptionName> must be unique within the
client. Non-ActiveMQ brokers may use different headers to specify the
subscription name.
=back
=head1 FAILOVER
You can specify one or more servers in a list for the apps config.
This enables fail over if an error occurs, like the broker or network
connection disappears.
It will try to use a server a set number of times, as determined by
tries_per_server in the config before failing on to the next server
in the list. It cycle through each server in turn, going back to the
start of the list if need be.
=head1 UTF-8
By default STOMP messages are assumed to be in UTF-8. This module can
automatically convert a Perl string into a UTF-8 set of octets to be
sent over the wire instead. This is a Good Thing, especially if you
use the function Load() from the package YAML::XS to un-serialize
it in your client - it assumes it is in UTF-8.
If you do want this behaviour, set 'utf8' to '1' in your config.
=head1 Simplified configuration
Instead of using the complete config layout as shown in the synopsis,
you can
=over 4
=item *
not specify a C<tries_per_server> (defaults to 1)
=item *
specify a single server:
server => { hostname => $host, port => $port }
=item *
use the old-style (pre 0.14) config having C<hostname> and C<port>
directly in the C<Engine::Stomp> block, without a C<server> key in
between.
=back
=cut
=head1 METHODS
=head2 _see_ya
Set to run when signal USR1 is received. Simply sets the stop flag.
=cut
my $stop = 0;
sub _see_ya {
$stop = 1;
delete $SIG{'USR1'};
}
=head2 run
App entry point. Starts a loop listening for messages.
If the stop flag is set (see _see_ya above) then no more requests are processed.
Keep in mind this is a blocking server and it will wait for a STOMP message forever.
Only after handling a request does it check the flag.
=cut
sub _qualify_destination {
my ($self,$unq_dest) = @_;
my $q_dest = $unq_dest;
if ($unq_dest !~ m{^ /? (?: queue | topic ) / }x) {
$q_dest = "/queue/$unq_dest";
}
# normalize slashes
$unq_dest =~ s{^/}{};
$q_dest =~ s{/+}{/};
$q_dest = "/$q_dest" unless $q_dest =~ m{^/};
return ($unq_dest,$q_dest);
}
sub _collect_destinations {
my ($self,$app) = @_;
my $sub_id=1;
my @dests;
for my $ctrl_name ($app->controllers) {
my $ctrl = $app->controller($ctrl_name);
my $dest_call = $ctrl->can('stomp_destination');
my $subh_call = $ctrl->can('stomp_subscribe_headers');
my ($unq_dest,$q_dest,$subh);
$unq_dest = $dest_call ? $ctrl->$dest_call() : $ctrl->action_namespace();
($unq_dest,$q_dest) = $self->_qualify_destination($unq_dest);
$subh = $subh_call ? $ctrl->$subh_call() : { };
push @dests,{
destination => $q_dest,
subscribe_headers => {
%$subh,
id => $sub_id,
}
};
$self->destination_namespace_map->{$q_dest} =
$self->destination_namespace_map->{"/subscription/$sub_id"} =
$ctrl->action_namespace();
++$sub_id;
}
return @dests;
}
sub run {
my ($self, $app, $oneshot) = @_;
$SIG{'USR1'} = \&_see_ya;
die 'No Engine::Stomp configuration found'
unless ref $app->config->{'Engine::Stomp'} eq 'HASH';
my @destinations = $self->_collect_destinations($app);
# connect up
my $config = $app->config->{'Engine::Stomp'};
my $index = 0;
# munge the configuration to make it easier to write
$config->{tries_per_server} ||= 1;
$config->{connect_retry_delay} ||= 15;
for my $h (qw(connect subscribe)) {
$config->{"${h}_headers"} ||= {};
die("${h}_headers config for Engine::Stomp must be a hashref!\n")
if (ref($config->{"${h}_headers"}) ne 'HASH');
}
if (! $config->{servers} ) {
$config->{servers} = [ {
hostname => (delete $config->{hostname}),
port => (delete $config->{port}),
} ];
}
elsif (ref $config->{servers} eq 'HASH') {
$config->{servers} = [ $config->{servers} ];
}
QUITLOOP:
while (1) {
# Go to next server in list
my %template = %{ $config->{servers}->[$index] };
$config->{hostname} = $template{hostname};
$config->{port} = $template{port};
++$index;
if ($index >= (scalar( @{$config->{servers}} ))) {
$index = 0; # go back to first server in list
}
my $tries = 0;
while ($tries < $config->{tries_per_server}) {
++$tries;
eval {
for my $h (qw(connect subscribe)) {
$template{"${h}_headers"} ||= {};
die("${h}_headers config for for server $config->{hostname}:$config->{port} in Engine::Stomp must be a hashref!\n")
if (ref($template{"${h}_headers"}) ne 'HASH');
}
my $per_server_connect_headers = {
%{$config->{connect_headers}},
%{$template{connect_headers}},
};
my $per_server_subscribe_headers = {
%{$config->{subscribe_headers}},
%{$template{subscribe_headers}},
};
$app->log->debug("Connecting to STOMP Q at " . $template{hostname}.':'.$template{port})
if $app->debug;
$self->connection(Net::Stomp->new(\%template));
$self->connection->connect($per_server_connect_headers);
$self->conn_desc($template{hostname}.':'.$template{port});
# subscribe, with client ack.
foreach my $destination (@destinations) {
my $dest_name = $destination->{destination};
my $local_subscribe_headers =
$destination->{subscribe_headers};
my $id = $local_subscribe_headers->{id};
$app->log->debug(
"subscribing to $dest_name ($id) ".
'which is mapped to '.
$self->destination_namespace_map->{"/subscription/$id"}
)
if $app->debug;
$self->connection->subscribe({
%$per_server_subscribe_headers,
%$local_subscribe_headers,
destination => $dest_name,
ack => 'client',
});
}
# Since we might block for some time, lets flush the log messages
$app->log->_flush() if $app->log->can('_flush');
# enter loop...
while (1) {
my $frame = $self->connection->receive_frame(); # block
$self->handle_stomp_frame($app, $frame);
if ( $ENV{ENGINE_ONESHOT} || $stop ){
# Perl does not like 'last QUITLOOP' inside an eval, hence we die and do it
die "QUITLOOP\n";
}
}
};
if (my $err=$@) {
# although it looks like a lot of pointless flush()ing we need
# to make sure the user(s) can see any new messages; we
# sometimes die before we flush() in the loop above
if ($err eq "QUITLOOP\n") {
last QUITLOOP;
}
else {
$app->log->error(" Problem dealing with STOMP : $err");
$app->log->_flush() if $app->log->can('_flush');
}
# don't loop continuously if we can't connect; take a break;
# give the service a chance to come back
if ($err =~ m{Connection refused}) {
$app->log->info(
'Unable to connect to '
. $template{hostname}.':'.$template{port}
. '; sleeping before next retry'
);
$app->log->_flush() if $app->log->can('_flush');
sleep $config->{connect_retry_delay};
}
}
}
}
}
=head2 prepare_request
Overridden to add the source broker to the request, in place of the
client IP address.
=cut
sub prepare_request {
my ($self, $c, $req, $res_ref) = @_;
shift @_;
$self->next::method(@_);
$c->req->address($self->conn_desc);
}
=head2 finalize_headers
Overridden to dump out any errors encountered, since you won't get a #'
"debugging" message as for HTTP.
=cut
sub finalize_headers {
my ($self, $c) = @_;
my $error = join "\n", @{$c->error};
if ($error) {
$c->log->debug($error);
}
return $self->next::method($c);
}
=head2 handle_stomp_frame
Dispatch according to Stomp frame type.
=cut
sub handle_stomp_frame {
my ($self, $app, $frame) = @_;
my $command = $frame->command();
if ($command eq 'MESSAGE') {
$self->handle_stomp_message($app, $frame);
}
elsif ($command eq 'ERROR') {
$self->handle_stomp_error($app, $frame);
}
else {
$app->log->debug("Got unknown Stomp command: $command");
}
}
=head2 handle_stomp_message
Dispatch a Stomp message into the Catalyst app.
=cut
sub handle_stomp_message {
my ($self, $app, $frame) = @_;
# destination -> controller
my $destination = $frame->headers->{destination};
my $subscription = $frame->headers->{subscription};
$app->log->debug("message from $destination ($subscription)")
if $app->debug;
my $controller = $self->destination_namespace_map->{"/subscription/$subscription"}
|| $self->destination_namespace_map->{$destination};
# set up request
my $config = $app->config->{'Engine::Stomp'};
my $url = 'stomp://'.$config->{hostname}.':'.$config->{port}.'/'.$controller;
my $request_headers = HTTP::Headers->new(%{$frame->headers});
my $req = HTTP::Request->new(POST => $url, $request_headers);
$req->content($frame->body);
$req->content_length(length $frame->body);
# dispatch
my $response;
$app->handle_request($req, \$response);
# reply, if header set
if (my $reply_to = $response->headers->header('X-Reply-Address')) {
my $reply_queue = '/remote-temp-queue/' . $reply_to;
my $content = $response->content;
if ($config->{utf8}) {
$content = encode("utf8", $response->content); # create octets
}
my $reply_headers = $response->headers->clone;
$reply_headers->remove_content_headers;
my %reply_hh =
map {
lc($_), scalar($reply_headers->header($_)),
}
grep { !/^X-/i }
$reply_headers->header_field_names();
$self->connection->send({
%reply_hh,
destination => $reply_queue,
body => $content
});
}
# ack the message off the destination now we've replied / processed
$self->connection->ack( { frame => $frame } );
}
=head2 handle_stomp_error
Log any Stomp error frames we receive.
=cut
sub handle_stomp_error {
my ($self, $app, $frame) = @_;
my $error = $frame->headers->{message};
$app->log->debug("Got Stomp error: $error");
}
__PACKAGE__->meta->make_immutable;
=head1 CONFIGURATION
=head2 subscribe_header
Add additional header key/value pairs to the subscribe message sent to the
message broker.
=cut
=head1 DEVELOPMENT
The source to Catalyst::Engine::Stomp is in github:
http://github.com/pmooney/catalyst-engine-stomp
=head1 AUTHOR
Chris Andrews C<< <chris@nodnol.org> >>
=head1 CONTRIBUTORS
Tomas Doran (t0m) C<< <bobtfish@bobtfish.net> >>
Jason Tang
Paul Mooney
=head1 LICENCE AND COPYRIGHT
Copyright (C) 2009 Venda Ltd
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.
=cut