Laravel 3: laravel queue usage tutorial
Overview
Concept of queue: a solution that is unknown. (PS: laravel version: 5.1)
This situation is often encountered during app server development:
A large task can be divided into 1, 2, and 3 small tasks. 2 of them depends on 1 and 4 of them. Therefore, 1, 2, and 4 can be considered as a transaction and must be executed in sequence. Otherwise, the task cannot be completed. However, task 3 has nothing to do with task 1, task 2, and Task 4. Task 3 can be completed independently. Specifically, the following tasks have three attributes: message pushing, email sending, and so on.
Message Queue
This is a typical producer/consumer model. A producer program creates a task and puts it in the queue. then the consumer program checks the queue and consumes the task when it finds it. Theoretically, it is transparent. To see the actual operation.
Message queue implemented by laravel
Official documentation: laravel queue
1. implement producer
Through the message queue model, we know that there should be a producer. in laravel, how should we construct this producer? Very simple.
Php artisan make: command PushMessage? Queued
After execution, you can see app \ Commands \ PushMessage in the app directory. php: open this file and you will see that it integrates a Command class. However, we do not have this class here. (if you are using version 5.0, it exists, 5.1 ). Do not be nervous. the code for creating an app \ Commands \ Command. php is as follows:
OK !, Return to the app \ Commands \ PushMessage. php file again, and there is a handle method. here we will use the file write operation for demonstration. Write the following content
file_put_contents('D:/webApp/test.txt', 'hello world!');
At this point, the producer has finished writing and finds a controller. Push the content generated by the producer into the queue. Here, I will demonstrate how to call it in app \ Http \ Controllers \ TestController. php. Paste the following source code:
Note the following two points:
① Here we use Queue Facade, but the official recommendation for using Bus Facade is the same. you can weigh the two by yourself.
② Here I specially added a 60 s to execute the tasks in the queue to view the effect.
2. where the queue data table is savedIf production is not successful, you have to save these products. For the sake of simplicity, I will use the database method. You can try it on your own in other ways. To use a database, perform the following configurations:
Open config/queue. php and change the default queue driver to a database:
Default '=> env ('queue _ DRIVER', 'database ')
Configure your database connection service. This will not be said. Use config/app. php to configure
Run the following two commands:
Php artisan queue: table
Php artisan migrate
The meaning of the command is to create a team list in the database to save the task.
Okay. Save the task list page. The next step is consumption. This is simple.
3. queue consumer
This consumer concept corresponds to the official documentDynamic queue listening serviceWhen it monitors the tasks to be executed, it will automatically execute according to the specified conditions.
Php artisan queue: listen
Tested Queue Service Now is the time to test the results. Take my local environment as an example. First, run the program to generate several tasks:
Open http: // 127.0.0.1/laravel/public/index. php in the browser
When you open the database, you can see the following:
Then observe the dos of the listening program. when the following figure is displayed, the program has been executed:
At this time, you can see the file test.txt in the corresponding directory.
Okay. For more information, see the official documentation. Change the preparation to write down laravel's Facade, which makes me understand the concept for a long time.