Overview
Queue concept: Slightly, not knowing the self solution. (PS: here Laravel version:5.1)
This is often the case in App server development:
A large task can be segmented into 1,2,3,4 tasks, where 2 of the completion relies on the completion of 1,4 dependent on 2. Then 1, 2, 4 can be understood as a transaction that must be executed sequentially or the task cannot be completed. But task 3, with 1, 2, 4 has no relationship, can be completed alone, who first and then have no impact. Specifically, the following tasks are the same as three attributes: Message push, mail delivery, and so on. understanding of Message queues
For this piece, it belongs to the typical producer/consumer model. A producer program makes a task, puts it in a queue, and then the consumer program checks the queue, discovers the task, and consumes it. Theoretical understanding of the transparent. Take a look at the actual operation. Message Queuing implemented by Laravel
Official document: Laravel's queue 1. Realization of producer
Through the model of Message Queuing, we know that there should be a producer, so how to construct this producer in Laravel. Very simple.
PHP Artisan Make:command pushmessage–queued
When you're done, you can see app\commands\pushmessage.php in the app directory, open the file, and see that it integrates a Command class, but we don't have this class here ( If you use the 5.0 version, then it exists, 5.1 is not. Don't be nervous, you're creating a new app\commands\command.php code that reads as follows:
<?php
namespace App\commands;
Abstract class Command
{
//
}
Ok. And then go back to app\commands\pushmessage.php this file, which has a method of handle, and we do this here to demonstrate, using the write file operation. In which you write the following
File_put_contents (' d:/webapp/test.txt ', ' Hello world! ');
Here, the producer will be finished, find a controller. Push the producer's content into the queue. I'm here to demonstrate that it's my own call in app\http\controllers\testcontroller.php. or paste the following source code bar:
<?php
namespace 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, new Pushmessage ()); Push Queue return
view (' Welcome ');
}
Here are two points that need to be noted:
① Here we use the queue façade, but the bus façade is the official recommendation. The effect is the same, you weigh it yourself.
② Here I've added a 60s to the task in order to view the effect. 2. Where the queue data table is stored
The light produced is not good, there must be a place to save these products. In order to be simple here, I use the database method. Other ways you can try for yourself, it's easy. Using a database requires the following configuration:
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. Configure by config/app.php
Execute the following two commands:
PHP Artisan queue:table
PHP Artisan Migrate
The meaning of the command is to create a queue table in the database to hold the task.
OK, save the task's list page. The next step is to consume. It's very simple. 3. Consumers of Queues
The concept of the consumer, corresponding to the official document is the dynamic Queue monitoring service , when it monitors the task to be carried out, it will automatically follow the specified conditions.
the queue service for PHP artisan queue:listen Test
Now is the time to test the results. Take my local environment for example. Run the program first, resulting in several tasks:
Open in Browser: http://127.0.0.1/laravel/public/index.php
When you open the database at this time, you can see the following:
Then watch DOS running the listener, and when you see the following, the program is finished:
This time, you go to the corresponding directory, you will see: test.txt this file.
Well, for more in-depth information, please refer to the official documentation. Change is ready to write down the laravel façade, a concept that I understood for a long time.
Laravel5 Topic directory