Caucho Technology
  • resin 4.0
  • jms messaging in quercus - sending messages


    Demo

    Files in this tutorial

    FILEDESCRIPTION
    WEB-INF/resin-web.xmlresin-web.xml configuration
    WEB-INF/classes/META-INF/beans.xmlJava Injection marker file to start bean scanning
    send-message.phpPHP script sending the message.
    WEB-INF/classes/example/MyListener.javaJava message bean listener receiving the message.
    WEB-INF/classes/example/MessageStore.javaSingleton service bean storing the received messages.

    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.

    Example: PHP sending script
    <?php
    
    if (isset($_POST["message"])) {
      $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().

    Configuring JMS for PHP and Java

    JMS requires two resources: A Queue and a ConnectionFactory. 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.

    MemoryQueue configuration

    The example uses the queue named Queue.

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

    ConnectionFactory configuration

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

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

    Message Bean configuration

    Example: MyListener configuration in resin-web.xml
    <web-app xmlns="http://caucho.com/ns/resin"
               xmlns:resin="urn:java:com.caucho.resin">
    
      <ejb-message-bean class="example.MyListener">
        <destination>#{Queue}</destination>
      </ejb-message-bean>
    
    </web-app>
    

    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.