Lavarel异步队列的使用

系统为window

启动队列:

php artisan queue:listen

设置队列类
.env文件需设置:QUEUE_CONNECTION=redis



namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

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

    protected $email;

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

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::info($this->email);
    }

}

使用

use App\Jobs\SendEmail;
class TestController extends Controller
{
    public function test() {
        Log::info('开始');
        $t = now()->addSeconds(10);//设置延迟时间
        SendEmail::dispatch('@qq.com')->delay($t);
        Log::info('结束');
    }
}

你可能感兴趣的:(php,lavarel,php)