Caucho Technology

jms messaging in quercus - receiving messages


Demo

Files in this tutorial

FILEDESCRIPTION
WEB-INF/resin-web.xmlresin-web.xml configuration
display-ad.phpPHP script displaying the advertisement.
WEB-INF/classes/example/AdProducer.javaJava listener that fills the advertisement queue.

Using JMS in Quercus

Quercus offers a simplified messaging interface built upon JMS. This functionality makes it possible to send and receive messages using either the Resin JMS implementation or any other messaging service with a JMS implementation. Many features of JMS are designed for message-driven services which make sense in the Java world, but are not appropriate for PHP. This tutorial focuses receiving messages in a non-blocking way.

Receiving JMS messages from a PHP script

This example uses two queues: an "ad queue" and a "control queue". The PHP script removes advertisements from the ad queue using thepoll()method. This method isnon-blocking- if there are no advertisements, the method will returnFALSEinstead of waiting for a new advertisement. Whenever the PHP script removes an advertisement from the ad queue, it signals a Java message driven bean (MDB) to add another ad by sending an empty message to the control queue.

$ad_queue = java_bean("AdQueue");
$control_queue = java_bean("ControlQueue");

if (! $ad_queue) {
  echo "Unable to get ad queue!\n";
} elseif (! $control_queue) {
  echo "Unable to get control queue!\n";
} else {
  $ad = $ad_queue->poll();

  if ($ad == null) {
    echo "No ads available on the queue\n";
  } else {
    echo "$ad";
  }

  if (! $control_queue->offer(0)) {
    echo "Unable to send control message\n";
  }
}

The programming model of the Quercus JMS interface is first to get access to the queue usingjava_beanto get the named queue. To create aJMSQueueobject, pass in the name of the JMS queue to be used. Since the JMS Queue implements theBlockingQueueAPI, the PHP script can useoffer()andpoll(). The example above shows how to use both methods.

Configuring JMS for PHP and Java

JMS requires that two resources be set up: AConnectionFactoryand aQueue. Both are configured inWEB-INF/resin-web.xml. TheConnectionFactoryis used to connect to all theQueues and only one of them needs to be set up.

Example: connection factory configuration in resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">

  <jms-connection-factory uri="resin:"/>

</web-app>

This example uses two queues,AdQueueandControlQueue.

Example: Queue configuration in resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin">

  <jms-queue name="AdQueue" uri="memory:"/>

  <jms-queue name="ControlQueue" uri="memory:"/>

</web-app>

The complete configuration is inWEB-INF/resin-web.xml.

Demo


Copyright © 1998-2011 Caucho Technology, Inc. All rights reserved.
Resin ® is a registered trademark, and Quercustm, Ambertm, and Hessiantm are trademarks of Caucho Technology.