Update documentation for Symfony 6
Hi,
I'm trying to implement rabbitMQ with RPC requests but the documentation seems to be madde for older versions of Symfony, especially when fetching the RpcClient in the Controller.
The documentation uses this code: $client = $this->get('old_sound_rabbit_mq.integer_store_rpc'); but when I try this, I get the following error:
Service "old_sound_rabbit_mq.api_request_rpc" not found: even though it exists in the app's container, the container inside "App\Controller\ApiController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "message_bus", "messenger.default_bus", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer" and "session" services. Try using dependency injection instead.
I tried to create a \App\Client\ApiRequestClient that simply extends the \OldSound\RabbitMqBundle\RabbitMq\RpcClient but this also throws the following Exception:
Cannot resolve argument $apiRequestClient of "App\Controller\ApiController::foo()": Cannot autowire service "App\Client\ApiRequestClient": argument "$conn" of method "OldSound\RabbitMqBundle\RabbitMq\BaseAmqp::__construct()" references class "PhpAmqpLib\Connection\AbstractConnection" but no such service exists. You should maybe alias this class to the existing "old_sound_rabbit_mq.connection.default" service.
Here are my files:
config/packages/old_sound_rabbit_mq.yaml:
old_sound_rabbit_mq:
connections:
default:
url: '%env(RABBITMQ_URL)%'
use_socket: true
lazy: true
rpc_clients:
api_request:
connection: default
unserializer: json_decode
lazy: true
direct_reply_to: false
expect_serialized_response: false
rpc_servers:
api_request:
connection: default
callback: 'App\Consumer\ApiRequestConsumer'
qos_options: { prefetch_size: 0, prefetch_count: 1, global: false }
exchange_options: { name: api_request, type: topic }
queue_options: { name: api_request_queue, durable: false, auto_delete: true }
serializer: json_encode
src/Controller/AcmeController.php:
<?php
namespace App\Controller;
class AcmeController extends AbstractController
{
public function foo()
{
$msg = [
'foo' => 'Foo value',
'bar' => 'Bar value',
];
$client = $this->get('old_sound_rabbit_mq.integer_store_rpc');
$client->addRequest(json_encode($msg), 'api_request', 'acmeRoute');
try {
$replies = $client->getReplies();
} catch (\PhpAmqpLib\Exception\AMQPTimeoutException $e) {
// Silence is golden
}
}
}
src/Consumer/ApiRequestConsumer.php:
<?php
namespace App\Consumer;
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Message\AMQPMessage;
class ApiRequestConsumer implements ConsumerInterface
{
public function execute(AMQPMessage $msg)
{
return ['success' => true, 'message' => 'RPC request complete!'];
}
}
Thanks for your help.
I'll leave this here since I was fighting with the bundle for like 1.5h to make it run. The documentation is vast but it's easy to get lost in it if You are new to AMQP like me. I basically debugged the code to make it work. No idea how stable it is but at least i got now some messages in queue.

Symfony: 5.3.x
-
NO need to define the
producerinsideconfig/packages/old_sound_rabbit_mq - Create new class
/**
* Needed because otherwise the bundle tries to initialize the {@see AbstractConnection} and will crush,
* This class is basically used as replacement for {@see AbstractConnection} to fix the instantiation issue
*/
class Connection extends AbstractConnection
{
}
- Go to
services.yamland add this to overwrite the calls toAbstractConnectionwithConnection
PhpAmqpLib\Connection\AbstractConnection: '@App\RabbitMq\Connection\Connection'
- The
AbstractConnectionrequires theioargument so add toservices.yamlalso this
PhpAmqpLib\Wire\IO\SocketIO:
arguments:
$host: '%env(RABBITMQ_HOST)%'
$port: '%env(int:RABBITMQ_PORT)%'
- And now bind arguments to the
Connectionwhich replacesAbstractConnection
App\RabbitMq\Connection\Connection:
arguments:
$user: '%env(RABBITMQ_LOGIN)%'
$password: '%env(RABBITMQ_PASSWORD)%'
$io: '@PhpAmqpLib\Wire\IO\SocketIO'
- Separate
RABBITMQ_URLfrom.envto
RABBITMQ_URL=amqp://${RABBITMQ_LOGIN}:${RABBITMQ_PASSWORD}@${RABBITMQ_HOST}:${RABBITMQ_PORT}
RABBITMQ_LOGIN=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_PORT=5672
RABBITMQ_HOST=localhost
- Create producer like that
/**
* @description dummy test producer, nobody cares about the message. It's just for testing if producing works
*/
class TestProducer extends Producer
{
protected $exchangeOptions = array(
'name' => 'test',
'type' => 'topic',
'passive' => false,
'durable' => true,
'auto_delete' => false,
'internal' => false,
'nowait' => false,
'arguments' => null,
'ticket' => null,
'declare' => true,
);
/**
* {@inheritDoc}
**/
public function publish($msgBody, $routingKey = null, $additionalProperties = array(), array $headers = null): void
{
parent::publish($msgBody);
}
}
It's important to overwrite the protected $exchangeOptions by adding the name & test, else it will throw exceptions.
That's all I managed to do for now. My goal was to see if producing works, I dunno about much about the types, what name is for but it works.
Personnally I ended up using this bundle : https://php-enqueue.github.io/symfony which is way easier to configure and worked out of the box with Symfony 6, rabbitMQ in RPC mode etc.
Personnally I ended up using this bundle : https://php-enqueue.github.io/symfony which is way easier to configure and worked out of the box with Symfony 6, rabbitMQ in RPC mode etc.
Thx, might use that too then if things start crashing.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue was closed because it has been stalled for 10 days with no activity.