|
| 1 | +# Mongodb transport |
| 2 | + |
| 3 | +Allows to use [MongoDB](https://www.mongodb.com/) as a message queue broker. |
| 4 | + |
| 5 | +* [Installation](#installation) |
| 6 | +* [Create context](#create-context) |
| 7 | +* [Send message to topic](#send-message-to-topic) |
| 8 | +* [Send message to queue](#send-message-to-queue) |
| 9 | +* [Send priority message](#send-priority-message) |
| 10 | +* [Send expiration message](#send-expiration-message) |
| 11 | +* [Send delayed message](#send-delayed-message) |
| 12 | +* [Consume message](#consume-message) |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +$ composer require enqueue/mongodb |
| 18 | +``` |
| 19 | + |
| 20 | +## Create context |
| 21 | + |
| 22 | +```php |
| 23 | +<?php |
| 24 | +use Enqueue\Mongodb\MongodbConnectionFactory; |
| 25 | + |
| 26 | +// connects to localhost |
| 27 | +$connectionFactory = new MongodbConnectionFactory(); |
| 28 | + |
| 29 | +// same as above |
| 30 | +$factory = new MongodbConnectionFactory('mongodb:'); |
| 31 | + |
| 32 | +// same as above |
| 33 | +$factory = new MongodbConnectionFactory([]); |
| 34 | + |
| 35 | +$factory = new MongodbConnectionFactory([ |
| 36 | + 'uri' => 'mongodb://localhost:27017/db_name', |
| 37 | + 'dbname' => 'enqueue', |
| 38 | + 'collection_name' => 'enqueue', |
| 39 | + 'polling_interval' => '1000', |
| 40 | +]); |
| 41 | + |
| 42 | +$psrContext = $factory->createContext(); |
| 43 | + |
| 44 | +// if you have enqueue/enqueue library installed you can use a function from there to create the context |
| 45 | +$psrContext = \Enqueue\dsn_to_context('mongodb:'); |
| 46 | +``` |
| 47 | + |
| 48 | +## Send message to topic |
| 49 | + |
| 50 | +```php |
| 51 | +<?php |
| 52 | +/** @var \Enqueue\Mongodb\MongodbContext $psrContext */ |
| 53 | +/** @var \Enqueue\Mongodb\MongodbDestination $fooTopic */ |
| 54 | + |
| 55 | +$message = $psrContext->createMessage('Hello world!'); |
| 56 | + |
| 57 | +$psrContext->createProducer()->send($fooTopic, $message); |
| 58 | +``` |
| 59 | + |
| 60 | +## Send message to queue |
| 61 | + |
| 62 | +```php |
| 63 | +<?php |
| 64 | +/** @var \Enqueue\Mongodb\MongodbContext $psrContext */ |
| 65 | +/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */ |
| 66 | + |
| 67 | +$message = $psrContext->createMessage('Hello world!'); |
| 68 | + |
| 69 | +$psrContext->createProducer()->send($fooQueue, $message); |
| 70 | +``` |
| 71 | + |
| 72 | +## Send priority message |
| 73 | + |
| 74 | +```php |
| 75 | +<?php |
| 76 | +/** @var \Enqueue\Mongodb\MongodbContext $psrContext */ |
| 77 | + |
| 78 | +$fooQueue = $psrContext->createQueue('foo'); |
| 79 | + |
| 80 | +$message = $psrContext->createMessage('Hello world!'); |
| 81 | + |
| 82 | +$psrContext->createProducer() |
| 83 | + ->setPriority(5) // the higher priority the sooner a message gets to a consumer |
| 84 | + // |
| 85 | + ->send($fooQueue, $message) |
| 86 | +; |
| 87 | +``` |
| 88 | + |
| 89 | +## Send expiration message |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | +/** @var \Enqueue\Mongodb\MongodbContext $psrContext */ |
| 94 | +/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */ |
| 95 | + |
| 96 | +$message = $psrContext->createMessage('Hello world!'); |
| 97 | + |
| 98 | +$psrContext->createProducer() |
| 99 | + ->setTimeToLive(60000) // 60 sec |
| 100 | + // |
| 101 | + ->send($fooQueue, $message) |
| 102 | +; |
| 103 | +``` |
| 104 | + |
| 105 | +## Send delayed message |
| 106 | + |
| 107 | +```php |
| 108 | +<?php |
| 109 | +use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy; |
| 110 | + |
| 111 | +/** @var \Enqueue\Mongodb\MongodbContext $psrContext */ |
| 112 | +/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */ |
| 113 | + |
| 114 | +// make sure you run "composer require enqueue/amqp-tools". |
| 115 | + |
| 116 | +$message = $psrContext->createMessage('Hello world!'); |
| 117 | + |
| 118 | +$psrContext->createProducer() |
| 119 | + ->setDeliveryDelay(5000) // 5 sec |
| 120 | + |
| 121 | + ->send($fooQueue, $message) |
| 122 | +; |
| 123 | +```` |
| 124 | + |
| 125 | +## Consume message: |
| 126 | + |
| 127 | +```php |
| 128 | +<?php |
| 129 | +/** @var \Enqueue\Mongodb\MongodbContext $psrContext */ |
| 130 | +/** @var \Enqueue\Mongodb\MongodbDestination $fooQueue */ |
| 131 | + |
| 132 | +$consumer = $psrContext->createConsumer($fooQueue); |
| 133 | + |
| 134 | +$message = $consumer->receive(); |
| 135 | + |
| 136 | +// process a message |
| 137 | + |
| 138 | +$consumer->acknowledge($message); |
| 139 | +// $consumer->reject($message); |
| 140 | +``` |
| 141 | + |
| 142 | +[back to index](../index.md) |
0 commit comments