Laravel Learning three: Laravel Queue usage Tutorial

Source: Internet
Author: User

Overview

Queue concept: Slightly, do not know the self-resolution. (PS: here Laravel version:5.1)
This is often the case with App server development:

A large task can be divided into 1,2,3,4 small tasks, where 2 of the completion depends on the completion of 1,4 2. Then 1, 2, 4 can be understood as a transaction that must be executed sequentially, otherwise the task cannot be completed. But task 3, with 1, 2, 4 has no relationship, can be completed alone, who first who has no effect. Specifically, the following tasks are followed by three is a property: Message push, mail send and so on.

understanding of Message Queuing

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. The theoretical understanding is transparent. Take a look at the actual operation.

Message Queuing implemented by Laravel

Official document: Laravel's queue

1. Implementing producers

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

After execution, you can see in the app directory app\Commands\PushMessage.php , open this file, you will see that it integrates a Command class, but we do not have this class here (if you are using version 5.0, then it is present, 5.1 is not). Don't be nervous. Create a new app\Commands\Command.php code content as follows:

<?phpnamespaceApp\Commands;abstractclass Command{    //}

Ok! And then go back to app\Commands\PushMessage.php this file again, where there is a method handle that we are doing here for demonstration purposes, using the write file operation. In which the following is written

file_put_contents(‘D:/webApp/test.txt‘‘hello world!‘);

Here, the producers have finished, find a controller. Push the producer-generated content into the queue. I'm here to demonstrate that I'm app\Http\Controllers\TestController.php making the call myself in. or 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 ( -,NewPushmessage ());//Propulsion queue        returnView' Welcome '); }}

Here are two points to note:
① here we are using queue facade, but the official recommendation is to use bus facade. The effect is the same, you weigh it yourself.
② Here I've deliberately added a 60s to perform the tasks in the queue to see the effect.

2. Where the queue data table is saved

The production of light does not work, there must be a place to save these products. Here for the sake of simplicity, I will use the database way. Other ways you can try it yourself, it's easy. With the database, the following configuration is required:

    • Open config/queue.php , change the default queue driver to database:

      Default ' = ' env (' queue_driver ', ' database ')

    • Configure your database connection service well. This will not be said. Through config/app.php to configure

    • 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 that is used to save the task.
OK, the list page of the Save task is done. The next step is to spend. This is very simple.

3. Queues of consumers

The concept of the consumer, corresponding to the official document is the dynamic Queue monitoring service , when it hears the task to be performed, it will automatically follow the specified conditions.

PHP Artisan Queue:listen

Testing the Queue service

Now is the time to test the results. Take my local environment for example. Run the program first, generating several tasks:
Open in Browser: http://127.0.0.1/laravel/public/index.php
To open the database at this time, you can see something like this:

Then observe the DOS running the listener, and when you see the following, the program has been executed:

This time, you go to the corresponding directory, you will see: test.txt this file.
Well, for further information, please refer to the official documentation. Change prepared to write down the laravel of the facade, this let me understand for a long time to understand the concept.

Reprint please specify the source (estimated this sentence and no egg)

Laravel Learning three: Laravel Queue usage Tutorial

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.