|
| 1 | +# Beanstalk (Pheanstalk) transport |
| 2 | + |
| 3 | +The transport uses [Beanstalkd](http://kr.github.io/beanstalkd/) job manager. |
| 4 | +The transport uses [Pheanstalk](https://github.com/pda/pheanstalk) library internally. |
| 5 | + |
| 6 | +* [Installation](#installation) |
| 7 | +* [Create context](#create-context) |
| 8 | +* [Send message to topic](#send-message-to-topic) |
| 9 | +* [Send message to queue](#send-message-to-queue) |
| 10 | +* [Consume message](#consume-message) |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +$ composer require enqueue/pheanstalk |
| 16 | +``` |
| 17 | + |
| 18 | + |
| 19 | +## Create context |
| 20 | + |
| 21 | +```php |
| 22 | +<?php |
| 23 | +use Enqueue\Pheanstalk\PheanstalkConnectionFactory; |
| 24 | + |
| 25 | +// connects to localhost:11300 |
| 26 | +$factory = new PheanstalkConnectionFactory(); |
| 27 | + |
| 28 | +// same as above |
| 29 | +$factory = new PheanstalkConnectionFactory('beanstalk://'); |
| 30 | + |
| 31 | +// connects to example host and port 5555 |
| 32 | +$factory = new PheanstalkConnectionFactory('beanstalk://example:5555'); |
| 33 | + |
| 34 | +// same as above but configured by array |
| 35 | +$factory = new PheanstalkConnectionFactory([ |
| 36 | + 'host' => 'example', |
| 37 | + 'port' => 5555 |
| 38 | +]); |
| 39 | +``` |
| 40 | + |
| 41 | +## Send message to topic |
| 42 | + |
| 43 | +```php |
| 44 | +<?php |
| 45 | +/** @var \Enqueue\Pheanstalk\PheanstalkContext $psrContext */ |
| 46 | + |
| 47 | +$fooTopic = $psrContext->createTopic('aTopic'); |
| 48 | +$message = $psrContext->createMessage('Hello world!'); |
| 49 | + |
| 50 | +$psrContext->createProducer()->send($fooTopic, $message); |
| 51 | +``` |
| 52 | + |
| 53 | +## Send message to queue |
| 54 | + |
| 55 | +```php |
| 56 | +<?php |
| 57 | +/** @var \Enqueue\Pheanstalk\PheanstalkContext $psrContext */ |
| 58 | + |
| 59 | +$fooQueue = $psrContext->createQueue('aQueue'); |
| 60 | +$message = $psrContext->createMessage('Hello world!'); |
| 61 | + |
| 62 | +$psrContext->createProducer()->send($fooQueue, $message); |
| 63 | +``` |
| 64 | + |
| 65 | +## Consume message: |
| 66 | + |
| 67 | +```php |
| 68 | +<?php |
| 69 | +/** @var \Enqueue\Pheanstalk\PheanstalkContext $psrContext */ |
| 70 | + |
| 71 | +$fooQueue = $psrContext->createQueue('aQueue'); |
| 72 | +$consumer = $psrContext->createConsumer($fooQueue); |
| 73 | + |
| 74 | +$message = $consumer->receive(2000); // wait for 2 seconds |
| 75 | + |
| 76 | +$message = $consumer->receiveNoWait(); // fetch message or return null immediately |
| 77 | + |
| 78 | +// process a message |
| 79 | + |
| 80 | +$consumer->acknowledge($message); |
| 81 | +// $consumer->reject($message); |
| 82 | +``` |
| 83 | + |
| 84 | +[back to index](../index.md) |
0 commit comments