magium/messaging

用于与通用消息平台的接口

0.1.2 2017-03-01 16:36 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:41:29 UTC


README

这个库只包含接口,没有功能。它的目的是允许PHP开发者构建基于消息的应用程序,而不必承诺特定的消息平台。平台库需要实现实际的功能。这个库的唯一目的是在库之间定义一致的用法。

虽然它大量借鉴了JMS标准,但体积要小得多。

你将如何使用它?

作为消费者

class DoSomeConsuming implements \Magium\Messaging\ExceptionListenerInterface
{

    protected $connectionFactory;

    public function __construct(
        \Magium\Messaging\ConnectionFactoryInterface $factory
    )
    {
        $this->connectionFactory = $factory;
    }

    public function onException(\Magium\Messaging\MessagingException $e)
    {
        echo 'Oh well ¯\_(ツ)_/¯ : ' . $e->getMessage();
        die();
    }

    public function run()
    {
        $connection = $this->connectionFactory->createConnection();
        $connection->setExceptionListener($this);

        $session = $connection->createSession();
        $destination = $session->createQueue('some queue');

        $consumer = $session->createConsumer($destination);

        $message = $consumer->receive();

        echo $message->getText();
    }

}

当你创建这个类的实例时,你需要提供你的消息库的工厂。从那时起,它应该完全一样工作。

作为生产者

<?php

class DoSomeproducing implements \Magium\Messaging\ExceptionListenerInterface
{

    protected $connectionFactory;

    public function __construct(
        \Magium\Messaging\ConnectionFactoryInterface $factory
    )
    {
        $this->connectionFactory = $factory;
    }

    public function onException(\Magium\Messaging\MessagingException $e)
    {
        echo 'Oh well ¯\_(ツ)_/¯ : ' . $e->getMessage();
        die();
    }

    public function run()
    {
        $connection = $this->connectionFactory->createConnection();
        $connection->setExceptionListener($this);

        $session = $connection->createSession();
        $destination = $session->createQueue('some queue');

        $producer = $session->createProducer($destination);

        $message = $session->createTextMessage('This is some text');

        $producer->send($message);
    }

}