Skip to content

Commit f87ef7f

Browse files
authored
Merge pull request #75 from php-enqueue/multi-transport-simple-client
Multi Transport Simple Client
2 parents da6ebcd + 99cefcd commit f87ef7f

22 files changed

+875
-283
lines changed

bin/release

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fi
1313

1414
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
1515

16-
for REMOTE in origin psr-queue stomp amqp-ext sqs fs redis dbal null enqueue enqueue-bundle job-queue test
16+
for REMOTE in origin psr-queue stomp amqp-ext sqs fs redis dbal null enqueue simple-client enqueue-bundle job-queue test
1717
do
1818
TMP_DIR="/tmp/enqueue-repo"
1919
REMOTE_URL=`git remote get-url $REMOTE`

bin/subtree-split

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function remote()
4545

4646
remote psr-queue git@github.com:php-enqueue/psr-queue.git
4747
remote enqueue git@github.com:php-enqueue/enqueue.git
48+
remote simple-client git@github.com:php-enqueue/simple-client.git
4849
remote stomp git@github.com:php-enqueue/stomp.git
4950
remote amqp-ext git@github.com:php-enqueue/amqp-ext.git
5051
remote fs git@github.com:php-enqueue/fs.git
@@ -58,6 +59,7 @@ remote test git@github.com:php-enqueue/test.git
5859

5960
split 'pkg/psr-queue' psr-queue
6061
split 'pkg/enqueue' enqueue
62+
split 'pkg/simple-client' simple-client
6163
split 'pkg/stomp' stomp
6264
split 'pkg/amqp-ext' amqp-ext
6365
split 'pkg/fs' fs

composer.json

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"enqueue/sqs": "*@dev",
1616
"enqueue/enqueue-bundle": "*@dev",
1717
"enqueue/job-queue": "*@dev",
18+
"enqueue/simple-client": "*@dev",
1819
"enqueue/test": "*@dev",
1920

2021
"phpunit/phpunit": "^5",
@@ -77,6 +78,10 @@
7778
{
7879
"type": "path",
7980
"url": "pkg/sqs"
81+
},
82+
{
83+
"type": "path",
84+
"url": "pkg/simple-client"
8085
}
8186
]
8287
}

