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 ascynchronous messages.
You will information about how this worker should work on the Happr Developer blog
Download the jar file and put it somewhere like /opt/mq2php/mq2php.jar. You might want to use a init script. See this file for a template. You do also need to install a message queue like Rabbit MQ.
Remember to protect your queue. If someone unauthorized could write to your queue he will get the same permission to execute programs as PHP does. Make sure you restrict access on the queue to localhost or make sure you know what you are doing.
There is some configuration you might want to consider when starting the worker.
How do you want to execute the php job? Do you want to do it with PHP-FPM (fastcgi) or PHP cli (shell command).
java -Dexecutor=fastcgi -jar mq2php.jar
Possible values are:
- fastcgi
- shell
What message queue system do you want to use?
java -DmessageQueue=rabbitmq -jar mq2php.jar
Possible values are:
- rabbitmq
When you are using rabbitmq we will connect to localhost with the official rabbit mq client library.
You can subscribe to different queues with different names. You should separate names by a comma.
java -DmessageQueue=rabbitmq -DqueueNames=foo,bar,baz -jar mq2php.jar
These topics will be evenly distributed over the worker 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.
java -jar mq2php.jar 5
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:
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
TzozOToiU3ltZm9ueVxDb21wb25lbnRcRXZlbnREm9ueVxDb21wb25lbnRcRXZlbnREaXNwYXRjRXZlbnREm9ueVxDb21wb25lbnRcRXZlbnRE
These headers must exist when you are using the Shell executor.
This should be a path to the php executable.
This header is populated once the message has been pulled from the queue. You could use this value in the dispatcher.
These headers must exist when you are using the FastCGI executor.
If you are using PHP-FPM you have to specify a host to connect to. This is normally "localhost".
The port that we should use together with fastcgi_host.
The absolute path to the dispatch.php. This is normally /path/to/symfony/bin/dispatch.php. The path should not be a symbolic link.
If you want to make changed and compile the application your self you can do so with:
mvn clean compile assembly:single
java -Dexecutor=shell -DqueueNames=asynchronous_commands,asynchronous_events -jar target/mq2php-0.4.0-SNAPSHOT-jar-with-dependencies.jar