|
| 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) |
0 commit comments