docs/client/quick_tour.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Simple client. Quick tour.
2+
3+
The simple client library takes Enqueue client classes and Symfony components and makes an easy to use client facade.
4+
It reduces the boiler plate code you have to write to start using the Enqueue client features.
5+
6+
* [Install](#install)
7+
* [Configure](#configure)
8+
* [Producer message](#produce-message)
9+
* [Consume messages](#consume-messages)
10+
11+
## Install
12+
13+
```bash
14+
$ composer require enqueue/simple-client enqueue/amqp-ext
15+
```
16+
17+
## Configure
18+
19+
```php
20+
<?php
21+
use Enqueue\SimpleClient\SimpleClient;
22+
23+
include __DIR__.'/vendor/autoload.php';
24+
25+
$client = new SimpleClient([
26+
'transport' => [
27+
'default' => 'amqp',
28+
'amqp' => [
29+
'host' => 'localhost',
30+
'port' => 5672,
31+
'vhost' => '/',
32+
'login' => 'guest',
33+
'password' => 'guest',
34+
],
35+
],
36+
'client' => [
37+
'app_name' => 'plain_php',
38+
],
39+
]);
40+
```
41+
42+
## Produce message
43+
44+
```php
45+
<?php
46+
47+
/** @var \Enqueue\SimpleClient\SimpleClient $client */
48+
49+
$client->send('a_bar_topic', 'aMessageData');
50+
51+
// or an array
52+
53+
$client->send('a_bar_topic', ['foo', 'bar']);
54+
55+
// or an json serializable object
56+
$client->send('a_bar_topic', new class() implements \JsonSerializable {
57+
public function jsonSerialize() {
58+
return ['foo', 'bar'];
59+
}
60+
});
61+
```
62+
63+
## Consume messages
64+
65+
```php
66+
<?php
67+
68+
use Enqueue\Psr\PsrMessage;
69+
70+
/** @var \Enqueue\SimpleClient\SimpleClient $client */
71+
72+
$client->bind('a_bar_topic', 'a_processor_name', function(PsrMessage $psrMessage) {
73+
// processing logic here
74+
});
75+
76+
$client->consume();
77+
```
78+
79+
## Cli commands
80+
81+
```php
82+
#!/usr/bin/env php
83+
<?php
84+
85+
// bin/enqueue.php
86+
87+
use Enqueue\Symfony\Client\ConsumeMessagesCommand;
88+
use Enqueue\Symfony\Client\Meta\QueuesCommand;
89+
use Enqueue\Symfony\Client\Meta\TopicsCommand;
90+
use Enqueue\Symfony\Client\ProduceMessageCommand;
91+
use Enqueue\Symfony\Client\SetupBrokerCommand;
92+
use Symfony\Component\Console\Application;
93+
94+
/** @var \Enqueue\SimpleClient\SimpleClient $client */
95+
96+
$application = new Application();
97+
$application->add(new SetupBrokerCommand($client->getDriver()));
98+
$application->add(new ProduceMessageCommand($client->getProducer()));
99+
$application->add(new QueuesCommand($client->getQueueMetaRegistry()));
100+
$application->add(new TopicsCommand($client->getTopicMetaRegistry()));
101+
$application->add(new ConsumeMessagesCommand(
102+
$client->getQueueConsumer(),
103+
$client->getDelegateProcessor(),
104+
$client->getQueueMetaRegistry(),
105+
$client->getDriver()
106+
));
107+
108+
$application->run();
109+
```
110+
111+
and run to see what is there:
112+
113+
```bash
114+
$ php bin/enqueue.php
115+
```
116+
117+
or consume messages
118+
119+
```bash
120+
$ php bin/enqueue.php enqueue:consume -vvv --setup-broker
121+
```
122+
123+
[back to index](../index.md)

docs/client/rpc_call.md

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Client. RPC call
22

3+
The client's [quick tour](quick_tour.md) describes how to get the client object.
4+
We use you followed instructions there and have instance of `Enqueue\SimpleClient\SimpleClient` in `$client` var.
35

46
## The client side
57

@@ -8,13 +10,10 @@ It allows you to easily send a message and wait for a reply.
810

911
```php
1012
<?php
11-
use Enqueue\Client\SimpleClient;
1213
use Enqueue\Client\RpcClient;
1314

14-
/** @var \Enqueue\Psr\PsrContext $context */
15-
15+
/** @var \Enqueue\SimpleClient\SimpleClient $client */
1616

17-
$client = new SimpleClient($context);
1817
$rpcClient = new RpcClient($client->getProducer(), $context);
1918

2019
$replyMessage = $rpcClient->call('greeting_topic', 'Hi Thomas!', 5);
@@ -24,13 +23,10 @@ You can perform several requests asynchronously with `callAsync` and request rep
2423

2524
```php
2625
<?php
27-
use Enqueue\Client\SimpleClient;
2826
use Enqueue\Client\RpcClient;
2927

30-
/** @var \Enqueue\Psr\PsrContext $context */
28+
/** @var \Enqueue\SimpleClient\SimpleClient $client */
3129

32-
33-
$client = new SimpleClient($context);
3430
$rpcClient = new RpcClient($client->getProducer(), $context);
3531

3632
$promises = [];
@@ -53,7 +49,6 @@ Of course it is possible to implement rpc server side based on transport classes
5349
```php
5450
<?php
5551

56-
use Enqueue\Client\SimpleClient;
5752
use Enqueue\Psr\PsrMessage;
5853
use Enqueue\Psr\PsrContext;
5954
use Enqueue\Consumption\Result;
@@ -62,7 +57,8 @@ use Enqueue\Consumption\Extension\ReplyExtension;
6257

6358
/** @var \Enqueue\Psr\PsrContext $context */
6459

65-
$client = new SimpleClient($this->context);
60+
/** @var \Enqueue\SimpleClient\SimpleClient $client */
61+
6662
$client->bind('greeting_topic', 'greeting_processor', function (PsrMessage $message, PsrContext $context) use (&$requestMessage) {
6763
echo $message->getBody();
6864

docs/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Extensions](consumption/extensions.md)
1414
- [Message processor](consumption/message_processor.md)
1515
* Client
16+
- [Quick tour](client/quick_tour.md)
1617
- [Message examples](client/message_examples.md)
1718
- [Supported brokers](client/supported_brokers.md)
1819
- [Message bus](client/message_bus.md)

docs/quick_tour.md

+20-10
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,30 @@ Here's an example of how you can send and consume messages.
167167

168168
```php
169169
<?php
170-
use Enqueue\Client\SimpleClient;
170+
use Enqueue\SimpleClient\SimpleClient;
171171
use Enqueue\Psr\PsrMessage;
172-
use Enqueue\Psr\PsrProcessor;
173-
174-
/** @var \Enqueue\Psr\PsrContext $psrContext */
175172

176-
$client = new SimpleClient($psrContext);
177-
$client->bind('foo_topic', 'processor_name', function (PsrMessage $message) {
178-
// process message
179-
180-
return PsrProcessor::ACK;
173+
$client = new SimpleClient([
174+
'transport' => [
175+
'default' => 'amqp',
176+
'amqp' => [
177+
'host' => 'localhost',
178+
'port' => 5672,
179+
'vhost' => '/',
180+
'login' => 'guest',
181+
'password' => 'guest',
182+
],
183+
],
184+
'client' => true,
185+
]);
186+
187+
$client->setupBroker();
188+
189+
$client->bind('a_foo_topic', 'fooProcessor', function(PsrMessage $message) {
190+
// your processing logic here
181191
});
182192

183-
$client->send('foo_topic', 'Hello there!');
193+
$client->send('a_bar_topic', 'aMessageData');
184194

185195
// in another process you can consume messages.
186196
$client->consume();

phpunit.xml.dist

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
<testsuite name="job-queue">
5757
<directory>pkg/job-queue/Tests</directory>
5858
</testsuite>
59+
60+
<testsuite name="simple-client">
61+
<directory>pkg/simple-client/Tests</directory>
62+
</testsuite>
5963
</testsuites>
6064

6165
<filter>

0 commit comments

Comments
 (0)