A method for using Message Queue queues and asynchronous queues in the Laravel framework of PHP _php instance

Source: Internet
Author: User
Tags message queue php class

Queue configuration

First, explain how I used the queue in my previous project.

Our current projects are all used in symfony, older projects with symfony1.4, new point projects are used Symfony2. Symfony Use the overall feeling is still very cool, especially symfony2, the overall use of a lot of Java inside the framework of the design idea. But he doesn't support queue. In Symfony, we also went through several processes using queue. The first use of Zhang Yi classmate 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 and research found that there are Apache and updated MQ, that is Apollo. Finally we decided to use the Apollo.

Queue's main scenario in our project is to asynchronously process some of the more time-consuming features, such as synchronizing third party data, changing data, synchronizing notifications to our third party data users, and so on. Our general idea is this, in each controller, if you need to handle asynchronously, a JSON object encode, into Apollo. Write a work command, parse the JSON object in this command, and invoke a different method of processing according to the action and parameters inside it. Running the command on a different machine at the same time as the daemon is running, and it is also a scheme to implement asynchronous multitasking applications. Just keep using it until you find Laravel. Going to look into it. It is not impossible if it is possible to replace it. Oh.

Because only then began to study, certainly directly on Laravel5. Routes, controller, view are basically and symfony difference, it is not difficult to start. Finally, look at 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 stands for 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

The task of push100 a queue in the controller action.

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

5, create the command of the queue

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;
}

Re-modified 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. Monitor Queue

PHP Artisan Queue:listen

To verify multitasking, we open three windows and run the same command.

8, with the Laravel built in the server to start the service

PHP Artisan serve--port 8080 

Open the browser to access the http://localhost:8080/page. Of course, you can use Nginx,apache and the like. But the need for a variety of configurations, or built-in use of convenience.

In the console you can see the execution of each queue, as shown in the following figure. You can see that 100 tasks are equally divided by three work.

To this, basically achieved the effect I want. Verify that the laravel can simply implement the queue and can be multitasking.

The code generated by the make Command has the use App\commands\command, but the runtime hints that the file is not. Solution, modified for 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 the queue in the controller action, there is no asynchronous execution, or it executes in the action script. Discovery is a configuration problem, not only to modify the queue.php in config, but also to modify the related configuration in EVN. Although the problem was solved, but still feel the egg pain, can not understand. Also need to learn laravel in learning.

Asynchronous Queue use method

1. Configure

The definition of queues is not covered here. There are two keys to using asynchronous queues:

(1) Where the queue is stored
(2) Services to perform tasks
Open config/queue.php, which is the Laravel5 configuration file for queues. First we can specify the default queue driver through the default parameter, which is sync, the sync queue, and the first thing to do is to change the asynchronous queue. Suppose we use the database as a driver, the queue task will be stored in the databases, and we will start a different background service to handle the queue task, which 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 command to generate data migration in the artisan commands, only need two commands, of course, you have to implement configuration database connection.

PHP artisan queue:table
php artisan Migrate

This automatically creates the jobs table in the database.

2. Start Queue Listening Service

Start the queue listening service by following this instruction, which automatically handles queue tasks in the jobs table:

PHP Artisan Queue:listen

In Linux, if you want it to run in the background, you can do this:

Nohup PHP Artisan Queue:listen &

3. Add Queue Task

About the queue task to add, the manual said in more detail, here is a simple example.

First, create a queue command by 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 a controller, you can distribute tasks simply by 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, which is handled by the Queue listener service.

More detailed usage suggestions refer to command bus and queue related manual chapters.

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.