我尝试使用Cro创建一个Rest API,该API将在RabbitMQ中发布消息。我想将路线分为不同的模块,并用“ include”组成。但是我也希望能够在每个模块中共享与RabbitMQ的相同连接。我尝试使用“我们的”,但它不起作用:
文件1:
unit module XXX::YYY;
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use Cro::HTTP::Log::File;
use XXX::YYY::Route1;
use Net::AMQP;
our $rabbitConnection is export = Net::AMQP.new;
await $rabbitConnection.connect;
my $application = route {
include <api v1 run> => run-routes;
}
...
档案2:
unit module XXX::YYY::Route1;
use UUID;
use Cro::HTTP::Router;
use JSON::Fast;
use Net::AMQP;
my $channel = $XXX::YYY::rabbitConnection.open-channel().result;
$channel.declare-queue("test_task", durable=> True );
sub run-routes() is export { ... }
错误信息:
===SORRY!===
No such method 'open-channel' for invocant of type 'Any'
谢谢!
定义可导出路由功能时,可以指定参数,然后在编写模块中可以创建共享库并将其传递给路由。例如在您的路由器模块中:
sub run-routes ($rmq) is export{
route {
... $rmq is available in here
}
}
然后,您可以在主路由器中创建队列并在包含以下内容时将其传递
my $rmq = # Insert queue creation code here
include product => run-routes( $rmq );
我没有尝试过,但是我看不出它为什么不起作用的任何原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句