braincrafted/mq-bundle

该软件包最新版本(v0.4)没有提供许可证信息。

v0.4 2013-11-16 13:32 UTC

This package is auto-updated.

Last update: 2024-09-20 09:58:50 UTC


README

BraincraftedMqBundle将BcMq包装成Symfony2的一个友好软件包,为您提供PHP实现的消息队列服务器。

作者:Florian Eckerstorfer

安装

BraincraftedMqBundle推荐通过Composer安装。

#composer.json
{
    "require": {
        "braincrafted/mq-bundle": "dev-master"
    }
}

您还需要将其添加到您的AppKernel.php中。

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Braincrafted\Bundle\MqBundle\BraincraftedMqBundle()
    );

    // ...

    return $bundles;
}

使用方法

BraincraftedMqBundle使用服务(而不是回调)来消费消息。您可以在config.yml中配置这些消费者。

app/config/config.yml

braincrafted_mq:
    consumers:
        write_file: acme_demo.consumer.write_file

现在您需要在您的bundle服务配置中定义服务acme_demo.consumer.write_file。您必须自己编写这些消费者。下面提供了一个示例。

<?php
// src/Braincrafted/Bundle/MqDemoBundle/Consumer/WriteFileConsumer.php

namespace Braincrafted\Bundle\MqDemoBundle\Consumer;

class WriteFileConsumer
{
    private $filename;

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

    public function consume($message)
    {
        file_put_contents($this->filename, $message."\n", FILE_APPEND);
    }
}

请注意,在这种情况下,$message是一个字符串,但您也可以发送更复杂的消息,例如数组。任何可以编码为JSON的内容都可以发送。

如果您的消费者已经就绪,您可以启动消息队列服务器并发送消息。

$ php app/console braincrafted:mq:server -p 4000

您还可以通过使用--verbose-v选项以详细模式启动服务器。如果详细模式被激活,所有传入的数据都会在控制台回显。

服务器将把发送到端口4000的消息重定向到消费者。每个消息必须是一个JSON对象,并且必须包含恰好两个值:typemessage。Type是消费者的名称(例如,上面的示例中的write_file)和message是一个字符串或一个数组。

{
    "type": "write_file",
    "message": "Hello World!"
}

复杂消息

{
    "type": "write_file",
    "message": { "text": "Hello World!", "date": "2013-05-29" }
}

如果您想从您的Symfony应用程序发送消息,您可以使用该软件包提供的生产者。

$producer = $container->get('braincrafted_mq.producer');
$producer->produce('write_file', 'Hello World!');

消息也可以是数组

$producer = $container->get('braincrafted_mq.producer');
$producer->produce('write_file', array('text' => 'Hello World!', 'time' => time());

变更日志

版本 0.4(2013-11-16)

  • 更改命名空间为Braincrafted
  • 修复了服务器中空回调的bug

版本 0.3(2013-07-05)

  • braincrafted/mq更新到版本0.3
  • 在详细模式下回显传入数据

版本 0.2(2013-06-04)

  • braincrafted/mq更新到版本0.2(与Symfony 2.3兼容)

版本 0.1.1(2013-06-04)

  • 修复依赖关系

版本 0.1

  • 首次发布

许可证

The MIT License (MIT)

Copyright (c) 2013 Florian Eckerstorfer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Bitdeli Badge