0

I'm using RabbitMQ Bundle for the Symfony2 web framework. My question is, how can I avoid creating multiple connections (to prevent overloading the broker) after running many workers in terminal? In example below, I've run two workers and ended up having two connections/channel.

config.yml

old_sound_rabbit_mq:
    connections:
        default:
            host:     127.0.0.1
            port:     5672
            user:     guest
            password: guest
            vhost:    /
            lazy:     true
    producers:
        order_create_bmw:
            connection:       default
            exchange_options: { name: order_create_ex, type: direct }
            queue_options:
                name: order_create_bmw_qu
                routing_keys:
                    - bmw
    consumers:
        order_create_bmw:
            connection:       default
            exchange_options: { name: order_create_ex, type: direct }
            queue_options:
                name: order_create_bmw_qu
                routing_keys:
                    - bmw
            callback:         application_frontend.consumer.order_create_bmw

services.yml

services:
    application_frontend.producer.order_create_bmw:
        class: Application\FrontendBundle\Producer\OrderCreateBmwProducer
        arguments:
            - @old_sound_rabbit_mq.order_create_bmw_producer

Producer

namespace Application\FrontendBundle\Producer;

use Application\FrontendBundle\Entity\Order;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;

class OrderCreateBmwProducer
{
    private $producer;

    public function __construct(ProducerInterface $producer)
    {
        $this->producer = $producer;
    }

    public function add(Order $order)
    {
        $message = [
            'order_id' => $order->getId(),
            'car_model' => $order->getCarModel(),
            'timestamp' => date('Y-m-d H:i:s')
        ];

        $this->producer->publish(json_encode($message), 'bmw');
    }
}

Running workers

$ app/console rabbitmq:consumer order_create_bmw
$ app/console rabbitmq:consumer order_create_bmw

RabbitMQ Management

enter image description here enter image description here enter image description here enter image description here

Community
  • 1
  • 1
BentCoder
  • 12,257
  • 22
  • 93
  • 165

1 Answers1

1

Every client (regardless if publisher or subscriber) that connects to rabbitmq will create a connection. Aside from using less clients, I can't think of any other way to achive this. I also can't think of a reason to do so :) If it's performance, than actually having more subscribers will help to "empty" the exchanges (and queues).

cantSleepNow
  • 9,691
  • 5
  • 31
  • 42