Methods for using message queue and asynchronous queue in the Laravel framework of PHP

Source: Internet
Author: User
Queue configuration first describes how to use queue in my previous project. Our current projects all use symfony, the older projects use symfony1.4, and the new projects use symfony2. Symfony is still very nice to use, especially symfony2. In general, many java framework design ideas are used. However

Queue configuration first describes how to use queue in my previous project. Our current projects all use symfony, the older projects use symfony1.4, and the new projects use symfony2. Symfony is still very nice to use, especially symfony2. In general, many java framework design ideas are used. However

Queue Configuration

First, explain how to use queue in my previous project.

Our current projects all use symfony, the older projects use symfony1.4, and the new projects use symfony2. Symfony is still very nice to use, especially symfony2. In general, many java framework design ideas are used. However, it does not support queue. In symfony, we also experienced several processes using queue. At first, Zhang's httpsqs was used. This is simple to use, but there is a single point. After all, our project is officially available for external services, so we studied ActiveMQ, an open-source project under Apache, and found that there is also an updated MQ under Apache, that is, Apollo. Finally, we decided to use the Apollo.

The main application scenario of queue in our project is to process some time-consuming functions asynchronously, such as synchronizing third-party data and notifying our third-party data users when data changes. The general idea is this. If asynchronous processing is required in each controller, encode a json object and plug it into Apollo. Write a work Command, parse the json object in the Command, and call different methods based on the actions and parameters in the Command. Running Command on different machines at the same time as the daemon process is always running according to business needs, which is also a solution for implementing asynchronous multi-task processing applications. This is the case until laravel is found. I plan to study it. If possible, it is not impossible. Haha.

Since learning started, of course, go directly to laravel5. Routes, controller, and view are basically different from symfony, but it is not difficult to get started. Finally, let's take a look at queue.

1. Installing laravle and using composer is simple.

composer global require "laravel/installer=~1.1"vi ~/.bash_profile

Change ~ /. Composer/vendor/bin is added to the environment variable.

source ~/.bash_profile

You can use laravel directly in the command line. Try it.

laravel -V

If you can see the following, it means the operation is successful.

Laravel Installer version 1.2.1

2. Create a project.

laravel new guagua

3. Configure redis and queue.

4. Create a controller,

php artisan make:controller DefaultController

Push100 queue tasks in the controller action.

for($i = 0; $i < 100; $i ++) {  Queue::push(new SendEmail("ssss".$i));}

5. Create the queue Command

php artisan make:command SendEmail --queued

Modify app/Commands/SendEmail. php and add a private variable.

protected $msg;

Modify the constructor at the same time.

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

Then modify the handle method.

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

6. Modify routes

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

7. Listen to the queue

php artisan queue:listen

To verify multi-task processing, we can run the same command in three windows at the same time.

8. Use laravel's built-in server to start the service.

php artisan serve --port 8080 

Open your browser and access the http: // localhost: 8080/page. Of course, nginx and apache can also be used. However, various configurations are required for built-in ease of use.

You can see the execution of each queue in the console, as shown in. We can see that 100 tasks are evenly divided by three jobs.

At this point, I basically achieved the desired effect. It demonstrates that laravel can simply implement queue and perform multi-task processing.

Use App \ Commands \ command in the code generated by make Command, but the file is not prompted during running. Solution: change it to use Illuminate \ Console \ Command. I don't know why this low-level problem occurs. Is it my mac system or my character problem.
When pushing a queue in the action of the controller, the queue is not asynchronously executed, but is still executed in the action Script. It was found that it was a configuration problem. In the past, we not only had to modify queue. php In config, but also related configurations in. evn. Although the problem has been solved, I still feel that it hurts and cannot be understood. You also need to learn laravel.

Asynchronous queue usage

1. Configuration

The definition of the queue is not described here. To use Asynchronous queues, there are two key points:

(1) Storage queue location
(2) services for executing tasks
Open config/queue. php, which is the queue configuration file of laravel5. First, we can use the default parameter to specify the default queue driver. The default configuration is sync. This is a synchronous queue. We need to change the asynchronous queue first. If we use database as the driver, the queue task will be stored in the database, and we will start another backend service to process the queue task, which is asynchronous.

'default' => 'database'

After the configuration is modified, we need to create a table to store queue tasks. Laravel5 has a built-in command in the artisan command to generate data migration. Only two commands are required, of course, you have to configure the database connection.

php artisan queue:tablephp artisan migrate

In this way, the jobs table is automatically created in the database.

2. Start the queue listening service

Run the following command to start the queue listening service. It will automatically process the 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. Add a queue task

The Manual describes how to add a queue task. Here is a simple example.

First, use artisan to create a queue command:

php artisan make:command SendEmail --queued

This will generate the class file app/Commands/SendEmail. php, which will be identified as a queue command. You can write your own business logic in the handle method.

In the controller, you can simply distribute tasks through Bus: dispatch:

Bus::dispatch(new \App\Commands\SendEmail());

You will find that the task is not executed immediately, but is put into the jobs table and processed by the queue listening service.

For more detailed usage instructions, see the command bus and queue manuals.

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.