Example #1
0
 public function tearDown()
 {
     if (!$this->channel) {
         return;
     }
     $this->channel->exchangeDelete($this->exchangeName);
     $this->channel->close();
     $this->connection->close();
 }
Example #2
0
 public function testGet()
 {
     $connection = new AmqpConnection();
     $connection->setConfig(['host' => $this->faker->ipv4, 'port' => $this->faker->randomDigit()]);
     $this->assertEquals('AMQPConnection', get_class($connection->get()));
     $amqpConnection = $this->getMockBuilder('AMQPConnection')->setMethods(['isConnected'])->getMock();
     $amqpConnection->expects($this->once())->method('isConnected')->willReturn(false);
     $connection->connection = $amqpConnection;
     $connection->get();
 }
Example #3
0
 /**
  * @param Connection $connection
  * @param null       $channelId
  * @param bool       $autoDecode
  */
 public function __construct($connection, $channelId = null, $autoDecode = true)
 {
     $this->frameBuilder = new FrameBuilder();
     if ($channelId == null) {
         $channelId = $connection->getFreeChannelId();
     }
     parent::__construct($connection, $channelId);
     if ($this->debug) {
         Helper::debugMsg(sprintf('using channel_id: %s', $channelId));
     }
     $this->autoDecode = $autoDecode;
     $this->xOpen();
 }
Example #4
0
<?php

include __DIR__ . '/config.php';
use AMQP\Connection;
use AMQP\Message;
$exchange = 'fanout_example_exchange';
$connection = new Connection(AMQP_RESOURCE);
$channel = $connection->channel();
/*
    name: $exchange
    type: fanout
    passive: false // don't check is an exchange with the same name exists
    durable: false // the exchange won't survive server restarts
    auto_delete: true //the exchange will be deleted once the channel is closed.
*/
$channel->exchangeDeclare($exchange, 'fanout');
$messageBody = implode(' ', array_slice($argv, 1));
$message = new Message($messageBody, array('content_type' => 'text/plain'));
$channel->basicPublish($message, array('exchange' => $exchange));
$channel->close();
$connection->close();
Example #5
0
<?php

/**
 *
 */
require_once __DIR__ . '/config.php';
use AMQP\Connection;
$options = array('ssl_options' => array('cafile' => CA_PATH, 'local_cert' => CERT_PATH, 'verify_peer' => true));
$connection = new Connection(AMQP_SSL_RESOURCE, $options);
register_shutdown_function(function () use($connection) {
    $connection->close();
});
while (true) {
}
Example #6
0
<?php

include __DIR__ . '/config.php';
use AMQP\Connection;
use AMQP\Message;
$exchange = 'basic_get_test';
$queue = 'basic_get_queue';
$conn = new Connection(AMQP_RESOURCE);
$ch = $conn->channel();
/*
    The following code is the same both in the consumer and the producer.
    In this way we are sure we always have a queue to consume from and an
        exchange where to publish messages.
*/
/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$ch->queueDeclare(array('queue' => $queue, 'durable' => true, 'auto_delete' => false));
/*
    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$ch->exchangeDeclare($exchange, 'direct', array('durable' => true, 'auto_delete' => false));
$ch->queueBind($queue, $exchange);
Example #7
0
<?php

/**
 * Usage: php file_consume.php 100
 */
include __DIR__ . '/config.php';
use AMQP\Connection;
use AMQP\Message;
$exchange = 'file_exchange';
$queue = 'file_queue';
$consumer_tag = '';
$conn = new Connection(AMQP_RESOURCE);
$ch = $conn->channel();
$ch->queueDeclare($queue, false, false, false, false);
$ch->exchangeDeclare($exchange, 'direct', false, false, false);
$ch->queueBind($queue, $exchange);
class Consumer
{
    protected $msgCount = 0;
    protected $startTime = null;
    public function process_message($msg)
    {
        if ($this->startTime === null) {
            $this->startTime = microtime(true);
        }
        if ($msg->body == 'quit') {
            echo sprintf("Pid: %s, Count: %s, Time: %.4f\n", getmypid(), $this->msgCount, microtime(true) - $this->startTime);
            die;
        }
        $this->msgCount++;
    }
Example #8
0
 /**
  * @param string $methodSig
  * @param string $args
  */
 protected function sendMethodFrame($methodSig, $args = '')
 {
     $this->connection->sendChannelMethodFrame($this->channelId, $methodSig, $args);
 }
<?php

include __DIR__ . '/config.php';
use AMQP\Connection;
$exchange = 'router';
$queue = 'msgs';
$consumerTag = 'consumer';
$connection = new Connection(AMQP_RESOURCE);
$channel = $connection->channel();
/*
    The following code is the same both in the consumer and the producer.
    In this way we are sure we always have a queue to consume from and an
        exchange where to publish messages.
*/
/*
    name: $queue
    passive: false
    durable: true // the queue will survive server restarts
    exclusive: false // the queue can be accessed in other channels
    auto_delete: false //the queue won't be deleted once the channel is closed.
*/
$channel->queueDeclare(array('queue' => $queue, 'durable' => true, 'auto_delete' => false));
/*
    name: $exchange
    type: direct
    passive: false
    durable: true // the exchange will survive server restarts
    auto_delete: false //the exchange won't be deleted once the channel is closed.
*/
$channel->exchangeDeclare($exchange, 'direct', array('durable' => true, 'auto_delete' => false));
$channel->queueBind($queue, $exchange);
<?php

use Amqp\Connection;
use NwWebsite\Di;
$connection = new Connection();
$di = Di::getInstance();
$config = $di->config->get('amqp');
$connection->setConfig(['host' => $config->host, 'port' => $config->port]);
return $connection;