我正在尝试编写一个进程来侦听 ActiveMQ 并基于消息,出去并从 web 服务中获取数据,进行一些处理,然后将进程数据放到另一个 web 服务中。(REST/JSON)
下面的模块可以正常工作,直到我与之交谈的其中一个不稳定的 Web 服务返回错误。我已经尝试了很多方法来捕获错误但无济于事。尽管我收到以下消息,但一旦发生 Web 服务错误:
未处理的事件回调异常 (MESSAGE, AnyEvent::STOMP::Client=HASH(0x3ad5e48), HASH(0x3a6bbb0) {"action":"created","data":{"id":40578737,"type":" alert","who":null},"guid":"ADCCEE0C-73A7-11E6-8084-74B346D1CA67","hostname":"myserver","pid":48632}): $fork_manager->start() 应该在管理器进程中调用
好的,我从概念上理解子进程正在尝试启动另一个进程,而 fork 管理器说这是不,不。但是鉴于下面的模块,启动新进程以处理长时间运行的处理的正确方法是什么。或者为什么一个子进程死亡导致这个异常,我该如何防止这种情况
这是模块(精简)
package consumer;
use AnyEvent::ForkManager;
use AnyEvent::STOMP::Client;
use JSON;
use Data::Dumper;
use v5.18;
use Moose;
sub run {
my $self = shift;
my $pm = AnyEvent::ForkManager->new(max_workers => 20);
my $stomp = AnyEvent::STOMP::Client->new();
$stomp->connect();
$stomp->on_connected(sub {
my $stomp = shift;
$stomp->subscribe('/topic/test');
say "Connected to STOMP";
});
$pm->on_start(sub {
my ($pm,$pid,@params) = @_;
say "Starting $pid worker";
});
$pm->on_finish(sub {
my ($pm, $pid,@params) = @_;
say "Finished $pid worker";
});
$pm->on_error(sub {
say Dumper(\@_);
});
$stomp->on_message(sub {
my ($stomp, $header, $body) = @_;
my $href = decode_json $body;
$pm->start(cb => sub {
my ($pm, @params) = @_;
$self->process(@params);
},
args => [ $href->{id}, $href->{data}->{type}, $href->{data}->{who} ],
);
});
my $cv = AnyEvent->condvar;
$cv->recv;
}
sub process {
say "Processing ".Dumper(\@_);
sleep 5;
if ( int(rand(10)) < 5 ) {
die "OOPS"; # this triggers the error message above
}
say "Done Processing $_[1]";
}
1;
下面是上述模块的驱动程序:
#!/usr/bin/env perl
use v5.18;
use lib '.';
use consumer;
my $c = consumer->new();
$c->run;
最后是一个流量生成器,您可以使用它来查看此操作:
#!/usr/bin/env perl
use lib '../lib';
use lib '../../lib';
use v5.18;
use Data::Dumper;
use JSON;
use Net::STOMP::Client;
$ENV{'scot_mode'} = "testing";
my $stomp = Net::STOMP::Client->new(
host => "127.0.0.1",
port => 61613
);
$stomp->connect();
for (my $i = 1; $i < 1000000; $i++) {
my $href = {
id => $i,
type => "event",
what => "foo",
};
my $json = encode_json $href;
say "Sending ".Dumper($href);
$stomp->send(
destination => "/topic/test",
body => $json,
);
}
$stomp->disconnect();