承接 symfony-bundles/event-queue-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

symfony-bundles/event-queue-bundle

最新稳定版本:v2.0.1

Composer 安装命令:

composer require symfony-bundles/event-queue-bundle

包简介

Symfony EventQueue Bundle

README 文档

README

SensioLabsInsight

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Latest Stable Version License

Installation

  • Require the bundle with composer:
composer require symfony-bundles/event-queue-bundle
  • Enable the bundle in the kernel:
public function registerBundles()
{
    $bundles = [
        // ...
        new SymfonyBundles\EventQueueBundle\SymfonyBundlesEventQueueBundle(),
        // ...
    ];
    ...
}
  • Configure the EventQueue bundle in your config.yml.

Defaults configuration:

sb_event_queue:
    service_name: 'event_queue'
    default_name: 'event:default'
    storage_path: '%kernel.cache_dir%/event-queue-daemon.%s.pid'

How to use

Add an event to the queue:

$dispatcher = $this->get('sb_event_queue');

$dispatcher->on(MyEvent::class, date('Y-m-d H:i:s'), 'Example message');

Your event class must implement SymfonyBundles\EventQueueBundle\EventInterface (or extending SymfonyBundles\EventQueueBundle\Event class) and having constant NAME with the event name. For example:

namespace AppBundle\Event;

use SymfonyBundles\EventQueueBundle\Event;

class MyEvent extends Event
{
    const NAME = 'event.example';

    private $time;
    private $message;

    public function __construct($time, $message)
    {
        $this->time = $time;
        $this->message = $message;
    }

    public function getTime()
    {
        return $this->time;
    }

    public function getMessage()
    {
        return $this->message;
    }
}

Creating an Event Listener. The most common way to listen to an event is to register an event listener:

namespace AppBundle\EventListener;

use AppBundle\Event\MyEvent;

class MyListener
{
    public function onEventExample(MyEvent $event)
    {
        $event->getTime();
        $event->getMessage();

        // and we are doing something...
    }
}

Now that the class is created, you just need to register it as a service and notify Symfony that it is a "listener":

services:
    app.my_listener:
        class: AppBundle\EventListener\MyListener
        tags:
            - { name: kernel.event_listener, event: event.example }

Execution (dispatching) of all events from the queue:

while ($dispatcher->count()) {
    $dispatcher->dispatch();
}

You can separate the events by section, specifying in event manager the needed section.

$dispatcher->setName('email.notifications');

$dispatcher->on(EmailNotifyEvent::class, 'Subject', 'Message Body', ['john@domain.com', 'alex@domain.com']);
$dispatcher->on(EmailNotifyEvent::class, 'Another subject', 'Another message Body', ['demo@domain.com']);

$dispatcher->setName('database.reindex');

$dispatcher->on(DatabaseReindexEvent::class, 'users');
$dispatcher->on(DatabaseReindexEvent::class, 'orders');
$dispatcher->on(DatabaseReindexEvent::class, 'products');

In this example, will be executed only an events from the section email.notifications:

$dispatcher->setName('email.notifications');

while ($dispatcher->count()) {
    $dispatcher->dispatch();
}

Console commands:

  • event:queue:dispatch
  • event:queue:daemon:start
  • event:queue:daemon:stop

In what situations is useful to apply the queue of events:

  • When sending email messages
  • Parsing websites
  • and in other cases, when the execution time of process is very long, and the response from the server must be returned immediately.

统计信息

  • 总下载量: 12.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 26
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 25
  • Watchers: 6
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固