|
| 1 | +# AMQP transport |
| 2 | + |
| 3 | +* [Create context](#create-context) |
| 4 | +* [Declare topic](#declare-topic) |
| 5 | +* [Declare queue](#decalre-queue) |
| 6 | +* [Bind queue to topic](#bind-queue-to-topic) |
| 7 | +* [Send message to topic](#send-message-to-topic) |
| 8 | +* [Send message to queue](#send-message-to-queue) |
| 9 | +* [Consume message](#consume-message) |
| 10 | + |
| 11 | +## Create context |
| 12 | + |
| 13 | +```php |
| 14 | +<?php |
| 15 | +use Enqueue\AmqpExt\AmqpConnectionFactory; |
| 16 | + |
| 17 | +$connectionFactory = new AmqpConnectionFactory([ |
| 18 | + 'host' => '127.0.0.1', |
| 19 | + 'port' => 5672, |
| 20 | + 'vhost' => '/', |
| 21 | + 'login' => 'guest', |
| 22 | + 'password' => 'guest', |
| 23 | + 'persisted' => false, |
| 24 | +]); |
| 25 | + |
| 26 | +$psrContext = $connectionFactory->createContext(); |
| 27 | +``` |
| 28 | + |
| 29 | +## Declare topic. |
| 30 | + |
| 31 | +Declare topic operation creates a topic on a broker side. |
| 32 | + |
| 33 | +```php |
| 34 | +<?php |
| 35 | +/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */ |
| 36 | + |
| 37 | +$fooTopic = $psrContext->createTopic('foo'); |
| 38 | +$fooTopic->addFlag(AMQP_EX_TYPE_FANOUT); |
| 39 | +$psrContext->declareTopic($fooTopic); |
| 40 | + |
| 41 | +// to remove topic use delete topic method |
| 42 | +//$psrContext->deleteTopic($fooTopic); |
| 43 | +``` |
| 44 | + |
| 45 | +## Declare queue. |
| 46 | + |
| 47 | +Declare queue operation creates a queue on a broker side. |
| 48 | + |
| 49 | +```php |
| 50 | +<?php |
| 51 | +/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */ |
| 52 | + |
| 53 | +$fooQueue = $psrContext->createQueue('foo'); |
| 54 | +$fooQueue->addFlag(AMQP_DURABLE); |
| 55 | +$psrContext->declareQueue($fooQueue); |
| 56 | + |
| 57 | +// to remove topic use delete queue method |
| 58 | +//$psrContext->deleteQueue($fooQueue); |
| 59 | +``` |
| 60 | + |
| 61 | +## Bind queue to topic |
| 62 | + |
| 63 | +Connects a queue to the topic. So messages from that topic comes to the queue and could be processed. |
| 64 | + |
| 65 | +```php |
| 66 | +<?php |
| 67 | +/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */ |
| 68 | +/** @var \Enqueue\AmqpExt\AmqpQueue $fooQueue */ |
| 69 | +/** @var \Enqueue\AmqpExt\AmqpTopic $fooTopic */ |
| 70 | + |
| 71 | +$psrContext->bind($fooTopic, $fooQueue); |
| 72 | +``` |
| 73 | + |
| 74 | +## Send message to topic |
| 75 | + |
| 76 | +```php |
| 77 | +<?php |
| 78 | +/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */ |
| 79 | +/** @var \Enqueue\AmqpExt\AmqpTopic $fooTopic */ |
| 80 | + |
| 81 | +$message = $psrContext->createMessage('Hello world!'); |
| 82 | + |
| 83 | +$psrContext->createProducer()->send($fooTopic, $message); |
| 84 | +``` |
| 85 | + |
| 86 | +## Send message to queue |
| 87 | + |
| 88 | +```php |
| 89 | +<?php |
| 90 | +/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */ |
| 91 | +/** @var \Enqueue\AmqpExt\AmqpQueue $fooQueue */ |
| 92 | + |
| 93 | +$message = $psrContext->createMessage('Hello world!'); |
| 94 | + |
| 95 | +$psrContext->createProducer()->send($fooQueue, $message); |
| 96 | +``` |
| 97 | + |
| 98 | +## Consume message: |
| 99 | + |
| 100 | +```php |
| 101 | +<?php |
| 102 | +/** @var \Enqueue\AmqpExt\AmqpContext $psrContext */ |
| 103 | +/** @var \Enqueue\AmqpExt\AmqpQueue $fooQueue */ |
| 104 | + |
| 105 | +$consumer = $psrContext->createConsumer($fooQueue); |
| 106 | + |
| 107 | +$message = $consumer->receive(); |
| 108 | + |
| 109 | +// process a message |
| 110 | + |
| 111 | +$consumer->acknowledge($message); |
| 112 | +// $consumer->reject($message); |
| 113 | +``` |
| 114 | + |
| 115 | +[back to index](index.md) |
0 commit comments