我试图编写一个监听ActiveMQ的进程并根据消息,从Web服务中获取数据,进行一些处理,然后将流程数据放到另一个Web服务中。 (REST / JSON)
以下模块正常工作,直到我与之交谈的一个不稳定的网络服务返回错误。我已经尝试过许多方法来捕捉错误,但无济于事。一旦发生webservice错误,我收到以下消息:
事件未处理的回调异常(MESSAGE, AnyEvent :: STOMP :: Client = HASH(0x3ad5e48),HASH(0x3a6bbb0) {"动作":"创建""数据" {" ID":40578737,"类型":& #34;警报""谁":空}" GUID":" ADCCEE0C-73A7-11E6-8084-74B346D1CA67""主机名":" 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();
答案 0 :(得分:1)
我能够通过使用Try :: Catch并使用try catch将调用包装到self-gt; process来解决这个问题:
$stomp->on_message(sub {
my ($stomp, $header, $body) = @_;
my $href = decode_json $body;
$pm->start(cb => sub {
my ($pm, @params) = @_;
try {
$self->process(@params);
}
catch {
# error handling stuff
};
},
args => [ $href->{id}, $href->{data}->{type}, $href->{data}->{who} ],
);
}
);