Laravel uses a queue to send messages.

Source: Internet
Author: User

Laravel uses a queue to send messages.

Preface

This article mainly introduces the content related to sending emails to queues in Laravel for your reference. I will not talk about the content below. Let's take a look at the details:

Batch processing tasks are often used in our development, such as mass mailing, message notifications, text messages, and Flash sales. We need to put this time-consuming operation in the queue for processing, this greatly shortens Web requests and the corresponding time. The following describes how to use queues in Laravel.

1. configuration file config/queue. php

<?phpreturn [ 'default' => env('QUEUE_DRIVER', 'sync'), 'connections' => [ 'sync' => [  'driver' => 'sync', ], 'database' => [  'driver' => 'database',  'table' => 'jobs',  'queue' => 'default',  'retry_after' => 90, ], 'beanstalkd' => [  'driver' => 'beanstalkd',  'host' => 'localhost',  'queue' => 'default',  'retry_after' => 90, ], 'sqs' => [  'driver' => 'sqs',  'key' => 'your-public-key',  'secret' => 'your-secret-key',  'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-account-id',  'queue' => 'your-queue-name',  'region' => 'us-east-1', ], 'redis' => [  'driver' => 'redis',  'connection' => 'default',  'queue' => 'default',  'retry_after' => 90, ], ], 'failed' => [ 'database' => env('DB_CONNECTION', 'mysql'), 'table' => 'failed_jobs', ],];

By default, the configuration file uses the synchronization drive sync. You can find the configurations of each queue driver in this file, including databases, Beanstalkd, Amazon SQS, and Redis. It also contains a null queue driver for tasks that discard the queue. The failed configuration item is used to configure the database and data table for storing failed queue tasks. Next we need to create a queue task class. For more information about the configuration, see queue driver configuration.

2. Create a queue task class. A SendEmail. php file will be generated in the app/Jobs directory.

php artisan make:job SendEmail
<? Phpnamespace App \ Jobs; use App \ User; use Illuminate \ Bus \ Queueable; use Illuminate \ Queue \ SerializesModels; use Illuminate \ Queue; use Illuminate \ Contracts \ Queue; use Illuminate \ Foundation \ Bus \ Dispatchable; use Illuminate \ Support \ Facades \ Mail; class SendEmail implements ShouldQueue {use Dispatchable, disable, Queueable, SerializesModels; protected $ user; /*** Create a new job instance. ** @ return void */public function _ construct (User $ user) {$ this-> user = $ user ;} /*** execution queue method, such as sending an email ** @ return void */public function handle () {$ user = $ this-> user; Mail :: raw ('Enter the mail content', function ($ message) {// sender (your own mailbox and name) $ message-> from ('your _ email@163.com ', 'yourname'); // the recipient's email address $ message-> to ($ this-> user ); // subject $ message-> subject ('send message in queue ');});}}

After the task class is created, add the data to the queue in the controller.

3. Create a message sending controller and use the dispatch method to manually distribute tasks.

<?phpnamespace App\Http\Controllers;use App\Jobs\SendEmail;use App\User;class MessageController extends Controller{ public function index() { $user = User::find(1); $this->dispatch(new SendEmail($user)); }}

4. Access the browser and run the project to push the task to the queue. Then run the Artisan command to execute the tasks in the queue.

Php artisan queue:

  • Queue: by default, work executes only one queue request. It is terminated after the request is executed;
  • Queue: listen listens to queue requests. As long as the queue is running, the request will be accepted until it is manually terminated;
  • Queue: work -- daemon is the same as listen. The difference is that work does not need to load the Framework again and runs the task directly. It is generally recommended to process queue listening.

Note:When you use queue: work -- daemon to update the code, you must stop it and restart it to apply the modified Code.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.