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.
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 :
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.