Skip to content

Commit

Permalink
Updated Readme to prepare for 0.4.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyholm committed May 5, 2016
1 parent ed8a593 commit b187f03
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## From 0.3.1 to 0.4.0

* Fixed memory leaks
* New message syntax, we do not longer use HTTP-like messages. We use a Json syntax instead.
* We do no longer accept any arguments when running the application. We only use system properties.

## From 0.3.0 to 0.3.1

* The data dispached will be Base64 **and** URL encoded. This will fix the bug where PHP interpets a Base64 endcoded string as two strings becuase it contains a plus sign (+).
Expand Down
79 changes: 40 additions & 39 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Message queue 2 PHP

This Java application pull data from a message queue and give the message to PHP by using PHP-FPM. It could be used as a worker for [SimpleBus](https://github.com/SimpleBus) ascynchronous messages.

You will information about how this worker should work on the [Happr Developer blog](http://developer.happyr.com/real-asynchronous-events-with-symfony2)
This Java application pull data from a message queue and give the message to PHP by using PHP-FPM. It could be used as a worker for [SimpleBus](https://github.com/SimpleBus) asynchronous messages.

## Installation

Expand All @@ -18,7 +16,7 @@ There is some configuration you might want to consider when starting the worker.

### executor

How do you want to execute the php job? Do you want to do it with PHP-FPM (fastcgi) or PHP cli (shell command).
How do you want to execute the php job? Do you want to do it with PHP-FPM (fastcgi) or PHP cli (shell) command. Use PHP-FPM for best performance.

```bash
java -Dexecutor=fastcgi -jar mq2php.jar
Expand Down Expand Up @@ -51,61 +49,64 @@ You can subscribe to different queues with different names. You should separate
java -DmessageQueue=rabbitmq -DqueueNames=foo,bar,baz -jar mq2php.jar
```

These topics will be evenly distributed over the worker threads.
These queues will be evenly distributed over the worker threads.

### Number of worker threads
### threads

As default there is 5 threads listening to the queue. These threads are waiting for a response from the PHP script. If
you are planning to have several long running script simultaneously you may want to increase this. Usually you don't need
to bother.
The number of threads you want to use for each queue. As default there ara 3 threads listening to each queue. These
threads are waiting for a response from the PHP script. If you are planning to have several long running script
simultaneously you may want to increase this. Usually you don't need to bother.

```bash
java -jar mq2php.jar 5
java -jar mq2php.jar -Dthreads=3
```

## Message headers

The message should contain some headers to tell the worker what is should do. The message and the header look a lot
like the HTTP protocol. This is an example message:
The message should contain some headers to tell the worker what is should do. The message is a json object. This is an example message:

```js
{
"headers":[
{
"key":"fastcgi_host",
"value":"localhost"
},
{
"key":"fastcgi_port",
"value":9000
},
{
"key":"dispatch_path",
"value":"\/absolute\/path\/bin\/dispatch-message.php"
},
{
"key":"php_bin",
"value":"\/usr\/local\/Cellar\/php70\/7.0.4\/bin\/php"
}
],
"body":"this body is not of any interest for mq2php."
}
```

```bash
php_bin: /usr/local/bin/php
dispatch_path: /Users/tobias/Workspace/Symfony/app/../bin/dispatch.php
fastcgi_host: localhost
fastcgi_port: 9000
queue_name: foo
When the message is sent to PHP it will contain one extra header. It will be the `queue_name` header that contains the
name of the queue.

TzozOToiU3ltZm9ueVxDb21wb25lbnRcRXZlbnREm9ueVxDb21wb25lbnRcRXZlbnREaXNwYXRjRXZlbnREm9ueVxDb21wb25lbnRcRXZlbnRE
```

### Shell Executor

These headers must exist when you are using the Shell executor.

#### php_bin

This should be a path to the php executable.

#### queue_name

This header is populated once the message has been pulled from the queue. You could use this value in the dispatcher.
* php_bin - This should be a path to the php executable.
* dispatch_path - The absolute path to the dispatch.php. This is normally /path/to/symfony/bin/dispatch.php.

### PHP-FPM (fastcgi)

These headers must exist when you are using the FastCGI executor.

#### fastcgi_host

If you are using PHP-FPM you have to specify a host to connect to. This is normally "localhost".

#### fastcgi_port

The port that we should use together with **fastcgi_host**.

#### dispatch_path

The absolute path to the dispatch.php. This is normally /path/to/symfony/bin/dispatch.php. The path should not be a symbolic link.

* fastcgi_host - If you are using PHP-FPM you have to specify a host to connect to. This is normally "localhost".
* fastcgi_port - The port that we should use together with **fastcgi_host**.
* dispatch_path - The absolute path to the dispatch.php. This is normally /path/to/symfony/bin/dispatch.php.

## Contribute

Expand Down
18 changes: 5 additions & 13 deletions dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set_time_limit(0);


/*
* Look both in $_SERVER, $_POST and $argv after some data.
*/
Expand All @@ -22,19 +23,10 @@
exit(1);
}


$message = base64_decode($data);
$headers = array();
$body = null;
$lines = explode("\n", $message);
foreach ($lines as $i=>$line) {
if ($line == '') {
$body = $lines[$i+1];
break;
}
list($name, $value) = explode(':', $line, 2);
$headers[$name]= trim($value);
}
// Decode the message and get the data
$message = json_decode($data, true);
$headers = $message['headers'];
$body = $message['body'];

//$headers is an array with headers
//$body is the content of the message

0 comments on commit b187f03

Please sign in to comment.