PHP's laravel framework uses Message Queuing queue and asynchronous queue methods _php instances

Source: Internet
Author: User
Queue Configuration

Let's start by explaining how I used queue in my previous project.

Our current projects are used symfony, older projects with the symfony1.4, the new point of the project is Symfony2. Symfony with the overall feeling is very cool, especially symfony2, the overall use of a lot of Java inside the framework of the design ideas. But he doesn't support the queue. In Symfony, we also experienced several processes using the queue. The first to use Zhang Yi classmate's Httpsqs. This is simple to use, but there is a single point. After all, our project is officially outside the service, so we studied Apache's Open source project ACTIVEMQ, research found that Apache also has updated MQ, that is, Apollo. Finally we decided to use the Apollo.

Queue in our project the main application scenario is to asynchronously handle some of the more time-consuming functions, such as synchronizing third-party data, data changes the synchronization of notifications to our third-party data users and so on. Our general idea is that, in each controller, if it needs to be processed asynchronously, a JSON object is encode and plugged into Apollo. Write a work command, parse the JSON object in this command, and invoke the different methods according to the action and parameters. Running the command as a daemon on different machines, depending on the business needs, is also a scenario for implementing asynchronous multitasking applications. It was so used until Laravel was discovered. I'm going to look into it. It is not impossible to replace it if possible. Oh.

Because only then began to study, of course directly on Laravel5. Routes, controller, view are basically and symfony difference, it is not difficult to get started. Finally, consider the queue.

1, install Laravle, use composer, it is very simple.

Composer global require "laravel/installer=~1.1" VI ~/.bash_profile

Add the ~/.composer/vendor/bin to the environment variable.

SOURCE ~/.bash_profile

You can use Laravel directly on the command line. Try it.

Laravel-v

If you can see the following, it means success.

Laravel Installer Version 1.2.1

2, create the project.

Laravel New Guagua

3. Configure Redis and queue.

4. Create Controller,

PHP Artisan Make:controller Defaultcontroller

Push100 a queue task in the Controller's action.

for ($i = 0; $i < $i + +) {  Queue::p ush (New SendEmail ("SSSs". $i));}

5. Create Queue command

PHP Artisan Make:command SendEmail--queued

Modify app/commands/sendemail.php to add a private variable.

protected $msg;

Modify the constructor at the same time.

Public function __construct ($msg) {  $this->msg = $msg;}

Handle method of re-modification

Public function handle () {  sleep (4);  echo $this->msg. " \ t ". Date (" Y-m-d h:i:s ")." \ n ";  $this->delete ();}

6, modify the routes

Route::get ('/', [  ' as ' = ' = ' index ',  ' uses ' = ' defaultcontroller@index ']);

7. Monitor Queue

PHP Artisan Queue:listen

To verify multitasking, we run the same command in three windows at the same time.

8, with Laravel built-in server start-up service

Open the browser and visit the http://localhost:8080/page. Of course, you can use Nginx,apache and the like. But it requires a variety of configurations, or built-in ease of use.

In the console you can see the execution of each queue, such as. You can see 100 quests divided by three jobs.

To this, basically achieved the effect I want. It is verified that laravel can implement queue easily and can multitask.

The make Command generates the code in use App\commands\command, but the runtime prompts you not to have the file. Solution, modified to use Illuminate\console\command; I do not know why this low-level problem, it is my Mac system problem, or my character problem.
When you push a queue in the Controller's action, it is not executed asynchronously or in the script of the action. Discovery is a configuration problem, it is not only to modify the config in the queue.php, but also to modify the. EVN in the relevant configuration. Although the problem is solved, but still feel the egg ache, cannot understand. Also need to learn to learn laravel.

How to use asynchronous queues

1. Configuration

There is no introduction to the definition of the queue. We have two keys to using an asynchronous queue:

(1) Where the queue is stored
(2) Service for performing tasks
Open config/queue.php, which is the Laravel5 configuration file for the queue. First we can specify the default queue driver through the default parameter, which is sync, which is the synchronization queue, we have to do the asynchronous queue first change here. Assuming that we use database as the driver, the queue task will be stored in the databases, and we will later start a background service to handle the queue task, this is the asynchronous way.

' Default ' = ' database '

After modifying the configuration, we need to create a table to hold the queue task, LARAVEL5 has built a directive in the artisan command to generate data migration, only need two commands, of course, you have to configure the database connection.

PHP Artisan queue:tablephp Artisan Migrate

This automatically creates the jobs table in the database.

2. Start the Queue monitoring service

Start the queue monitoring service with this command, which automatically handles queue tasks in the jobs table:

PHP Artisan Queue:listen

In Linux, if you want it to be executed in the background, you can:

Nohup PHP Artisan Queue:listen &

3. Adding a Queue task

About the addition of the queue task, the manual said in more detail, here is a simple example of it.

First, create a queue command from artisan:

PHP Artisan Make:command SendEmail--queued

This generates the App/commands/sendemail.php class file, which is identified as a queue command, and you can write your own business logic in the handle method.

In the controller, you can simply distribute the task via Bus::d ispatch:

Bus::d ispatch (New \app\commands\sendemail ());

You will find that the task is not executed immediately, but is placed in the jobs table and handled by the Queue listener service.

For more detailed usage, refer to command bus and queue related manual chapters.

  • Related Article

    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.