Skip to content

Commit bec5bb9

Browse files
authored
Merge pull request #92 from php-enqueue/readme-impr
Add some handy functions. Improve READMEs
2 parents d8dbffa + d45120a commit bec5bb9

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is where all development happens. The repository provides a friendly enviro
88
Features:
99

1010
* [JMS](https://docs.oracle.com/javaee/7/api/javax/jms/package-summary.html) like transport [abstraction](https://github.com/php-enqueue/psr-queue).
11-
* Feature rich.
11+
* [Feature rich](docs/quick_tour.md).
1212
* Supports transports:
1313
- [AMQP](docs/transport/amqp.md) (RabbitMQ, ActiveMQ and others),
1414
- [STOMP](docs/transport/stomp.md)
@@ -18,7 +18,56 @@ Features:
1818
- [Filesystem](docs/transport/filesystem.md)
1919
- [Null](docs/transport/null.md).
2020
* Generic purpose abstraction level (the transport level).
21+
22+
```php
23+
<?php
24+
use function Enqueue\dsn_to_context;
25+
use function Enqueue\send_queue;
26+
use function Enqueue\consume;
27+
use Enqueue\Psr\PsrMessage;
28+
use Enqueue\Consumption\Result;
29+
30+
// composer require enqueue/enqueue enqueue/amqp-ext
31+
32+
$c = dsn_to_context('amqp:://');
33+
34+
send_queue($c, 'a_queue', 'Hello there');
35+
36+
consume($c, 'a_queue', function(PsrMessage $message) {
37+
$body = $message->getBody();
38+
39+
// to stop consumption: throw new \Enqueue\Consumption\Exception\ConsumptionInterruptedException;
40+
41+
return Result::ACK;
42+
});
43+
```
44+
2145
* Easy to use abstraction level (the client level).
46+
47+
```php
48+
<?php
49+
use Enqueue\SimpleClient\SimpleClient;
50+
use Enqueue\Psr\PsrMessage;
51+
use Enqueue\Consumption\Result;
52+
53+
// composer require enqueue/simple-client enqueue/fs
54+
55+
$client = new SimpleClient('file://');
56+
$client->bind('a_topic', 'a_processor', function(PsrMessage $message) {
57+
$body = $message->getBody();
58+
59+
// to stop consumption: throw new \Enqueue\Consumption\Exception\ConsumptionInterruptedException;
60+
61+
return Result::ACK;
62+
});
63+
64+
$client->setupBroker();
65+
66+
$client->send('a_topic', 'Hello there');
67+
68+
$client->consume();
69+
```
70+
2271
* [Symfony bundle](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/bundle/quick_tour.md)
2372
* [Magento1 extension](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/magento/quick_tour.md)
2473
* [Message bus](http://www.enterpriseintegrationpatterns.com/patterns/messaging/MessageBus.html) support.
@@ -30,6 +79,7 @@ Features:
3079

3180
## Resources
3281

82+
* [Quick tour](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/quick_tour.md)
3383
* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md)
3484
* [Questions](https://gitter.im/php-enqueue/Lobby)
3585
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)

pkg/enqueue-bundle/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Integrates message queue components to Symfony application.
99

1010
## Resources
1111

12+
* [Quick tour](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/bundle/quick_tour.md)
1213
* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md)
1314
* [Questions](https://gitter.im/php-enqueue/Lobby)
1415
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)

pkg/enqueue/functions.php

+40
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue;
44

55
use Enqueue\AmqpExt\AmqpConnectionFactory;
6+
use Enqueue\Consumption\QueueConsumer;
67
use Enqueue\Fs\FsConnectionFactory;
78
use Enqueue\Null\NullConnectionFactory;
89
use Enqueue\Psr\PsrConnectionFactory;
@@ -56,3 +57,42 @@ function dsn_to_context($dsn)
5657
{
5758
return dsn_to_connection_factory($dsn)->createContext();
5859
}
60+
61+
/**
62+
* @param PsrContext $c
63+
* @param string $topic
64+
* @param string $body
65+
*/
66+
function send_topic(PsrContext $c, $topic, $body)
67+
{
68+
$topic = $c->createTopic($topic);
69+
$message = $c->createMessage($body);
70+
71+
$c->createProducer()->send($topic, $message);
72+
}
73+
74+
/**
75+
* @param PsrContext $c
76+
* @param string $queue
77+
* @param string $body
78+
*/
79+
function send_queue(PsrContext $c, $queue, $body)
80+
{
81+
$queue = $c->createQueue($queue);
82+
$message = $c->createMessage($body);
83+
84+
$c->createProducer()->send($queue, $message);
85+
}
86+
87+
/**
88+
* @param PsrContext $c
89+
* @param string $queue
90+
* @param callable $callback
91+
*/
92+
function consume(PsrContext $c, $queue, callable $callback)
93+
{
94+
$queueConsumer = new QueueConsumer($c);
95+
$queueConsumer->bind($queue, $callback);
96+
97+
$queueConsumer->consume();
98+
}

0 commit comments

Comments
 (0)