Skip to content

Commit 05059f8

Browse files
committed
Merge pull request #177 from steveYeah/master
Add expect_serialized_response option to RCP Client set up
2 parents fc54885 + 908fe41 commit 05059f8

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- 2014-02-24
2+
* Add a parameter to RPC client configuration to disable auto unserialize when adding call results to results array.
3+
14
- 2013-01-18
25
* adds an an optional parameter for the AMQP Message Properties for the publish method of the Producer, so they can be set as well. For example, seeting the application_headers is now possible.
36

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function getConfigTreeBuilder()
7979
->prototype('array')
8080
->children()
8181
->scalarNode('connection')->defaultValue('default')->end()
82+
->booleanNode('expect_serialized_response')->defaultTrue()->end()
8283
->end()
8384
->end()
8485
->end()

DependencyInjection/OldSoundRabbitMqExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected function loadRpcClients()
226226
$definition = new Definition('%old_sound_rabbit_mq.rpc_client.class%');
227227
$definition
228228
->addTag('old_sound_rabbit_mq.rpc_client')
229-
->addMethodCall('initClient');
229+
->addMethodCall('initClient', array($client['expect_serialized_response']));
230230
$this->injectConnection($definition, $client['connection']);
231231
if ($this->collectorEnabled) {
232232
$this->injectLoggedChannel($definition, $key, $client['connection']);

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ The arguments we are sending are the __min__ and __max__ values for the `rand()`
407407
408408
The final piece is to get the reply. Our PHP script will block till the server returns a value. The __$replies__ variable will be an associative array where each reply from the server will contained in the respective __request\_id__ key.
409409
410+
By default the RCP Client expects the response to be serialized. If the server you are working with returns a non-serialized result then set the RPC client expect_serialized_response option to false. For example, if the integer_store server didn't serialize the result the client would be set as below:
411+
412+
```yaml
413+
rpc_clients:
414+
integer_store:
415+
connection: default
416+
expect_serialized_response: false
417+
```
418+
410419
You can also set a expiration for request in seconds, after which message will no longer be handled by server and client request will simply time out. Setting expiration for messages works only for RabbitMQ 3.x and above. Visit http://www.rabbitmq.com/ttl.html#per-message-ttl for more information.
411420
412421
```php

RabbitMq/RpcClient.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ class RpcClient extends BaseAmqp
1010
protected $requests = 0;
1111
protected $replies = array();
1212
protected $queueName;
13+
protected $expectSerializedResponse;
1314
protected $timeout = 0;
1415

15-
public function initClient()
16+
public function initClient($expectSerializedResponse = true)
1617
{
1718
list($this->queueName, ,) = $this->getChannel()->queue_declare("", false, false, true, true);
19+
$this->expectSerializedResponse = $expectSerializedResponse;
1820
}
1921

2022
public function addRequest($msgBody, $server, $requestId = null, $routingKey = '', $expiration = 0)
@@ -32,7 +34,7 @@ public function addRequest($msgBody, $server, $requestId = null, $routingKey = '
3234
$this->getChannel()->basic_publish($msg, $server, $routingKey);
3335

3436
$this->requests++;
35-
37+
3638
if ($expiration > $this->timeout) {
3739
$this->timeout = $expiration;
3840
}
@@ -56,6 +58,11 @@ public function getReplies()
5658

5759
public function processMessage(AMQPMessage $msg)
5860
{
59-
$this->replies[$msg->get('correlation_id')] = unserialize($msg->body);
61+
$messageBody = $msg->body;
62+
if ($this->expectSerializedResponse) {
63+
$messageBody = unserialize($messageBody);
64+
}
65+
66+
$this->replies[$msg->get('correlation_id')] = $messageBody;
6067
}
6168
}

Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public function testFooRpcClientDefinition()
338338
$this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.foo_connection');
339339
$this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.foo_client');
340340
$this->assertEquals(
341-
array(array('initClient', array())),
341+
array(array('initClient', array(true))),
342342
$definition->getMethodCalls()
343343
);
344344
$this->assertEquals('%old_sound_rabbit_mq.rpc_client.class%', $definition->getClass());
@@ -353,7 +353,7 @@ public function testDefaultRpcClientDefinition()
353353
$this->assertEquals((string) $definition->getArgument(0), 'old_sound_rabbit_mq.connection.default');
354354
$this->assertEquals((string) $definition->getArgument(1), 'old_sound_rabbit_mq.channel.default_client');
355355
$this->assertEquals(
356-
array(array('initClient', array())),
356+
array(array('initClient', array(true))),
357357
$definition->getMethodCalls()
358358
);
359359
$this->assertEquals('%old_sound_rabbit_mq.rpc_client.class%', $definition->getClass());

0 commit comments

Comments
 (0)