vyuldashev / laravel-queue-rabbitmq

RabbitMQ driver for Laravel Queue. Supports Laravel Horizon.
MIT License
1.9k stars 376 forks source link

Cannot get the queue data in the exchange #393

Closed ice-matcha closed 3 years ago

ice-matcha commented 3 years ago

Here is my version

"laravel/framework": "^6.20";
"vladimir-yuldashev/laravel-queue-rabbitmq": "^10.2"

"RabbitMQ":3.8.9

Here is my config

'rabbitmq' => [

       'driver' => 'rabbitmq',
       'queue' => env('RABBITMQ_QUEUE', 'default'),
       'connection' => PhpAmqpLib\Connection\AMQPLazyConnection::class,

       'hosts' => [
           [
               'host' => env('RABBITMQ_HOST', '127.0.0.1'),
               'port' => env('RABBITMQ_PORT', 5672),
               'user' => env('RABBITMQ_USER', 'guest'),
               'password' => env('RABBITMQ_PASSWORD', 'guest'),
               'vhost' => env('RABBITMQ_VHOST', '/'),
           ],
       ],

       'options' => [
            'queue' => [
                'exchange' => 'mms',
                'exchange_type' => 'topic',
                'exchange_routing_key' => 'tt',
            ],

            'ssl_options' => [
                'cafile' => env('RABBITMQ_SSL_CAFILE', null),
                'local_cert' => env('RABBITMQ_SSL_LOCALCERT', null),
                'local_key' => env('RABBITMQ_SSL_LOCALKEY', null),
                'verify_peer' => env('RABBITMQ_SSL_VERIFY_PEER', true),
                'passphrase' => env('RABBITMQ_SSL_PASSPHRASE', null),
            ],
       ],

       /**
        * Set to "horizon" if you wish to use Laravel Horizon.
        */
       'worker' => env('RABBITMQ_WORKER', 'default'),
    ],

Here is my code

IndexController:

public function second()
{
    for ($i=1; $i <= 10; $i++) {
        $data = [
            'second_key'=> $i,
            'time'  => date("Y-m-d H:i:s", time())
        ];

        sleep(2);
        SecondRabbitQueue::dispatch($data);
    }
}

SecondRabbitQueue:

class SecondRabbitQueue implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

     protected $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            echo "second\n";
            print_r($this->data);
        }catch(\Exception $e){
            echo json_encode([
                'code'=>0,
                'msg'=>$e->getMessage()
            ]);
        }
    }
}

Question:

When I run the above code, there is nothing to get data

image image image

juliocmartins commented 3 years ago

Same doubt here, any ideas? need something else in queue.php?

"laravel/lumen-framework": 7.0 "vladimir-yuldashev/laravel-queue-rabbitmq": 10.2.3

tks

adm-bome commented 3 years ago

Probably because your jobs dont specify the queue/topic. Your message is going to the default queue or topic. But because there are no endpoints (queue's) the exxhange disgards the message.

You publish agaist an exchange. The published message must contain its destination. Queues must be bound as endpoints to an exchange.

Read also tje manual of RabbitMQ. More wil be clear how rabbitMQ handles things

juliocmartins commented 3 years ago

Thanks @adm-bome for te reply.

I upgraded the package from 7.0 to 10, in 7.0 i was able to create the exchange, queues e de bound between them automatically when the first message was pushed.

So in 10.x version i need to create all of them before push the first message?

adm-bome commented 3 years ago

Yes and no ;) (as always with programming)

The way the lib handles messages has changed allot. Its now more decoupled. The app/lib does not force the existence of queus and exchanges.

For you... It depents on your use-case, but for a topic exchange u most likly will because u devops this from a rabbitmq point of view.

Your application should not controle the messages. Your application just sends the message and does not know in which endpoint/queue your message land's.

Workers just work queues and publishers just publish ;)

If u want some sort of control (from within your app) u should specify commands/crons to specify the exchanges and queues upfront or on startup off your app.

ice-matcha commented 3 years ago

Probably because your jobs dont specify the queue/topic. Your message is going to the default queue or topic. But because there are no endpoints (queue's) the exxhange disgards the message.

You publish agaist an exchange. The published message must contain its destination. Queues must be bound as endpoints to an exchange.

Read also tje manual of RabbitMQ. More wil be clear how rabbitMQ handles things

Thank you for your answer.