Laravel Use different queue connection to dispatch Jobs

11,189

Solution 1

This worked for me.

//code to be used in the controller (taken from @jedrzej.kurylo above)
$job = (new SendReminderEmail($user))->onQueue('emails');
$this->dispatch($job);

I think this dispatches the job on to the queue named "emails". To execute the job dispatched on 'emails' queue:

//Run this command in a new terminal window
php artisan queue:listen --queue=emails

Solution 2

In order to specify the queue you need to call onQueue() method on your job object, e.g.:

$job = (new SendReminderEmail($user))->onQueue('emails');
$this->dispatch($job);

If you want to send the job to a connection other than default, you need to do fetch connection manually and send the job there:

$connection = Queue::connection('connection_name');
$connection->pushOn('queue_name', $job)

Solution 3

I'd suggest this:

app('queue')->connection('connection_name')->pushOn('queue_name', $job);

From here: In Laravel how to create a queue object and set their connection without Facade

Share:
11,189
Sameer Sheikh
Author by

Sameer Sheikh

Updated on July 28, 2022

Comments

  • Sameer Sheikh
    Sameer Sheikh almost 2 years

    I am using laravel 5.1 and i am using the dispatch method to push the job onto the queue. But there are two kind of jobs and i have created and two queues for that in sqs. How should i achieve this?

  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    What does it mean "it doesn't work"? This is how the official docs say it should be done laravel.com/docs/5.1/queues#pushing-jobs-onto-the-queue
  • Sameer Sheikh
    Sameer Sheikh over 8 years
    "This does not push jobs to different queue "connections" as defined by your queue configuration file, but only to specific queues within a single connection. " This is what the document says
  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    Ah, so you want to change both queue and connection?
  • Sameer Sheikh
    Sameer Sheikh over 8 years
    either change the queue or the connection..any thing would do
  • jedrzej.kurylo
    jedrzej.kurylo over 8 years
    I've updated the answer. I checked how sending to a connection/queue is implemented in Laravel, it seems that above should be enough. Please let me know if there are any issues, I haven't had a chance to test it
  • markashworth
    markashworth over 6 years
    I'd suggest this: app('queue')->connection('connection_name')->pushOn('queue_n‌​ame', $job);
  • agent47
    agent47 over 5 years
    sorry haven't tested it