Laravel event (event) + queue (queue) basics

1. In the EventServiceProviders.php file under the Providers directory, manually add the red framed code.

'App\Events\Demo' => [
            'App\Listeners\DemoListener',
        ],

 2. Execute in the root directory of the project

php artisan event:generate

At this time, there will be two more directories in the project, Events and Listeners. And will create two files by itself, Demo.php and DemoListener.php as shown below

3. Contents in the Demo.php file:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class Demo
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $data;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

 4. Contents in the DemoListeners.php file:

<?php

namespace App\Listeners;

use App\Events\Demo;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class DemoListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\Demo  $event
     * @return void
     */
    public function handle(Demo $event)
    {
        $data = $event->data;
        file_put_contents("../logs.txt",json_encode($data).PHP_EOL,FILE_APPEND);
    }
}

5. In the controller, write the data of the test event as follows

public function index()
    {
        $arr = [1,2,3,'laravel event'];
        event(new Demo($arr));
        echo "事件执行完毕";
    }

6. Visit the controller to see if the event is executed successfully. As a result, a logs.txt file will appear in the root directory of the project, as shown in the figure below:

The above is the simple and practical laravel event, for reference only 

The next step is to learn the use of event+queue.

1. DemoListener inherits (implements) ShouldQueue. On the basis of the above, it just adds the code circled in red. as the picture shows:

<?php

namespace App\Listeners;

use App\Events\Demo;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\InteractsWithQueue;

class DemoListener implements ShouldQueue
{
    public $queue = 'demo';

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\Demo  $event
     * @return void
     */
    public function handle(Demo $event)
    {
        $data = $event->data;
        Log::info(json_encode($data).PHP_EOL);
        //file_put_contents("../logs.txt",json_encode($data).PHP_EOL,FILE_APPEND);
    }
}

 

2. Modify the queue.php file in the config directory. The main location is as shown in the figure: you can modify it according to your own needs. 

3. Visit the controller. This time I was surprised to find that the logs.txt file was not found in the project root directory. This is because, this time, it is in the form of event + queue. The access controller just puts the event into the redis queue, but it has not been executed yet. The following picture is: the content of the event in redis

 Notice:

1. The project needs to change the queue driver from sync to redis in the .env file generated by laravel

QUEUE_CONNECTION=redis

2. To use the redis asynchronous driver, you need to install the predis/predis package

composer requires predis/predis

3. Replace the above logs.txt file with the log file that comes with the framework

4. There are already events in the queue, and the queue will be executed below. as the picture shows:

php artisan queue:listen --queue=demo

 Finally, the log file will record information

 

Summarize:

The difference between the separate use of event + queue and event is whether the monitored event class inherits from ShouldQueue. If there is no inheritance, it is a separate event, and inheritance is an event + queue.
Separate event: When the program triggers the event, the event will be executed immediately.
Event + Queue: When the program triggers an event, the event will first be distributed to the queue. Wait for the queue to execute sequentially.

Guess you like

Origin blog.csdn.net/u012322399/article/details/127654821