<document>
<localtoc/> <deftable>
File Description
resin-web.xml configuration
PHP script sending the message.
Java message bean listener receiving the message.
Singleton service bean storing the received messages.
Servlet displaying the contents of the message store.
</deftable>

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 on sending messages.

In this example, the script checks a POST variable "message" and if it is set, sends the value of that variable to a JMS queue. A Message Driven Bean (MDB) receives these messages and records them. The record is displayed by a servlet.

<?php if (array_key_exists("message", $_POST)) { $queue = java_bean("Queue"); if (! $queue) { echo "Unable to get message queue!\n"; } else { if ($queue->offer($_POST["message"]) == TRUE) { echo "Successfully sent message '" . $_POST["message"] . "'"; } else { echo "Unable to send message '" . $_POST["message"] . "'"; } } } ?>

The programming model of the Quercus JMS interface is first to get access to the queue using the `java_bean()` call. `java_bean` will look for the named bean in the resin-web.xml, in this case our queue. Since the Queue implements the `java.util.concurrent.BlockingQueue` API, the PHP script can send data to the queue directly using `offer()` and receive messages with `poll()`.

JMS requires that two resources be set up: A `ConnectionFactory` and a `Queue`. Both are configured in . The `ConnectionFactory` is used to connect to all the `Queue`s and only one of them needs to be set up.

<web-app xmlns="http://caucho.com/ns/resin"> <jms-connection-factory uri="resin:"/> </web-app>

The example uses the queue named `Queue`.

<web-app xmlns="http://caucho.com/ns/resin"> <jms-queue name="Queue" uri="memory:"/> </web-app>

The complete configuration is in .

</document>