Using JMS with PHP
Introduction
NOTE: This article was originally published in 2007 and since then has not been maintained. Also note that since then I had stopped using stomp due to many problems with ActiveMQ locking up and not notifying client of messages properly. I switched to using the Jboss message queue server instead. I use SOAP to communicate between PHP and Java and then use a Java client to do the actual communication. Even tough this has additional work and overhead I had not found any other reliable way to use message queuing in PHP.
JMS stands for Java Messaging Service. It allows for enterprise apps to create, send, and receive messages in a loosely coupled, reliable and asynchronous manner. A typical JMS installation requires a JMS broker which hosts the message queues or topics to which Java applications can subscribe.
PHP is a popular server-side scripting language which enables fast and easy web site development. However, it is not limited to just web-site development. It can be compiled with a CLI interface and used for command line application development.
This document will discuss integrating PHP applications with ActiveMQ, a fast, popular open source JMS server using the Stomp protocol.
Related Articles
Around October 2004, OnJava.com put up an article about using PHPMQ and MantaRay for adding JMS functionality to PHP [link->]. PHPMQ is a layer which uses the PHP-Java functions to wrap the Java implementation of a JMS API and expose it in PHP. This is a good approach to use but it suffers from the overhead of multiple JNI calls and also uses the experimental PHP-Java api [link->].
ActiveMQ
Overview of JMS
Introducing STOMP
Examples
1. Simple PHP Producer
Description: xyz
<?
require_once( "/usr/home/kraman/activemq/libs/Stomp.php" );
$conn = new StompConnection( “onp.dev.farheap.com” );
$res = $conn->connect( “kraman” , “pass” );
print_r( $res );
for( $i=0 ; $i < 10000 ; $i ++ ){
print “Sending msg id ${i}\n”;
$res = $conn->send( “/queue/q1″ , “Message from PHP. ID: ${i}”);
print_r( $res );
sleep(1);
}
$conn->disconnect();
?>
Output:
dklaksd
dasdasd
dasdasd
sadasd
as
2. Simple PHP Consumer
<?
require_once( "/usr/home/kraman/activemq/libs/Stomp.php" );
$conn = new StompConnection( “onp.dev.farheap.com” );
$res = $conn->connect( “kraman” , “pass” );
print_r( $res );
$res = $conn->subscribe( “/queue/q1″ );
while( true ){
$res = $conn->readFrame();
print_r( $res );
sleep(1);
$conn->acknowledge( $res->headers["message-id"] );
}
$c->disconnect();
?>
3. Binary Message Producer
<?
require_once( "/usr/home/kraman/activemq/libs/Stomp.php" );
$conn = new StompConnection( “onp.dev.farheap.com” );
$res = $conn->connect( “kraman” , “pass” );
print_r( $res );
for( $i=0 ; $i < 100 ; $i ++ ){
$data = file_get_contents( “/dev/urandom” , false , NULL , 0 , 10 );
$msgHeader = array();
$msgHeader["content-length"] = 10;
print “Sending msg ${i}. MD5: ” . md5($data) . “\n”;
$res = $conn->send( “/queue/q2″ , $data , $msgHeader );
print_r( $res );
sleep(1);
}
$conn->disconnect();
?>
4. Producing messages with piorities
<?
require_once( "/usr/home/kraman/activemq/libs/Stomp.php" );
$conn = new StompConnection( “onp.dev.farheap.com” );
$res = $conn->connect( “kraman” , “pass” );
print_r( $res );
for( $i=0 ; $i < 10000 ; $i ++ ){
$priority = rand( 0 , 10);
$msgHeader = array();
$msgHeader["priority"] = $priority;
print “Sending msg id ${i} with priority $priority\n”;
$res = $conn->send( “/queue/q1″ , “Message from PHP. ID: ${i}” , $msgHeader );
print_r( $res );
sleep(1);
}
$conn->disconnect();
?>
5. Consuming messages with priorities (Message selectors)
<?
require_once( "/usr/home/kraman/activemq/libs/Stomp.php" );
$conn = new StompConnection( “onp.dev.farheap.com” );
$res = $conn->connect( “kraman” , “pass” );
print_r( $res );
$header = array();
$header["selector"] = “JMSPriority <= 5″;
$res = $conn->subscribe( “/queue/q1″ , $header );
while( true ){
$res = $conn->readFrame();
print_r( $res );
sleep(1);
$conn->acknowledge( $res->headers["message-id"] );
}
$c->disconnect();
?>
1 Trackback or Pingback for this entry
November 4th, 2009 on 2:39 am
[...] Using JMS with PHP [...]