JMS Messaging in Quercus - Sending messages
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Config
SwiftMQ
Tutorials

Simple Listener
JCA Listener
JMS in Quercus - Sending messages
JMS in Quercus - Receiving messages
JCA Listener
Tutorials
JMS in Quercus - Receiving messages

Find this tutorial in: /usr/local/resin/webapps/resin-doc/jms/tutorial/php-queue-send
Try the Tutorial

  1. Files in this tutorial
  2. Using JMS in Quercus
  3. Sending JMS messages from a PHP script
  4. Configuring JMS for PHP and Java

Files in this tutorial

WEB-INF/resin-web.xml resin-web.xml configuration
send-message.php PHP script sending the message.
WEB-INF/classes/example/JMSLogger.java Java listener receiving the message.
WEB-INF/jmslogger.ejb EJB Message bean configuration.

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

Sending JMS messages from a PHP script

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.

if (array_key_exists("message", $_POST)) {
  $queue = new JMSQueue("jms/Queue");

  if (! $queue) {
    echo "Unable to get message queue!\n";
  } else {
    if ($queue->send($_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 create a connection to a queue by instantiating a JMSQueue object. To create a JMSQueue object, pass in the JNDI name of the JMS queue to be used. JMSQueue objects have two methods: send() and receive(). This example shows that using send() is as simple as passing in a PHP value.

Configuring JMS for PHP and Java

JMS requires that two resources be set up: A ConnectionFactory and a Queue. Both are configured in WEB-INF/resin-web.xml. The ConnectionFactory is used to connect to all the Queues and only one of them needs to be set up. The default JNDI name is jms/ConnectionFactory - Quercus automatically uses this connection factory. Another connection factory may be used by setting the PHP ini variable jms.connection_factory.

  <resource jndi-name="jms/ConnectionFactory"
    type='com.caucho.jms.ConnectionFactoryImpl' />

The example above uses the queue jms/Queue.

  <resource jndi-name="jms/Queue"
    type='com.caucho.jms.memory.MemoryQueue' />

The complete configuration is in WEB-INF/resin-web.xml.

Try the Tutorial


JCA Listener
Tutorials
JMS in Quercus - Receiving messages
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.