laravel ÐÐÁÐÔõÑùʹÓÃ
Ëæ×Å»¥ÁªÍøÐÐÒµµÄÒ»Ö±Éú³¤£¬ÎÒÃǵÄÍøÕ¾Ô½À´Ô½Öش󣬻á¼ûÁ¿Ò²Ô½À´Ô½´ó¡£Ôڸ߲¢·¢µÄÇéÐÎÏ£¬´¦Àí´ó×ڵĺÄʱʹÃüÊÇÒ»ÏîºÜÊǼ¬ÊÖµÄʹÃü£¬»áµ¼ÖÂÎÒÃǵÄÓªÒµ·ºÆ𿨶١¢¹£ÈûµÄÇéÐΡ£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬laravelÌṩÁËÐÐÁй¦Ð§£¬¿ÉÓÃÀ´´¦ÀíһЩÒì²½¿ÉÑÓ³ÙµÄʹÃü£¬ÈÃÓû§²»»á¸ÐÊܵ½Í£Áô¡£½ÓÏÂÀ´£¬ÎÒÃǾÍÀ´ÏàʶlaravelÐÐÁÐÔõÑùʹÓá£
Ò»¡¢×°ÖÃÐÐÁÐ
1.×°ÖÃÐÐÁаü
ÔÚLaravelÖУ¬ÐÐÁб»°²ÅÅÔÚLaravelÐÐÁÐÊÂÇé°üÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃComposerÀ´×°Öøðü£º
composer require illuminate/queue
2.Ìí¼ÓÐÐÁÐÌṩ³ÌÐò
LaravelÖ§³Ö¶à¸öÐÐÁкó¶Ë£¬°üÀ¨Êý¾Ý¿â¡¢Redis¡¢BeanstalkdµÈ¡£ÎÒÃÇÐèÒªÔÚconfig/queue.phpÖÐÉèÖÃÐÐÁÐÌṩ³ÌÐò¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃRedisÐÐÁÐÌṩ³ÌÐò£º
‘connections’ => [
'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => 'default', 'retry_after' => 90, 'block_for' => null, ],
µÇ¼ºó¸´ÖÆ
],
¶þ¡¢½¨ÉèʹÃü
ÐÐÁб»Éè¼ÆÓÃÀ´´¦Àí³¤Ê±¼äÔËÐеÄʹÃü£¬Õâ¿ÉÒÔÊÇÈκÎʹÃü»òÏÂÁî¡£ÎÒÃÇ¿ÉÒÔʹÓÃÏÂÁîÐÐÀ´ÌìÉúÒ»¸öеÄÐÐÁÐʹÃü£º
php artisan make:job SendEmail
ÌìÉúµÄÐÐÁÐʹÃüÀà±»°²ÅÅÔÚapp/JobsĿ¼Ï£¬ÊÖ¶¯ÌìÉúµÄSendEmailʹÃüÀàÊÇÒ»¸ö»ù´¡À࣬¿´ÆðÀ´ÏñÕâÑù£º
namespace AppJobs;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
class SendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct() { // } /** * Execute the job. * * @return void */ public function handle() { // }
µÇ¼ºó¸´ÖÆ
}
ÎÒÃÇ¿ÉÒÔÔÚhandle()ÒªÁìÖбàдʹÃüµÄÏêϸʵÏÖÂß¼¡£
Èý¡¢ÍÆËÍʹÃüµ½ÐÐÁÐ
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃ$dispatch()¾²Ì¬ÒªÁ콫ʹÃü¼ÓÈëÐÐÁС£ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷¡¢Â·ÓÉ»òÆäËûÈκεط½Ê¹ÓôËÒªÁ죬ÈçÏÂËùʾ£º
use AppJobsSendEmail;
SendEmail::dispatch();
ÎÒÃÇÒ²¿ÉÒÔʹÓÃÐÐÁеÄdelay()ºÍonQueue()ÒªÁìÖ¸¶¨ÑÓ³ÙºÍÐÐÁÐÃû³Æ¡£
ÀýÈ磬½«Ê¹ÃüÍÆË͵½ÃûΪ”emails”µÄÐÐÁУ¬²¢ÔÚ10Ãëºó´¦Àí£º
SendEmail::dispatch()->onQueue(’emails’)->delay(now()->addSeconds(10));
ËÄ¡¢ÔËÐÐÐÐÁÐ
ÎÒÃÇÒѾ½«Ê¹ÃüÍÆË͵½ÐÐÁÐÖУ¬ÏÖÔÚÐèÒªÔËÐÐÐÐÁв¢´¦ÀíʹÃü¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃArtisanÏÂÁîÀ´Æô¶¯ÐÐÁд¦ÀíÆ÷£º
php artisan queue:work
ĬÈÏÇéÐÎÏ£¬ÐÐÁд¦ÀíÆ÷½«Ê¹ÓÃÃûΪ”default”µÄÐÐÁÐÌṩ³ÌÐò¡£ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓÔ–queue”Ñ¡ÏîÀ´ÉèÖô¦ÀíÆ÷´¦ÀíµÄÐÐÁС£ÀýÈ磬´¦ÀíÓʼþÐÐÁУº
php artisan queue:work –queue=emails
ÈôÊÇÄãÏ£ÍûʹÓÃËùÓпÉÓõĴ¦ÀíÆ÷ÂÖÁ÷´¦ÀíÐÐÁУ¬Ôò¿ÉÒÔʹÓÔ–daemon”Ñ¡Ïî¡£ÕâÑù¿ÉÒÔÈ·±£×ÝȻһ¸ö´¦ÀíÆ÷¹Òµô£¬Ò²²»»á×èÖ¹´¦ÀíÆ÷µÄÔËÐС£
php artisan queue:work –daemon
ÉÐÓÐÆäËûÐí¶àÐÐÁд¦ÀíÆ÷µÄÑ¡Ïî¡£Äú¿ÉÒÔʹÓÃphp artisan queue:work –helpÏÂÁîÉó²éËùÓÐÑ¡ÏîºÍÏà¹ØÐÅÏ¢¡£
Îå¡¢×ܽá
ÒÔÉÏÊÇlaravelÐÐÁеÄʹÓð취£¬Ê¹ÓÃlaravelÐÐÁУ¬ÎÒÃÇ¿ÉÒÔÇáËÉ´¦Àí´ó×ÚºÄʱʹÃü£¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄЧÂʺÍÏìÓ¦ËÙÂÊ¡£ÈôÊÇÄúÏ£ÍûÉîÈëÏàʶÐÐÁеĸü¶àÄÚÈݺÍÑ¡ÏÇëÉó²éLaravel¹Ù·½Îĵµ¡£
ÒÔÉϾÍÊÇlaravel ÐÐÁÐÔõÑùʹÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