Laravel 3: laravel queue usage tutorial, laravel queue
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 it in the app directory.app\Commands\PushMessage.php
Open the file and you will see that it integratesCommand
This class, however, we do not have this class here (if you are using version 5.0, it exists, and version 5.1 will not exist ). Do not be nervous. Create a Newapp\Commands\Command.php
The Code is as follows:
<?phpnamespace App\Commands;abstract class Command{ //}
OK !, Then returnapp\Commands\PushMessage.php
This file contains a methodhandle
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. I am here to demonstrate that you areapp\Http\Controllers\TestController.php
. Paste the following source code:
<? Phpnamespace App \ Http \ Controllers; use Illuminate \ Http \ Request; use App \ Http \ Requests; use App \ Http \ Controllers \ Controller; use Queue; use App \ Commands \ PushMessage; class TestController extends Controller {/*** Display a listing of the resource. ** @ return Response */public function index () {Queue: later (60, new PushMessage (); // push the Queue return view ('Welcome ');}}
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 saved
If 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:
Openconfig/queue.php
To change the default queue driver to a database:
Default '=> env ('queue _ DRIVER', 'database ')
Configure your database connection service. This will not be said. Passconfig/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 go to the corresponding directory and you will see:test.txt
This file is now available.
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.
Reprinted please indicate the source (it is estimated that this sentence is not usable)