How to create a simple JMS queue, producer and consumer in Oracle 10g

Posted by Steve Racanovic | Posted in , | Posted on 3:19 PM

0

This demonstration shows how to create a simple jms producer and consumser using Oracle 10g as the JMS Provider.

I am using Oracle AS 10.1.3.2.0 and JDev 10.1.3.2.0 in this example. This article assumes you have installed and configure the environment for both applications.

1. Once you have your application server running, first create a new oc4j instance for the application and start the instance.

[sracanov@sracanov-au D]$ createinstance -instanceName JMS
Creating OC4J instance "JMS"...
Set OC4J administrator password for "JMS" (password text will not be displayed as it is entered):
Enter password:
Confirm password:
A new OC4J instance "JMS" is created in the group "default_group".

[sracanov@sracanov-au D]$ opmnctl startproc process-type=JMS
opmnctl: starting opmn managed processes...

2. Create a JMS Destination.

Our new JMS instance will already have some default JMS destination configured that we could use. See http://download-west.oracle.com/docs/cd/B25221_04/web.1013/b14427/jms.htm#i1084454

However in this example we will create our own in-memory JMS destination.

First let's determine the opmn port:

[sracanov@sracanov-au D]$ opmnctl status -port
sracanov-au:6007

Now create a new JMS destination:

[sracanov@sracanov-au D]$ cd /D %ORACLE_HOME\j2ee\JMS

[sracanov@sracanov-au D]$ java -jar admin_client.jar deployer:oc4j:opmn://localhost:6007/JMS oc4jadmin welcome1 -addDestination -domain queue -name myfirstqueue -jndiLocation jms/MyFirstQueue

3. Create a Connection Factory.

Our new JMS instance will already have some default connection factory configured that we could use. See http://download-west.oracle.com/docs/cd/B25221_04/web.1013/b14427/jms.htm#i1084454

However in this example we will create our connection factory
[sracanov@sracanov-au D]$ java -jar admin_client.jar deployer:oc4j:opmn://localhost:6007/JMS oc4jadmin welcome1 -addJMSConnectionFactory -domain queue -jndiLocation jms/MyFirstQCF

4. In Jdev, now create a simple producer and consumer.

a) Create a new Application and Project.

b) Create a simple producer class that will send 5 messages to the queue.


package project1;

import javax.jms.*;
import javax.naming.*;
import java.util.Hashtable;

public class QueueProducer {

public static void main(String[] args) {
String queueName = "jms/MyFirstQueue";
String queueConnectionFactoryName = "jms/MyFirstQCF";
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueSender queueSender = null;
TextMessage message = null;
int noMessages = 5;

/*
* Set the environment for a connection to the OC4J instance
*/
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");
env.put(Context.PROVIDER_URL, "opmn:ormi://localhost:6007:JMS/default");

/*
* Set the Context Object.
* Lookup the Queue Connection Factory.
* Lookup the JMS Destination.
*/
try {
jndiContext = new InitialContext(env);
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup(queueConnectionFactoryName);
queue = (Queue) jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " +
e.toString());
System.exit(1);
}

/*
* Create connection.
* Create session from connection.
* Create sender.
* Create text message.
* Send messages.
* Send non text message to end text messages.
* Close connection.
*/
try {
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < noMessages; i++) {
message.setText("Message " + (i + 1));
System.out.println("Producing message: " +
message.getText());
queueSender.send(message);
}
queueSender.send(queueSession.createBytesMessage());;
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
System.out.println("Closing error: " + e.toString());
}
}
}
}
}

c) Create a simple consumer class that will read all text messages from the queue and end once it reads a non text message:

package project1;

import java.util.Hashtable;

import javax.jms.*;

import javax.naming.*;

public class QueueConsumer {

public static void main(String[] args) {
String queueName = "jms/MyFirstQueue";
String queueConnectionFactoryName = "jms/MyFirstQCF";
Context jndiContext = null;
QueueConnectionFactory queueConnectionFactory = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue = null;
QueueReceiver queueReceiver = null;
TextMessage message = null;

/*
* Set the environment for a connection to the OC4J instance
*/
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"oracle.j2ee.rmi.RMIInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "oc4jadmin");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");
env.put(Context.PROVIDER_URL,
"opmn:ormi://localhost:6007:JMS/default");

/*
* Set the Context Object.
* Lookup the Queue Connection Factory.
* Lookup the JMS Destination.
*/
try {
jndiContext = new InitialContext(env);
queueConnectionFactory =
(QueueConnectionFactory)jndiContext.lookup(queueConnectionFactoryName);
queue = (Queue)jndiContext.lookup(queueName);
} catch (NamingException e) {
System.out.println("JNDI lookup failed: " + e.toString());
System.exit(1);
}

/*
* Create connection.
* Create session from connection.
* Create receiver.
* Receive all text messages from queue until non text message is receivied
* Close connection.
*/
try {
queueConnection = queueConnectionFactory.createQueueConnection();
queueSession =
queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueReceiver = queueSession.createReceiver(queue);
queueConnection.start();
while (true) {
Message m = queueReceiver.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage)m;
System.out.println("Consuming message: " +
message.getText());
} else {
break;
}
}
}
} catch (JMSException e) {
System.out.println("Exception occurred: " + e.toString());
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
System.out.println("Closing error: " + e.toString());
}
}
}
}
}

d) Add the following libraries to the project:
* J2EE
* Apache Ant

5) Run the QueueProducer class to send the message to the queue.

The is result:
Producing message: Message 1
Producing message: Message 2
Producing message: Message 3
Producing message: Message 4
Producing message: Message 5

6) Run the QueueConsumer class to receive the message from the queue.

This is the result:

Consuming message: Message 1
Consuming message: Message 2
Consuming message: Message 3
Consuming message: Message 4
Consuming message: Message 5