如何将RabbitMqBundle生成器注入服务?

时间:2018-04-16 13:09:45

标签: php symfony symfony4

我将RabbitMqBundle的RabbitMq生产者注入我的服务时遇到问题。

服务:     

print

old_sound_rabbit_mq.yml:

namespace App\Service;

use Carbon\Carbon;
use Doctrine\ORM\EntityManagerInterface;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;

class RatingPositionRecalculateService
{

    protected $entityManager;

    protected $positionProducer;

    public function __construct(EntityManagerInterface $entityManager, ProducerInterface $producer)
    {
        $this->entityManager = $entityManager;
        $this->positionProducer = $producer;
    }

    public function recalculate(Carbon $day)
    {
        // do stuff
    }

}

我得到了:

old_sound_rabbit_mq:
    connections:
        default:
            url: ''

    producers:
        rating_position:
            connection:       default
            exchange_options: { name: 'rating_position', type: direct }

    consumers:
        rating_position:
            connection:       default
            exchange_options: {name: 'rating_position', type: direct}
            queue_options:    {name: 'rating_position'}
            callback:         rating_position_service

我尝试过使用services.yml:

进行布线
Cannot autowire service "App\Service\RatingPositionRecalculateService": argument "$producer" of method "__construct()" references interface "OldSound\RabbitMqBundle\RabbitMq\ProducerInterface" but no such service exists. You should maybe alias this interface to the existing "old_sound_rabbit_mq.rating_position_producer" service. Did you create a class that implements this interface?

但我仍然得到同样的例外。

2 个答案:

答案 0 :(得分:6)

如果您只有一个制作人,可以在services.yaml中定义这样的别名:

OldSound\RabbitMqBundle\RabbitMq\ProducerInterface: '@old_sound_rabbit_mq.rating_position_producer'

但我建议你为你的制作人创建一个空类:

// src/Producer/RatingPositionProducer.php

<?php

namespace App\Producer;

class RatingPositionProducer extends \OldSound\RabbitMqBundle\RabbitMq\Producer
{
}

然后,在old_sound_rabbit_mq.yml文件中:

old_sound_rabbit_mq:
    ...
    producers:
        rating_position:
           class: App\Producer\RatingPositionProducer
           ...

services.yaml文件中:

App\Producer\RatingPositionProducer: '@old_sound_rabbit_mq.rating_position_producer'

最后,在你的RatingPositionRecalculateService班级

// src/Service/RatingPositionRecalculateService.php

namespace App\Service;

use Carbon\Carbon;
use Doctrine\ORM\EntityManagerInterface;
use App\Producer\RatingPositionProducer;

class RatingPositionRecalculateService
{
    ...

    public function __construct(EntityManagerInterface $entityManager, RatingPositionProducer $producer)
    {
        ...
    }

答案 1 :(得分:0)

我不认为你的问题与捆绑有关。您的服务定义看起来不错。他们在the documentation中写道:

  

这里我们配置连接服务和应用程序将拥有的消息端点。在此示例中,您的服务容器将包含服务old_sound_rabbit_mq.upload_picture_producer和old_sound_rabbit_mq.upload_picture_consumer。后者期望有一项名为upload_picture_service的服务。

这意味着您应该拥有服务old_sound_rabbit_mq.rating_position_producer。您可以使用命令bin/console debug:cont --show-private | grep old_sound_rabbit_mq验证它。

但问题可能在其他地方。

我想这是:

  • 您有一个配置文件,将所有类注册为服务
  • 您有RatingPositionRecalculateService
  • 的另一个配置文件

如果是,那么错误是由于您的服务重复注册。注册所有服务时,Symfony DIC会尝试注册您的RatingPositionRecalculateService,但无法将其链接到生产者(因为生产者未在同一文件中注册)。您可以查看另一个存在类似问题的问题:External service configuration in Symfony 4