如何将RabbitMqBundle生产者注入服务?

sim*_*imm 1 php symfony symfony4

我有注射从RabbitMQ的生产问题RabbitMqBundle到我的服务。

服务:

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
    }

}
Run Code Online (Sandbox Code Playgroud)

old_sound_rabbit_mq.yml:

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
Run Code Online (Sandbox Code Playgroud)

我得到:

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?
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用services.yml进行接线:

rating_position_recalculate_service:
        class: App\Service\RatingPositionRecalculateService
        autowire: false
        arguments:
            $entityManager: '@doctrine.orm.entity_manager'
            $producer: '@old_sound_rabbit_mq.rating_position_producer'
Run Code Online (Sandbox Code Playgroud)

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

fxb*_*xbt 7

如果只有一个生产者,则可以在您的中定义一个别名services.yaml

OldSound\RabbitMqBundle\RabbitMq\ProducerInterface: '@old_sound_rabbit_mq.rating_position_producer'
Run Code Online (Sandbox Code Playgroud)

但我建议您为生产者创建一个空类:

// src/Producer/RatingPositionProducer.php

<?php

namespace App\Producer;

class RatingPositionProducer extends \OldSound\RabbitMqBundle\RabbitMq\Producer
{
}
Run Code Online (Sandbox Code Playgroud)

然后,在您的 old_sound_rabbit_mq.yml文件中:

old_sound_rabbit_mq:
    ...
    producers:
        rating_position:
           class: App\Producer\RatingPositionProducer
           ...
Run Code Online (Sandbox Code Playgroud)

在您的services.yaml文件中:

App\Producer\RatingPositionProducer: '@old_sound_rabbit_mq.rating_position_producer'
Run Code Online (Sandbox Code Playgroud)

最后,在你的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)
    {
        ...
    }
Run Code Online (Sandbox Code Playgroud)