public function tearDown() { if (!$this->channel) { return; } $this->channel->exchangeDelete($this->exchangeName); $this->channel->close(); $this->connection->close(); }
<?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();
$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); $toSend = new Message('test message', array('content_type' => 'text/plain', 'delivery_mode' => 2)); $ch->basicPublish($toSend, array('exchange' => $exchange)); $msg = $ch->basicGet(array('queue' => $queue)); $ch->basicAck($msg->delivery_info['delivery_tag']); var_dump($msg->body); $ch->close(); $conn->close();