JMS1.1 Domain Unification Sample

This document consists of following sections:

Overview of the Sample Application 

Technology Overview

Java Messaging Service (JMS), a part of J2EE specification provides standard way of enterprise messaging between distributed components. It supports both the point-to-point and publisher-subscriber messaging domains using the queues and topics respectively.

JMS 1.0.2b provided standard interfaces for communicating with queues and topics. But the two domains were kept completely separate each having its own set of interfaces. For example, to access a queue one had to use interfaces like QueueConnectionFactory, QueueConnection, QueueSession, QueueSender, QueueReceiver, Queue etc. Similarly one needed to use interfaces like TopicConnectionFactory, TopicConnection, TopicSession, TopicPublisher, TopicSubscriber to send and receive messages from topic. These API's lacked the reusability in that they could not work equally well with either domain.

JMS 1.1, a part of J2EE 1.4 specification, provides for the unification of the two domains. It provides a common set of interfaces to communicate with both the queues and topics. The common interfaces provided are Destination, ConnectionFactory, Session, MessageProducer, MessageConsumer etc. Thus a client need not be implemented for a specific type of destination. The advantage is that the APIs are simplified and developers can write more reusable code.

An inherent advantage achieved through domain unification is that the queues and topics can now be accessed from within the same transaction using the same Session. This was not possible with previous versions of JMS as topic could be accessed using TopicSession and queue could be accessed using the QueueSession but there was no way to access both from the same session. Now with JMS 1.1, it is possible to receive message from either of the destination and send it to the other destination in the same transaction. This feature can be of significant importance to applications that require transaction capabilities so that the messages are either sent(received) or are not sent (received) to both the destinations.

Application Overview

The sample uses in memory JMS queue and topic configured in the OC4J 10g. The sample provides two Java classes namely JMSProducerConsumer.java and AsyncClient.java. JMSProducerConsumer class features the use of JMS 1.1 domain unification interfaces to communicate with both the queue and topic. The class accepts input message from the user and posts it to the in memory queue. Within the same transaction, it dequeues the message from the queue and posts it to the topic.

The sample's AsyncClient.java class serves as a listener to the topic. It implements the javax.jms.MessageListener interface and defines the onMessage() method to receive messages asynchronously from the topic. The class looks up the topic on OC4J and sets itself as the message listener to the topic. The class also extends java.lang.Thread class to keep itself alive and running so that it can listen to the messages from topic. The class prints out any messages received from the topic on console.

Required Software 

Back to Top

  • Oracle Application Server (OC4J) Version 10g  or later ( Developer Preview or Production ).
  • JDK 1.4.x or later available here.
  • Apache Ant v1.5 or later.
  •    

Configuring and Running the sample 

Back to Top

Execute the following steps to run the sample. In the steps, J2EE_HOME refers to <oc4j10g_install_dir>/j2ee/home directory and SAMPLE_HOME refers to directory where sample is extracted.

Step 1:

Extract the sample source files as follows:

>jar xvf JMSSample.jar

The sample files are extracted into the JMS1.1 directory. Click here to view the directory structure and description of sample files.

Step 2: Edit SAMPLE_HOME/src/oracle/otnsamples/jms/ConnectionParams.java file to specify the connection parameters of the OC4J you are using.
Step 3:

Edit J2EE_HOME/config/jms.xml to configure the sample queue and topic by adding following lines within <jms-server> tag :

 <queue name="Sample Queue" location="jms/sampleQueue">
<description>A sample queue</description>
</queue>

<topic name="Sample Topic" location="jms/sampleTopic">
<description>A sample topic</description>
</topic>

<connection-factory location="jms/sampleConnectionFactory"/>
Step 4:

Navigate to J2EE_HOME on command prompt and start the OC4J as follows:

java -jar oc4j.jar
Step 5:

Navigate to SAMPLE_HOME directory on a new command prompt. Include ANT_HOME/bin in the PATH environment variable. Set the following properties required by the ant build script

  • Set the OC4J_HOME environment variable to your <OC4J> root directory.
    Example:

    Windows       : set OC4J_HOME=c:\oc4j10g
    Linux/Unix    : OC4J_HOME=/home/oc4j10g
                    export OC4J_HOME
  • Set the JAVA_HOME environment variable to your Java installation directory.
    Example:

    Windows       : set JAVA_HOME=c:\j2sdk1.4
    Linux/UNIX    : JAVA_HOME=/home/j2sdk1.4
                    export JAVA_HOME
Step 6:

Execute following ant task to compile the source classes and run the topic listener class.

>ant 
>ant listenTopic
Step 7:

Navigate to SAMPLE_HOME directory on a new command prompt and perform Step 5. Execute the following ant task to run JMSProducerConsumer.java class and post message to the queue:

>ant postMessage
Provide the input message. The message will be sent to the queue. A message consumer receives the message from queue and posts it to the topic in the same transaction. Check back the prompt window in Step 6 to see if the message from the topic is received.

 
 

Description of Sample Files 

Back to Top

Following is the directory structure of JMS 1.1 Domain Unification application. JMS1.1 is the sample's top level directory.

Directory File Description
/doc Readme.html This file
/src/oracle/otnsamples/jms
JMSProducerConsumer.java This class is a producer and consumer of messages for JMS queue and topic.
AsyncClient.java This class is a listener to the topic.
ConnectionParams.java This class defines the parameters to connect to OC4J.
/ builld.xml The ANT config file that defines the ant tasks to compile and run the sample.

Please enter your comments about this sample in the OTN Sample Code Discussion Forum.

E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy