Skip to content

Amqp interop based transports. #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions php-interop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# PHP code for RabbitMQ tutorials based on AMQP interop

Here you can find PHP code examples from [RabbitMQ
tutorials](http://www.rabbitmq.com/getstarted.html) adopted to [amqp interop](https://github.com/queue-interop/queue-interop#amqp-interop)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to say queue-interop since that's the name of your project and mention that it's a PHP library. amqp-interop can hint at AMQP 0-9-1 / AMQP 1.0 interoperability to some.

These examples will work with any amqp interop compatible transports such as [enqueue/amqp-ext](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp.md), [enqueue/amqp-bunny](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_bunny.md), [enqueue/amqp-lib](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/transport/amqp_lib.md)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what should be done here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Nevermind, I will correct it locally before merging.


To successfully use the examples you will need a running RabbitMQ server.

## Requirements

### PHP 5.5+

You need `PHP 5.5` and one of the amqp interop compatible transport.


### Composer

Then [install Composer](https://getcomposer.org/download/) per instructions on their site.

### Client Library

Then you can, for example, install `enqueue/amqp-bunny` using [Composer](http://getcomposer.org).

To do that install Composer and add it to your path, then run the following command
inside this project folder:

```bash
$ composer require enqueue/amqp-bunny
```

34 changes: 34 additions & 0 deletions php-interop/emit_log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpTopic;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$topic = $context->createTopic('logs');
$topic->setType(AmqpTopic::TYPE_FANOUT);

$context->declareTopic($topic);

$data = implode(' ', array_slice($argv, 1));
if (empty($data)) {
$data = 'info: Hello World!';
}
$message = $context->createMessage($data);

$context->createProducer()->send($topic, $message);

echo ' [x] Sent ', $data, "\n";

$context->close();
38 changes: 38 additions & 0 deletions php-interop/emit_log_direct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpTopic;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$topic = $context->createTopic('direct_logs');
$topic->setType(AmqpTopic::TYPE_DIRECT);

$context->declareTopic($topic);

$severity = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'info';

$data = implode(' ', array_slice($argv, 2));
if (empty($data)) {
$data = 'Hello World!';
}

$message = $context->createMessage($data);
$message->setRoutingKey($severity);

$context->createProducer()->send($topic, $message);

echo ' [x] Sent ',$severity,':',$data," \n";

$context->close();
38 changes: 38 additions & 0 deletions php-interop/emit_log_topic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpTopic;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$topic = $context->createTopic('topic_logs');
$topic->setType(AmqpTopic::TYPE_TOPIC);

$context->declareTopic($topic);

$routing_key = isset($argv[1]) && !empty($argv[1]) ? $argv[1] : 'anonymous.info';

$data = implode(' ', array_slice($argv, 2));
if (empty($data)) {
$data = 'Hello World!';
}

$message = $context->createMessage($data);
$message->setRoutingKey($routing_key);

$context->createProducer()->send($topic, $message);

echo ' [x] Sent ',$routing_key,':',$data," \n";

$context->close();
36 changes: 36 additions & 0 deletions php-interop/new_task.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpMessage;
use Interop\Amqp\AmqpQueue;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$queue = $context->createQueue('task_queue');
$queue->addFlag(AmqpQueue::FLAG_DURABLE);

$context->declareQueue($queue);

$data = implode(' ', array_slice($argv, 1));
if (empty($data)) {
$data = 'Hello World!';
}
$message = $context->createMessage($data);
$message->setDeliveryMode(AmqpMessage::DELIVERY_MODE_PERSISTENT);

$context->createProducer()->send($queue, $message);

echo ' [x] Sent ', $data, "\n";

$context->close();
34 changes: 34 additions & 0 deletions php-interop/receive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpConsumer;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'receive_method' => 'basic_consume',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$queue = $context->createQueue('hello');
$context->declareQueue($queue);

$consumer = $context->createConsumer($queue);
$consumer->addFlag(AmqpConsumer::FLAG_NOACK);

echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

while (true) {
if ($message = $consumer->receive()) {
echo ' [x] Received ', $message->getBody(), "\n";
}
}

$context->close();
42 changes: 42 additions & 0 deletions php-interop/receive_logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpConsumer;
use Interop\Amqp\AmqpTopic;
use Interop\Amqp\Impl\AmqpBind;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'receive_method' => 'basic_consume',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$topic = $context->createTopic('logs');
$topic->setType(AmqpTopic::TYPE_FANOUT);

$context->declareTopic($topic);

$queue = $context->createTemporaryQueue();

$context->bind(new AmqpBind($topic, $queue));

$consumer = $context->createConsumer($queue);
$consumer->addFlag(AmqpConsumer::FLAG_NOACK);

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

while (true) {
if ($message = $consumer->receive()) {
echo ' [x] ', $message->getBody(), "\n";
}
}

$context->close();
50 changes: 50 additions & 0 deletions php-interop/receive_logs_direct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpConsumer;
use Interop\Amqp\AmqpTopic;
use Interop\Amqp\Impl\AmqpBind;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'receive_method' => 'basic_consume',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$topic = $context->createTopic('direct_logs');
$topic->setType(AmqpTopic::TYPE_DIRECT);

$context->declareTopic($topic);

$queue = $context->createTemporaryQueue();

$severities = array_slice($argv, 1);
if (empty($severities)) {
file_put_contents('php://stderr', "Usage: $argv[0] [info] [warning] [error]\n");
exit(1);
}

foreach ($severities as $severity) {
$context->bind(new AmqpBind($topic, $queue, $severity));
}

$consumer = $context->createConsumer($queue);
$consumer->addFlag(AmqpConsumer::FLAG_NOACK);

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

while (true) {
if ($message = $consumer->receive()) {
echo ' [x] '.$message->getRoutingKey().':'.$message->getBody()."\n";
}
}

$context->close();
50 changes: 50 additions & 0 deletions php-interop/receive_logs_topic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

// composer require enqueue/amqp-bunny
require_once __DIR__.'/vendor/autoload.php';

use Enqueue\AmqpBunny\AmqpConnectionFactory;
use Interop\Amqp\AmqpConsumer;
use Interop\Amqp\AmqpTopic;
use Interop\Amqp\Impl\AmqpBind;

$config = [
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
'pass' => 'guest',
'receive_method' => 'basic_consume',
];

$connection = new AmqpConnectionFactory($config);
$context = $connection->createContext();

$topic = $context->createTopic('topic_logs');
$topic->setType(AmqpTopic::TYPE_TOPIC);

$context->declareTopic($topic);

$queue = $context->createTemporaryQueue();

$binding_keys = array_slice($argv, 1);
if (empty($binding_keys)) {
file_put_contents('php://stderr', "Usage: $argv[0] [binding_key]\n");
exit(1);
}

foreach ($binding_keys as $binding_key) {
$context->bind(new AmqpBind($topic, $queue, $binding_key));
}

$consumer = $context->createConsumer($queue);
$consumer->addFlag(AmqpConsumer::FLAG_NOACK);

echo ' [*] Waiting for logs. To exit press CTRL+C', "\n";

while (true) {
if ($message = $consumer->receive()) {
echo ' [x] '.$message->getRoutingKey().':'.$message->getBody()."\n";
}
}

$context->close();
Loading