Skip to content

Commit 700dc96

Browse files
committed
Amqp bunny
1 parent 06fd596 commit 700dc96

46 files changed

Lines changed: 3347 additions & 18 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎composer.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"enqueue/stomp": "*@dev",
99
"enqueue/amqp-ext": "*@dev",
1010
"enqueue/amqp-lib": "*@dev",
11+
"enqueue/amqp-bunny": "*@dev",
1112
"php-amqplib/php-amqplib": "^2.7@dev",
1213
"enqueue/redis": "*@dev",
1314
"enqueue/fs": "*@dev",
@@ -79,6 +80,10 @@
7980
"type": "path",
8081
"url": "pkg/amqp-lib"
8182
},
83+
{
84+
"type": "path",
85+
"url": "pkg/amqp-bunny"
86+
},
8287
{
8388
"type": "path",
8489
"url": "pkg/redis"

‎docs/index.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* [Quick tour](quick_tour.md)
44
* Transports
5-
- Amqp based on [the ext](docs/transport/amqp.md), [the lib](docs/transport/amqp_lib.md)
5+
- Amqp based on [the ext](docs/transport/amqp.md), [bunny](docs/transport/amqp_bunny.md), [the lib](docs/transport/amqp_lib.md)
66
- [Amazon SQS](transport/sqs.md)
77
- [Beanstalk (Pheanstalk)](transport/pheanstalk.md)
88
- [Gearman](transport/gearman.md)

‎docs/transport/amqp_bunny.md‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
# AMQP transport
2+
3+
Implements [AMQP specifications](https://www.rabbitmq.com/specification.html) and implements [amqp interop](https://github.com/queue-interop/amqp-interop) interfaces.
4+
Build on top of [bunny lib](https://github.com/jakubkulhan/bunny).
5+
6+
* [Installation](#installation)
7+
* [Create context](#create-context)
8+
* [Declare topic](#declare-topic)
9+
* [Declare queue](#decalre-queue)
10+
* [Bind queue to topic](#bind-queue-to-topic)
11+
* [Send message to topic](#send-message-to-topic)
12+
* [Send message to queue](#send-message-to-queue)
13+
* [Send priority message](#send-priority-message)
14+
* [Send expiration message](#send-expiration-message)
15+
* [Consume message](#consume-message)
16+
* [Purge queue messages](#purge-queue-messages)
17+
18+
## Installation
19+
20+
```bash
21+
$ composer require enqueue/amqp-bunny
22+
```
23+
24+
## Create context
25+
26+
```php
27+
<?php
28+
use Enqueue\AmqpBunny\AmqpConnectionFactory;
29+
30+
// connects to localhost
31+
$connectionFactory = new AmqpConnectionFactory();
32+
33+
// same as above
34+
$connectionFactory = new AmqpConnectionFactory('amqp://');
35+
36+
// same as above
37+
$connectionFactory = new AmqpConnectionFactory([]);
38+
39+
// connect to AMQP broker at example.com
40+
$connectionFactory = new AmqpConnectionFactory([
41+
'host' => 'example.com',
42+
'port' => 1000,
43+
'vhost' => '/',
44+
'user' => 'user',
45+
'pass' => 'pass',
46+
'persisted' => false,
47+
]);
48+
49+
// same as above but given as DSN string
50+
$connectionFactory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f');
51+
52+
$psrContext = $connectionFactory->createContext();
53+
```
54+
55+
## Declare topic.
56+
57+
Declare topic operation creates a topic on a broker side.
58+
59+
```php
60+
<?php
61+
use Interop\Amqp\AmqpTopic;
62+
63+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
64+
65+
$fooTopic = $psrContext->createTopic('foo');
66+
$fooTopic->setType(AmqpTopic::TYPE_FANOUT);
67+
$psrContext->declareTopic($fooTopic);
68+
69+
// to remove topic use delete topic method
70+
//$psrContext->deleteTopic($fooTopic);
71+
```
72+
73+
## Declare queue.
74+
75+
Declare queue operation creates a queue on a broker side.
76+
77+
```php
78+
<?php
79+
use Interop\Amqp\AmqpQueue;
80+
81+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
82+
83+
$fooQueue = $psrContext->createQueue('foo');
84+
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
85+
$psrContext->declareQueue($fooQueue);
86+
87+
// to remove topic use delete queue method
88+
//$psrContext->deleteQueue($fooQueue);
89+
```
90+
91+
## Bind queue to topic
92+
93+
Connects a queue to the topic. So messages from that topic comes to the queue and could be processed.
94+
95+
```php
96+
<?php
97+
use Interop\Amqp\Impl\AmqpBind;
98+
99+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
100+
/** @var \Interop\Amqp\Impl\AmqpQueue $fooQueue */
101+
/** @var \Interop\Amqp\Impl\AmqpTopic $fooTopic */
102+
103+
$psrContext->bind(new AmqpBind($fooTopic, $fooQueue));
104+
```
105+
106+
## Send message to topic
107+
108+
```php
109+
<?php
110+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
111+
/** @var \Interop\Amqp\Impl\AmqpTopic $fooTopic */
112+
113+
$message = $psrContext->createMessage('Hello world!');
114+
115+
$psrContext->createProducer()->send($fooTopic, $message);
116+
```
117+
118+
## Send message to queue
119+
120+
```php
121+
<?php
122+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
123+
/** @var \Interop\Amqp\Impl\AmqpQueue $fooQueue */
124+
125+
$message = $psrContext->createMessage('Hello world!');
126+
127+
$psrContext->createProducer()->send($fooQueue, $message);
128+
```
129+
130+
## Send priority message
131+
132+
```php
133+
<?php
134+
/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */
135+
136+
$fooQueue = $psrContext->createQueue('foo');
137+
$fooQueue->addFlag(AmqpQueue::FLAG_DURABLE);
138+
$fooQueue->setArguments(['x-max-priority' => 10]);
139+
$psrContext->declareQueue($fooQueue);
140+
141+
$message = $psrContext->createMessage('Hello world!');
142+
143+
$psrContext->createProducer()
144+
->setPriority(5) // the higher priority the sooner a message gets to a consumer
145+
//
146+
->send($fooQueue, $message)
147+
;
148+
```
149+
150+
## Send expiration message
151+
152+
```php
153+
<?php
154+
/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */
155+
/** @var \Interop\Amqp\Impl\AmqpQueue $fooQueue */
156+
157+
$message = $psrContext->createMessage('Hello world!');
158+
159+
$psrContext->createProducer()
160+
->setTimeToLive(60000) // 60 sec
161+
//
162+
->send($fooQueue, $message)
163+
;
164+
```
165+
166+
## Consume message:
167+
168+
```php
169+
<?php
170+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
171+
/** @var \Interop\Amqp\Impl\AmqpQueue $fooQueue */
172+
173+
$consumer = $psrContext->createConsumer($fooQueue);
174+
175+
$message = $consumer->receive();
176+
177+
// process a message
178+
179+
$consumer->acknowledge($message);
180+
// $consumer->reject($message);
181+
```
182+
183+
## Purge queue messages:
184+
185+
```php
186+
<?php
187+
/** @var \Enqueue\AmqpBunny\AmqpContext $psrContext */
188+
/** @var \Interop\Amqp\Impl\AmqpQueue $fooQueue */
189+
190+
$queue = $psrContext->createQueue('aQueue');
191+
192+
$psrContext->purgeQueue($queue);
193+
```
194+
195+
[back to index](../index.md)

‎phpunit.xml.dist‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
<directory>pkg/amqp-ext/Tests</directory>
2626
</testsuite>
2727

28+
<testsuite name="amqp-bunny transport">
29+
<directory>pkg/amqp-bunny/Tests</directory>
30+
</testsuite>
31+
2832
<testsuite name="fs transport">
2933
<directory>pkg/fs/Tests</directory>
3034
</testsuite>

‎pkg/amqp-bunny/.gitignore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*~
2+
/composer.lock
3+
/composer.phar
4+
/phpunit.xml
5+
/vendor/
6+
/.idea/

‎pkg/amqp-bunny/.travis.yml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sudo: false
2+
3+
git:
4+
depth: 1
5+
6+
language: php
7+
8+
php:
9+
- '5.6'
10+
- '7.0'
11+
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
install:
17+
- composer self-update
18+
- composer install
19+
20+
script:
21+
- vendor/bin/phpunit --exclude-group=functional

0 commit comments

Comments
 (0)