Apache ActiveMQ is one good option for implementing message queue in your PHP application. It can be easily installed on your server and it’s web accessible admin interface really makes administrator’s life easy. It can be easily connected with PHP via STOMP. I will suggesst to use MySql for Data persistance and start ActiveMQ as unix service.
Basic requirements: java, php, mysql.
So lets starts with installation steps:
Step 1: Apache ActiveMQ installation
I am installing ActiveMQ in /usr/share/php directory.
cd /usr/share/php wget http://mirror.its.uidaho.edu/pub/apache/activemq/apache-activemq/5.3.0/apache-activemq-5.3.0-bin.tar.gz tar zxvf apache-activemq-5.3.0-bin.tar.gz mv apache-activemq-5.3.0 activemq
We have renamed the directory to “activemq” for ease.
.
Step 2: Run Apache ActiveMq
activemq/bin/activemq
Don’t close this terminal, open a new terminal for running below commands
.
Step3: Check running status of ActiveMq
netstat -an|grep 61616 ps -ef|grep activemq
ps command will show the process id (PID) of running ActiveMQ
.
Step4: ActiveMQ Web Admin interface
http://localhost:8161/admin/
Here you can monitor your current status of queues, topics, connections etc. You can also browse,
delete, add data according to requirement.
.
Step5: Stop Apache AcitveMQ
kill -9 [PID]
where [PID] is the process id of the ActiveMQ process. Take it from ps command
.
Step6: ActiveMQ admin command
ActiveMQ is taking configuration details via simple xml (default:activemq.xml) file located in conf directory.
vi activemq/conf/activemq.xml
Make a small change in activemq.xml and your activemq-admin command will start working
Change the default createConnector=”false” to createConnector=”true” like below:
Now you can use below command for starting Apache ActiveMQ, stopping it and listing the connected brokers.
activemq/bin/activemq-admin start activemq/bin/activemq-admin stop activemq/bin/activemq-admin list
.
Step7: Configure STOMP transport connector, so that your PHP application can communicate with ActiveMQ
vi activemq/conf/activemq.xml
The default transport connector is openwire for native connectivity to Java and it will be available at tcp port 61616.
Change below code:
to
.
Step8: Data Persistence with MySql
Download latest MySQL Java Connector (mysql-connector-java-5.0.6-bin.jar) from MySql website.
Copy the file into “activemq/lib/optional” folder
Launch the MySQL command line program and create a database for ActiveMQ
mysql> create database activemq;
vi activemq/conf/activemq.xml
The default persistence adapter is kahaDB which is fast but not very reliable. You can test it according to your condition or give MySql a change and take the better one.
Change below code:
to
.
Start the ActiveMQ and check the MySql
mysql> use activemq; show tables; Database changed +--------------------+ | Tables_in_activemq | +--------------------+ | ACTIVEMQ_ACKS | | ACTIVEMQ_LOCK | | activemq_msgs | +--------------------+
Your MySql data persistence is working fine.
.
Step9. Installing PHP STOMP client
pecl install stomp-beta
It will generate a stomp.so in /usr/lib/php/modules/ directory. Open /etc/php.ini and load the extension like below
extension=stomp.so
Restart httpd service like below:
service httpd restart
.
Step10. PHP message queue producer and consumer code
Now you have to make two php sample files, one for producting messages in queue and other for consuming those messages.
producer.php
————————————————————————————