Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

Commit

Permalink
add sample message to the doc
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Sep 16, 2016
1 parent 5a1fc4a commit 35ef4af
Showing 1 changed file with 103 additions and 1 deletion.
104 changes: 103 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,111 @@ $executor->execute(

### Iteration callback to get runtime information with a message queue

#### Sample Message for parent / children communication

```{.php}
<?php
namespace My\Project;
use Saxulum\MessageQueue\MessageInterface;
class SampleMessage implements MessageInterface
{
/**
* @var string
*/
private $context;
/**
* @var string
*/
private $message;
/**
* @param string $context
* @param string $message
*/
public function __construct(string $context, string $message)
{
$this->context = $context;
$this->message = $message;
}
/**
* @param string $json
*
* @return MessageInterface
*/
public static function fromJson(string $json): MessageInterface
{
$rawMessage = json_decode($json);
return new self($rawMessage->context, $rawMessage->message);
}
/**
* @return string
*/
public function toJson(): string
{
return json_encode([
'context' => $this->context,
'message' => $this->message,
]);
}
/**
* @return string
*/
public function getContext(): string
{
return $this->context;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}
```

#### Sample child command

```{.php}
#!/usr/bin/env php
<?php
use My\Project\SampleMessage;
use Saxulum\MessageQueue\SystemV\SystemVSend;
if (!isset($argv[1])) {
throw new \InvalidArgumentException('Missing key for SystemVSend');
}
if (!isset($argv[2])) {
throw new \InvalidArgumentException('Missing child id');
}
$send = new SystemVSend($argv[1]);
for ($i = 0; $i < 100; ++$i) {
$message = new SampleMessage($argv[2], sprintf('message %d', $i));
$send->send($message);
echo $message->toJson().PHP_EOL;
}
```

#### Parent command

```{.php}
<?php
use My\Project\SampleMessage;
use Symfony\Component\Process\Process;
use Saxulum\MessageQueue\SystemV\SystemVReceive;
use Saxulum\ProcessesExecutor\ProcessesExecutor;
Expand All @@ -105,7 +207,7 @@ $processes = [
$messages = [];
$receiver = new SystemVReceive(<MessageInterface::class>, 1);
$receiver = new SystemVReceive(SampleMessage::class, 1);
// make sure the queue is empty
while (null !== $message = $receiver->receive()) {}
Expand Down

0 comments on commit 35ef4af

Please sign in to comment.