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.
1 2 3 4 | 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
1 | activemq/bin/activemq |
Don’t close this terminal, open a new terminal for running below commands
.
Step3: Check running status of ActiveMq
1 2 | netstat -an|grep 61616 ps -ef|grep activemq |
ps command will show the process id (PID) of running ActiveMQ
.
Step4: ActiveMQ Web Admin interface
1 | 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
1 | 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.
1 | 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:
1 2 3 | <managementContext>
<managementContext createConnector="true"/>
</managementContext> |
Now you can use below command for starting Apache ActiveMQ, stopping it and listing the connected brokers.
1 2 3 | 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
1 | 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:
1 2 3 | <transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616"/>
</transportConnectors> |
to
1 2 3 | <transportConnectors> <transportConnector name="stomp" uri="stomp://localhost:61613"/> </transportConnectors> |
.
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
1 | mysql> create database activemq; |
1 | 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:
1 2 3 | <persistenceAdapter>
<kahaDB directory="${activemq.base}/data/kahadb"/>
</persistenceAdapter> |
to
1 2 3 4 5 6 7 8 9 10 11 | <persistenceAdapter>
<journaledJDBC journalLogFiles="5" dataDirectory="${activemq.base}/activemq-data" dataSource="#mysql-ds"/>
</persistenceAdapter>
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
<property name="username" value="username"/>
<property name="password" value="password"/>
<property name="poolPreparedStatements" value="true"/>
</bean> |
.
Start the ActiveMQ and check the MySql
1 2 3 4 5 6 7 8 9 | 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
1 | 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
1 | extension=stomp.so |
Restart httpd service like below:
1 | 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
————————————————————————————
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $queue1 = '/queue/queue1'; try { $stomp = new Stomp('tcp://localhost:61613'); } catch(StompException $e) { die('Connection failed: ' . $e->getMessage()); } for($i=1;$i<10;$i++) { $msg1 = "queue one my data".$i; $stomp->send($queue1, $msg1, array('persistent' => 'true')); } unset($stomp); ?> |
In above code, I have generated a few messages via loop. In real senerio, it will be generated by events whenever something need to be processed in queue.
consumer.php
————————————————————————————
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php $queue1 = '/queue/queue1'; try { $stomp = new Stomp('tcp://localhost:61613'); } catch(StompException $e) { die('Connection failed: ' . $e->getMessage()); } $stomp->subscribe($queue1); while(1) { $frame = $stomp->readFrame(); if ( $frame != null) { echo $frame->body; //process your message echo "\n"; $stomp->ack($frame); } } unset($stomp); ?> |
If you look at the code carefully, the consumption of messages are in a infinite loop. The consumer is ready to process all incoming messages.
You have to run the consumer code from your terminal like below:
1 | nohup php consumer.php > consumer.log 2>&1 & |
It will start consumer.php forever with nohup command, directs the messages in the consumer.log file.
.
Step11. Configure ActiveMQ as Unix Service
ActiveMQ should be configured as Unix Service and it should start automatically if your server restarts.
Create ActiveMQ startup script /etc/init.d/activemqstart.sh with the following content:
1 2 3 | #!/bin/bash export JAVA_HOME=/usr /usr/share/php/activemq/bin/activemq & |
Create ActiveMQ stop script /etc/init.d/activemqstop.sh with the following content:
1 2 3 | #!/bin/bash export JAVA_HOME=/usr /usr/share/php/activemq/bin/activemq-admin stop |
Create ActiveMQ Linux service configuration script /etc/init.d/activemq with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/bin/bash
#
# activemq Starts ActiveMQ.
#
# chkconfig: 345 88 12
# description: ActiveMQ is a JMS Messaging Queue Server.
### BEGIN INIT INFO
# Provides: $activemq
### END INIT INFO
# Source function library.
. /etc/init.d/functions
[ -f /etc/init.d/activemqstart.sh ] || exit 0
[ -f /etc/init.d/activemqstop.sh ] || exit 0
RETVAL=0
umask 077
start() {
echo -n $"Starting ActiveMQ: "
daemon /etc/init.d/activemqstart.sh
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down ActiveMQ: "
daemon su -c /etc/init.d/activemqstop.sh activemq
echo
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $? |
Enable ActiveMQ service configuration as Linux Daemon:
1 2 3 4 5 | chmod +x /etc/init.d/activemqstart.sh chmod +x /etc/init.d/activemqstop.sh chmod +x /etc/init.d/activemq /sbin/chkconfig --add activemq /sbin/chkconfig activemq on |
Restart the server.
1 | reboot |
All done, Enjoy!
Cheers!!


Comments
I can not agree with you in 100% regarding some thoughts, but you got good point of view.
Hi
its really nice and comprehensive tutorial regarding message queue. Can you please elaborate some possible usage of this in web projects. what advantages we will have after this implementation and what benefits we can get ??
Nice & easy article about ActiveMQ…. no fuss at all
Hey, this is really helpful. great article.
I will suggesst to use MySql for Data persistance and start ActiveMQ as unix service.
Great post, thanks for sharing this idea
yes thanks for the indepth layout.. thumbs up
your server and it’s web accessible admin interface really makes administrator’s life easy. It can be
Well, that was a delightful reading!!
This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent wor
You made some good points there. I did a search on the subject matter and found most people will agree with your blog.
I’ve gotta say it is pretty evident you know whats up.I’m going to be tweeting this to my followers as I think it’s pretty important.I’d recommend writing additional posts like this one as it’s one of your best.I’ll be back for sure, excellent stuff.
Thanks a lot for sharing this information with us all, it has really helped me that’s for sure.
great first time visiting here i bookmarked thanks!
Merit for this write-up, I will certainly attach this website to my rss feeds, a buddy basically told me concerning this a week ago. this may be the best..
Good post, I just emailed this to a friend who was doing a little research on this. Thanks again.
Your article waswhat I was looking for. Overall I think this is well worth a bookmark, thanks
As always, you can read in the very interesting content. Way to go, and I hope readers.
Great! I know what I stand!
How to build this content? Do you have a lot of experience in this topic? Do you base solely on the theory?
I am not really good at this, do I need it if I have a VPS hosting package?
thank you for this, this is some really interesting stuff
By far the most succinct and also up-to-date info I found about this subject matter. Positive happy that I stumbled upon your write-up by likelihood. I will likely be signing up for the rss feed so as I’ll obtain the newest posts. Recognize every thing right here.
I didn’t rather understand this when I first read it. Merely when I went through it a 2nd time, it all became perfect. Thanks for the perception. Absolutely thing to remember around.