emag-tech-labs/rabbitmq-bundle
Abandoned RabbitMQ integration bundle for Symfony using php-amqplib. Provides producers, consumers, CLI commands, and common messaging patterns. Project is superseded by php-amqplib/rabbitmq-bundle (^2.6); migrate to that package.
Installation:
composer require emag-tech-labs/rabbitmq-bundle
(Note: Redirect to php-amqplib/RabbitMqBundle for active maintenance.)
Configuration:
Add to config/packages/rabbitmq.yaml:
rabbit_mq:
hosts:
default:
host: 'localhost'
port: 5672
user: 'guest'
password: 'guest'
vhost: '/'
connections:
default:
host: 'default'
producers:
default:
connection: default
exchange_options:
name: 'your_exchange'
type: direct
consumers:
default:
connection: default
queue: 'your_queue'
callback: 'App\RabbitMQ\Consumer\YourConsumer'
exchange_options:
name: 'your_exchange'
type: direct
First Use Case: Publish a message from a controller:
use RabbitMQ\Client;
class MessageController extends AbstractController
{
public function send(Client $rabbitMQ)
{
$rabbitMQ->publish(
'default',
['user_id' => 123, 'action' => 'login']
);
}
}
Producer Pattern:
$rabbitMQ->publish('producer_name', $message, ['content_type' => 'application/json']);
Consumer Pattern:
namespace App\RabbitMQ\Consumer;
use RabbitMQ\Client;
class OrderConsumer
{
public function execute($message, $messageInfo)
{
// Process $message (e.g., JSON decode, validate, handle)
return true; // Acknowledge message
}
}
config/packages/rabbitmq.yaml:
consumers:
order_consumer:
callback: 'App\RabbitMQ\Consumer\OrderConsumer'
php bin/console rabbitmq:consumer order_consumer
(Use --auto-ack for auto-acknowledgment or --limit=10 for batch processing.)Message Serialization:
json_encode/json_decode. Override via serializer config:
rabbit_mq:
producers:
default:
serializer: 'App\RabbitMQ\Serializer\CustomSerializer'
Error Handling:
RabbitMQ\Client\ProducerInterface to customize retry behavior.exchange_options:
exchange_options:
name: 'your_exchange'
type: direct
dead_letter_exchange: 'dlx_exchange'
Symfony Events:
Bind RabbitMQ to Symfony events (e.g., kernel.request):
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use RabbitMQ\Client;
class RabbitMQListener
{
public function onKernelResponse(FilterResponseEvent $event, Client $rabbitMQ)
{
$rabbitMQ->publish('events', ['event' => 'response', 'data' => $event->getResponse()->getContent()]);
}
}
Dependency Injection:
RabbitMQ\Client directly into services/controllers.RabbitMQ\Client\ConsumerInterface for type-hinting.Testing:
RabbitMQ\Client in PHPUnit:
$this->createMock(RabbitMQ\Client::class);
RabbitMQ\Client\MockClient for integration tests (if available).Environment-Specific Configs:
Override configs per environment (e.g., config/packages/rabbitmq/dev.yaml):
rabbit_mq:
hosts:
default:
host: 'rabbitmq-dev'
Abandoned Package:
php-amqplib/RabbitMqBundle (v2.6+).Connection Management:
try-catch blocks and ensure close() is called:
try {
$rabbitMQ->publish(...);
} finally {
$rabbitMQ->close();
}
Consumer Lifecycle:
try-catch and log errors:
public function execute($message, $messageInfo)
{
try {
// Process message
} catch (\Exception $e) {
\Log::error('Consumer error: ' . $e->getMessage());
throw $e; // Reject message
}
}
Message Ordering:
priority in basic_publish or implement custom sequencing.Configuration Overrides:
php bin/console cache:clear
Logs:
Enable RabbitMQ logs in config/packages/rabbitmq.yaml:
rabbit_mq:
logging: true
(Logs appear in Symfony’s monolog channel.)
RabbitMQ Management UI:
http://localhost:15672 (default credentials: guest/guest).Common Errors:
Connection refused: Verify host, port, and credentials.No such exchange: Ensure exchange is declared before publishing.Consumer timeout: Increase timeout in consumer config or optimize processing.Custom Serializers:
Extend RabbitMQ\Client\Serializer\SerializerInterface:
class CustomSerializer implements SerializerInterface
{
public function serialize($data) { /* ... */ }
public function unserialize($data) { /* ... */ }
}
Register in config:
rabbit_mq:
producers:
default:
serializer: 'App\RabbitMQ\Serializer\CustomSerializer'
Middleware: Add pre/post-processing to producers/consumers:
$rabbitMQ->getProducer('default')->addMiddleware(new \App\RabbitMQ\Middleware\LoggingMiddleware());
Dynamic Exchanges/Queues:
Use RabbitMQ\Client\DynamicProducer for runtime declarations:
$dynamicProducer = new DynamicProducer($connection, 'dynamic_exchange');
$dynamicProducer->publish($message);
Health Checks: Integrate with Symfony’s health system:
use Symfony\Component\HealthCheck\RabbitMqConnectionCheck;
services:
Symfony\Component\HealthCheck\RabbitMqConnectionCheck:
arguments:
$rabbitMQ: '@rabbit_mq.client'
How can I help you explore Laravel packages today?