Install the Bundle
Add to your composer.json:
composer require ano/barbeq-bundle
Enable in config/bundles.php:
return [
// ...
Ano\BarbeQBundle\AnoBarbeQBundle::class => ['all' => true],
];
Configure BarbeQ
Update config/packages/ano_barbeq.yaml (auto-generated after installation):
ano_barbeq:
connection: 'amqp://user:pass@localhost:5672/%2f'
exchange: 'your_exchange'
queue: 'your_queue'
routing_key: 'your.routing.key'
First Use Case: Publishing a Message
Inject the BarbeQPublisher service and publish a message:
use Ano\BarbeQBundle\Publisher\BarbeQPublisher;
class SomeService
{
public function __construct(private BarbeQPublisher $publisher)
{
}
public function sendMessage()
{
$this->publisher->publish('your.message', ['data' => 'example']);
}
}
Publishing Messages
BarbeQPublisher to send messages to RabbitMQ:
$this->publisher->publish('event.name', ['payload' => $data]);
BarbeQPublisher::setSerializer().Consuming Messages
Ano\BarbeQBundle\Consumer\ConsumerInterface:
use Ano\BarbeQBundle\Consumer\ConsumerInterface;
class MyConsumer implements ConsumerInterface
{
public function consume($message)
{
$data = json_decode($message, true);
// Process $data
}
}
services.yaml:
services:
App\Consumer\MyConsumer:
tags:
- { name: 'ano_barbeq.consumer', routing_key: 'your.routing.key' }
Error Handling
Ano\BarbeQBundle\Consumer\ErrorHandlerInterface for custom error logic:
class MyErrorHandler implements ErrorHandlerInterface
{
public function handleError($message, \Exception $e)
{
// Log or retry logic
}
}
services.yaml:
services:
App\ErrorHandler\MyErrorHandler:
tags: ['ano_barbeq.error_handler']
Dynamic Routing
BarbeQPublisher::publishWithRoutingKey() for dynamic routing:
$this->publisher->publishWithRoutingKey('dynamic.key', ['data' => 'value']);
Symfony Events: Trigger message publishing in event listeners:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MessageSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return ['event.name' => 'onEvent'];
}
public function onEvent()
{
$this->publisher->publish('event.processed', []);
}
}
Dependency Injection:
BarbeQPublisher and consumers to ensure testability.Configuration Overrides:
config/packages/dev/ano_barbeq.yaml).Connection Issues
ano_barbeq.connection in config matches your RabbitMQ setup. Test with:
php bin/console debug:container --parameter=ano_barbeq.connection
Consumer Not Triggering
ano_barbeq.consumer.routing_key in the tag matches the published message’s routing key.Serialization Errors
JsonSerializable or use a custom serializer:
# config/packages/ano_barbeq.yaml
ano_barbeq:
serializer: 'App\Serializer\CustomSerializer'
Duplicate Consumers
Check Queue Status
Use RabbitMQ management UI (http://localhost:15672) to verify:
Enable Logging
Add to config/packages/monolog.yaml:
handlers:
ano_barbeq:
type: stream
path: "%kernel.logs_dir%/ano_barbeq.log"
level: debug
channels: ["ano_barbeq"]
Then configure the bundle to use the channel:
# config/packages/ano_barbeq.yaml
ano_barbeq:
logging_channel: "ano_barbeq"
Test Locally Use a Dockerized RabbitMQ for development:
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:management
Custom Serializers
Implement Ano\BarbeQBundle\Serializer\SerializerInterface and bind it in services.yaml:
services:
App\Serializer\ProtobufSerializer:
tags: ['ano_barbeq.serializer']
Middleware Pipeline Add processing steps before/after message handling:
use Ano\BarbeQBundle\Consumer\Middleware\ConsumerMiddlewareInterface;
class LoggingMiddleware implements ConsumerMiddlewareInterface
{
public function handle($message, callable $next)
{
\Log::info('Processing message', ['message' => $message]);
return $next($message);
}
}
Register in services.yaml:
services:
App\Middleware\LoggingMiddleware:
tags: ['ano_barbeq.consumer_middleware']
Retry Logic
Extend Ano\BarbeQBundle\Consumer\ConsumerInterface to implement retries:
class RetryConsumer implements ConsumerInterface
{
private $retries = 0;
private $maxRetries = 3;
public function consume($message)
{
try {
// Business logic
} catch (\Exception $e) {
if ($this->retries++ < $this->maxRetries) {
throw $e; // Requeue automatically
}
throw new \RuntimeException('Max retries exceeded');
}
}
}
How can I help you explore Laravel packages today?