简体   繁体   English

如何在一个项目中使用两个或多个队列驱动程序laravel v4.2

[英]How to use two or more queue driver in one project laravel v4.2

I am working in laravel v4.2 project and I have different queus tasks that I want to perform. 我正在laravel v4.2项目中工作,我有不同的队列任务要执行。 But now I want to perform this task by using two or more drivers fot this. 但是现在我想通过使用两个或多个驱动程序来执行此任务。 For example I have an queue to send registration email and now I want to send email using redis server. 例如,我有一个队列发送注册电子邮件,现在我想使用Redis服务器发送电子邮件。

Second queue I have is to send push notifications to users for this I want to use database drive. 我要使用的第二个队列是向我要使用数据库驱动器的用户发送推送通知。 So is it possible that use two or more queue drivers on one project. 因此有可能在一个项目上使用两个或多个队列驱动程序。

Please educate me. 请教育我。

Thank you 谢谢

Just write another QueueManager extend it from the Base QueueManager 只需编写另一个QueueManager从Base QueueManager对其进行扩展

Extend Your Base Queue Driver 扩展您的基本队列驱动程序

use Illuminate\Queue\Connectors\RedisConnector;
use Illuminate\Queue\QueueManager;

class RedisQueueManager extends QueueManager
{
    public function __construct(\Illuminate\Foundation\Application $app)
    {
        parent::__construct($app);

        $this->registerRedisConnector();
    }

    /**
     * Get the name of the default queue connection.
     *
     * @return string
     */
    public function getDefaultDriver()
    {
        return 'redis';
    }

    protected function registerRedisConnector()
    {
        $app = $this->app;

        $this->addConnector('redis', function () use ($app) {
            return new RedisConnector($app['redis']);
        });
    }
}

Now create a service provider to access it through app 现在创建一个服务提供商以通过应用程序访问它

    <?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Extensions\RedisQueueManager;

class RedisQueueServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bindShared('redis-queue', function ($app) {
            return new RedisQueueManager($app);
        });
    }
}

Finally Register Your Service provider in config/app.php providers array. 最后,在config/app.php provider数组中注册您的Service提供商。

And use it. 并使用它。

Route::get('/', function () {

    $default = app('queue');

    $redis = app('redis-queue');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM