Closed rootlevel closed 7 years ago
Hi, can you please post complete stack trace of an error?
Why did you put an instance of InterpretationDataProducer class as a dependency to itself? And same thing with the producer. You don't need to describe console commands as services in DI container, rather you can use the built-in ones like this:
// should be in console.php
'controllerMap' => [
'rabbitmq-consumer' => \mikemadisonweb\rabbitmq\controllers\ConsumerController::class,
'rabbitmq-producer' => \mikemadisonweb\rabbitmq\controllers\ProducerController::class,
],
Consumer command here would trigger a method execute() in a Consumer class that you defined in a 'callback' parameter. Example of Consumer class:
<?php
use mikemadisonweb\rabbitmq\components\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
class ImportDataConsumer implements ConsumerInterface
{
/**
* @param AMQPMessage $msg
* @return bool
*/
public function execute(AMQPMessage $msg)
{
$data = unserialize($msg->body);
var_dump($data);
return ConsumerInterface::MSG_ACK;
}
}
If this callback class have a dependency, then it's worth to define it as a singleton service in DI. As for the producer, there is no need to define it as service as well. After you configure one in 'producers' array you can get it like so:
$producer = \Yii::$container->get(sprintf('rabbit_mq.producer.%s', 'event_main_interpretation'));
Thanks Mikhail! After reading your comments i was understand, the problem in my config and producer container name. I completely deleted my container initialization (DI singleton containers configs). After that i changed "producer"-call like you tell me:
$producer = \Yii::$container->get(sprintf('rabbit_mq.producer.%s', 'event_main_interpretation'));
And... fantastic, it works! But all my produced tasks wrote to default queue. The default queue named like amq.gen-uk3TX_mm1usNpaP1bxcTYA. Hmmm..... I think i missed something. I missed producer queue_options in my rabbitmq componen config.
return [
'class' => 'mikemadisonweb\rabbitmq\Configuration',
'connections' => [
'default' => [
'host' => '192.168.22.112',
'port' => '5672',
'user' => 'loginxxx',
'password' => 'XXXX',
'vhost' => 'interpretation_host',
'heartbeat' => 0,
],
],
'producers' => [
'event_main_interpretation' => [
'connection' => 'default',
'exchange_options' => [
'name' => 'main_interpretation',
'type' => 'direct',
],
'queue_options' => [
'name' => 'interpretation',
'routing_keys' => ['interpretation'],
'durable' => true,
'auto_delete' => false,
],
],
],
'consumers' => [
'event_main_interpretation' => [
'connection' => 'default',
'exchange_options' => [
'name' => 'main_interpretation',
'type' => 'direct',
],
'queue_options' => [
'name' => 'interpretation',
'routing_keys' => ['interpretation'],
'durable' => true,
'auto_delete' => false,
],
'callback' => \app\commands\interpretation\InterpretationDataConsumer::class,
],
],
Now all functionaloty it's works fine. Thanks again!
您好,我在执行Producer Times时犯了这个错误。 Class rabbit_mq.producer.import_data does not exist
Hi! Firstly... thanks to all contributors for awesome package! The second one, can some one explain to me why my producer throw this error (if i trying place simple message to my queue).
rabbitmq component configuration (in both... console.php and web.php file configs)
My container definition ("included" in web and console config)
Code in controller which place message to queue:
Producer (\app\commands\interpretation\InterpretationDataProducer)
Can some one help to me?