Symfony2, RabbitMQ: I'm lost

I have installed RabbitMQ Bundle already. Now, here's what I want to do:

Controller: Creates a Redis-List, passes a message to the client, and then posts the message to the queue, so a heavier background task can run asynchronously.

But I'm lost.

$msg = array('userid' => $someid);
$this->get('old_sound_rabbit_mq.task_example_producer')->publish(serialize($msg));

      

Will it send some data to the product? And the corresponding consumer will do a heavy background task (DB queries, etc., based on the "userid" from the producer)? Do I need a callback? What queue ?! Does the queue forward messages from the producer to the consumer one by one? So, can I have multiple consumers handle more messages at the same time ?!

+3


source to share


3 answers


Regards, but if anyone else is looking for help:

You seem to be using the old_sound rabbitmq package. There is some useful tutorial type documentation here: https://github.com/videlalvaro/RabbitMqBundle

This helped me get my head around the symfony rabbit.

In a nutshell:

1: you need to have some configuration in your config.yml file. For example:

# RabbitMQ Configuration
old_sound_rabbit_mq:
    connections:
        default:
            host:        'localhost'
            port:        5672
            user:        'guest'
            password:    'guest'
            vhost:       '/'
            lazy:        true
            connection_timeout: 3
            read_write_timeout: 3

            # requires php-amqplib v2.4.1+ and PHP5.4+
            keepalive: false

            # requires php-amqplib v2.4.1+
            heartbeat: 0

    producers:
        task_example:
            connection:       default
            exchange_options: {name: 'task_example', type: direct}
    consumers:
        task_example:
            connection:       default
            exchange_options: {name: 'task_example', type: direct}
            queue_options:    {name: 'task_example'}
            callback:         test_class

      

The connection is defined here as well as one producer and one consumer. Both use the same default connection. You also need to define the callback as a service:

# My services
services:
   test_class:
      class:      AppBundle\Testclasses\rabbittest\testclass
      arguments:  [@logger]

      

2: Now you need to have a consumer, which is the callback here, "test_class". A simple consumer might look like this:

namespace AppBundle\Testclasses\rabbittest;

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

class testclass implements ConsumerInterface
{
   private $logger; // Monolog-logger.

   // Init:
   public function __construct( $logger )
   {
      $this->logger = $logger;
      echo "testclass is listening...";
   }

   public function execute(AMQPMessage $msg)
   {
      $message = unserialize($msg->body);
      $userid = $message['userid'];
      // Do something with the data. Save to db, write a log, whatever.
   }
}

      



3: And now the producer you already had:

$msg = array('userid' => $someid);
$this->get('old_sound_rabbit_mq.task_example_producer')->publish(serialize($msg));

      

4: And the final piece of the puzzle drives the consumer. The consumer is started from the console, I am developing on a Windows machine and using Windows PowerShell. You can start the consumer like this:

php app/console rabbitmq:consumer task_example

      

And it should give you the text:

testclass is listening ...

if you copied this from this example. This text is not needed, and without it the console will not display anything, but it will work fine. Unless some error is specified.

But remember that you must be in the directory where your symfony application is located. For example:

C: \ WAMP \ WWW \ Symfony \ my_project

+7


source


A queue is a list of messages that you want to process.

Exchange is a message router for queues. (for example, you can have multiple queue lists for the same exchange).

The producer sends messages for exchange.



The consumer reads messages from the queue.

Typically you have one producer and many consumers to process messages in parallel.

The code you posted demonstrates a manufacturer posting to trade.

+6


source


RabbitMQBundle expects you to have an in-depth knowledge of the broker's internals. This is not always what you want.

There is a solution that hides all of these details, leaving a simple yet powerful interface for you. The document is short . If you follow along, you will end up with a working solution with zero knowledge of how RabbitMQ works.

PS Here's a blog post on how to migrate from RabbitMQBundle to EnqueueBundle .

0


source







All Articles