Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 and RabbitMqBundle. Can't publish a message

I am trying to use syfmony2 framework with RabbitMqBundle from here

I am sure that my rabbitmq server is up and running and I am doing the configuration and publishers code accordingly to the docs delivered on github. Unfortunately I can`t add any message to the queue.

I am sure that my rabbitmq server is up and running. I have queue named accordingly to the symfony configuration file.

Have anyone got any clue what is wrong?

Thanks in advance for any suggestions.

like image 666
Gabriel Filipiak Avatar asked Oct 10 '12 14:10

Gabriel Filipiak


1 Answers

well... try this simple example

# app/config.yml
old_sound_rabbit_mq:
    connections: %rabbitmq_connections%
    producers: %rabbitmq_producers%
    consumers: %rabbitmq_consumers%

parameters:
    # connection parameters
    rabbitmq_connections:
        default: { host: 'localhost', port: 5672, user: 'guest', password: 'guest', vhost: '/' }

    # define producers
    rabbitmq_producers:
        sample:
            connection:         default
            exchange_options:   {name: 'exchange_name', type: direct, auto_delete: false, durable: true}

    # define consumers
    rabbitmq_consumers:
        sample:
            connection:         default
            exchange_options:   {name: 'exchange_name', type: direct, auto_delete: false, durable: true}
            queue_options:      {name: 'sample', auto_delete: false}
            callback:           rabbitmq.callback.service

then you should define your callback service. feel free to put it in app/config.yml

services:
    rabbitmq.callback.service:
        class: RabbitMQ\Callback\Service

and yes. you should write this callback service. here is simple implementation. should be enough for understanding and check is it works for you.

namespace RabbitMQ\Callback;

use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Message\AMQPMessage;

class Service implements ConsumerInterface 
{
    public function execute(AMQPMessage $msg)
    {
        var_dump(unserialize($msg->body));
    }
}     

then you should start rabbitmq server, run consumer and check was new exchange and queue added. to run test consumer you should run

app/console rabbitmq:consumer sample --route="sample"

in your controller (where you want to send message to rabbitMQ put next code

# get producer service
$producer = $this->get('old_sound_rabbit_mq.sample_producer');
# publish message
$producer->publish(serialize(array('foo'=>'bar','_FOO'=>'_BAR')), 'sample');

Hope it's more or less clear and will help you with rabbitmq.

PS: it's easier to debug if you have rabbitmq management plugin. if you have no, use console commands like rabbitmqctl to check queues/exchanges/consumers and so on...

and also would be nice to see your configuration for producers/consumers. callback services code as well.

like image 78
chmeliuk Avatar answered Nov 03 '22 13:11

chmeliuk