Help - I am stuck! Has anyone used a TemporaryQueue on Orion?
I get an Invalid Queue Exception when trying to create a QueueReceiver
for a TemporaryQueue. The exception and the QueueTestSender and
QueueTestReceiver classes are attached below. The error occurs when I
run the QueueTestSender, which creates the TemporaryQueue and then
attempts to create a QueueReceiver for the TemporaryQueue.
I am using Orion 1.2.9 on Windows NT. This code works fine on the
Weblogic application server.
I reported it as a bug to Bugzilla today.
C:\Orion\orion\applications\OrionTempQueue\lib\java>java
OrionTempQueue.OrionTestSender
Sender: The temporary queue was assigned this name:
com.evermind.server.jms.cq@4cb029
javax.jms.InvalidDestinationException: Invalid queue
at com.evermind.server.jms.cv.createReceiver(JAX)
at com.evermind.server.jms.cv.createReceiver(JAX)
at
OrionTempQueue.OrionTestSender.initJMS(OrionTestSender.java:79)
at
OrionTempQueue.OrionTestSender.<init>(OrionTestSender.java:46)
at OrionTempQueue.OrionTestSender.main(OrionTestSender.java:176)
Receiver: Ready to receive messages
javax.jms.JMSException: QueueConnection not started
at com.evermind.server.jms.cw.send(JAX)
at com.evermind.server.jms.cw.send(JAX)
at com.evermind.server.jms.cw.send(JAX)
at
OrionTempQueue.OrionTestSender.sendRequest(OrionTestSender.java:103)
at OrionTempQueue.OrionTestSender.main(OrionTestSender.java:181)
Exception in thread "main" java.lang.NullPointerException
at
OrionTempQueue.OrionTestSender.closeJMS(OrionTestSender.java:160)
at
OrionTempQueue.OrionTestSender.sendRequest(OrionTestSender.java:109)
at OrionTempQueue.OrionTestSender.main(OrionTestSender.java:181)
*
* OrionTestSender.java
*
* Created on July 7, 2000, 2:02 PM
*
*/
package OrionTempQueue;
import javax.jms.*;
import javax.naming.*;
import java.util.*;
import java.util.Properties;
/** QueueTestSender sends a request message to a
* permanent queue, and receives a response message on a temporary
queue.
* <p>
* It creates a temporary queue for the response, then
* creates a request message with that temporary queue in the JMSReplyTo
field,
* then writes the request message to a permanent queue, and listens for
the
* response message on the temporary queue.
* <p>
* Start the Orion server first, then run QueueTestSender.
*
* @author cdull
* @version %I%, %G%
*/
public class OrionTestSender implements MessageListener{
//private boolean messageReceived;
private int messageReceived;
private Queue queue;
private QueueSender msgSender;
private QueueReceiver responseReceiver;
private QueueSession writeSession;
private QueueSession readSession;
private QueueConnection conn;
private TemporaryQueue responseQueue;
private Context ctx;
private OrionTestReceiver myReceiver;
/** Creates new QueueTestSender*/
public OrionTestSender() {
messageReceived = 0;
try {
ctx = getInitialContext();
initJMS();
}
catch (Exception e) {
e.printStackTrace();
}
myReceiver = new OrionTestReceiver();
}
/**Sets up all of the JMS stuff: connections, sessions, queues,
sender,
* and receiver, creates a temporary queue for the response messages,
puts
the
* name of the temporary queue in the JMSReplyTo field of the request
* message, and sends the request message on the permanent queue
*/
private void initJMS () {
try {
QueueConnectionFactory factory =
(QueueConnectionFactory)ctx.lookup("jms/QueueConnectionFactory");
Queue requestQueue = (Queue)ctx.lookup("jms/sampleQueue");
conn = factory.createQueueConnection();
//establish separate write and read sessions
readSession = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
//writeSession = secondConn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
writeSession = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
//create a MessageSender for the permanent requestQueue
msgSender = writeSession.createSender(requestQueue);
//create a temporary queue for responses, and try to print
it's name
responseQueue = readSession.createTemporaryQueue();
System.out.println("Sender: The temporary queue was
assigned this
name: " + responseQueue.toString());
//create a receiver for the temporary response queue
responseReceiver =
readSession.createReceiver(responseQueue);
responseReceiver.setMessageListener(this);
//start the read connection
conn.start();
}
catch (Exception e) {
e.printStackTrace();
// closeJMS();
}
}
public void sendRequest (String str) {
try {
ObjectMessage objMsg = writeSession.createObjectMessage();
objMsg.setObject(str);
//set JMSReplyTo field of the request message to the
temporary queue
objMsg.setJMSReplyTo(responseQueue);
//send the message to the permanent request queue
msgSender.send(objMsg);
System.out.println("Sender: Sent the request to the
permanent
queue");
}
catch (Exception e) {
e.printStackTrace();
closeJMS();
}
}
/**Prints out the response message when is received from the
temporary queue
* @param msg the msg received from the temporary response queue
*/
public void onMessage(final javax.jms.Message msg) {
System.out.println("Sender: Got the message from temporary
response
queue");
if (msg instanceof ObjectMessage) {
ObjectMessage message = (ObjectMessage)msg;
try {
System.out.println("Sender: The message was: " +
(String)message.getObject());
}
catch (JMSException jmse)
{
System.out.println("Unable to print the message");
}
}
// so it knows to stop waiting for a response
messageReceived ++;
//messageReceived = true;
}
/** Gets an initial context from the QueueConnectionFactory*/
protected static Context getInitialContext() throws NamingException
{
Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.evermind.server.rmi.RMIInitialContextFactory");
p.setProperty(Context.PROVIDER_URL, "ormi://localhost/default");
p.setProperty(Context.SECURITY_PRINCIPAL, "admin");
p.setProperty(Context.SECURITY_CREDENTIALS, "123");
return new InitialContext(p);
}
/** Waits for the response message */
private void waitForResponse() {
do {// System.out.println("Waiting...");
} while (messageReceived < 3);
//while (messageReceived == false);
}
/**Frees the JMS resources*/
public void closeJMS() {
try {
msgSender.close();
writeSession.close();
//secondConn.close();
responseReceiver.close();
readSession.close();
conn.close();
}
catch (JMSException jmse)
{ jmse.printStackTrace ();
System.out.println("Unable to close all JMS objects");
}
}
public static void main(String[] args) {
/*
* create a QueueTestSender, which will send to the perm queu and
* listen on the temp queue
*/
int t = 1;
OrionTestSender sender = new OrionTestSender();
//send 3 requests on the same Temporary Queue
for (t=1; t < 4; t++)
{
sender.sendRequest("Request Msg. Number: " + t);
/*
* Without this waitForResponse method call, the main method of
* the QueueTestSender class ends, and closes the
* connection (which closes the Temporary Queue) before the
* QueueTestReceiver can send a response to the JMSReplyTo
destination
*/
}
sender.waitForResponse();
sender.closeJMS();
}
}
/*
* OrionTestReceiver.java
*
* Created on September 26 2000, 5:00 PM
*
*/
package OrionTempQueue;
import javax.jms.*;
import javax.naming.*;
import java.util.*;
import java.io.*;
import java.util.Properties;
import java.rmi.*;
import javax.rmi.*;
/**
* QueueTestReceiver listens to a permanent queue for requests, and
writes
* its responses to the temporary queue in the JMSReplyTo destination
field
* of the request message.
*
* @author cdull
* @version %I%, %G%
*/
public class OrionTestReceiver implements MessageListener {
private QueueConnection conn;
private Queue requestQueue;
private QueueReceiver rec;
private QueueSender msgSender;
private QueueSession readSession;
private QueueSession writeSession;
private Context ctx;
private int respNumber;
/** Creates new OrionTestReceiver */
public OrionTestReceiver() {
respNumber = 0;
try {
ctx = getInitialContext();
initJMS();
}
catch (Exception e) {
e.printStackTrace();
closeJMS();
}
}
/** Gets an initial context*/
protected static Context getInitialContext() throws NamingException
{
Properties p = new Properties();
p.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"com.evermind.server.rmi.RMIInitialContextFactory");
p.setProperty(Context.PROVIDER_URL, "ormi://localhost/default");
p.setProperty(Context.SECURITY_PRINCIPAL, "admin");
p.setProperty(Context.SECURITY_CREDENTIALS, "123");
return new InitialContext(p);
}
/** Sets up the JMS connections, readsession and receiver*/
private void initJMS() {
try {
QueueConnectionFactory factory =
(QueueConnectionFactory)PortableRemoteObject.narrow
(ctx.lookup("jms/QueueConnectionFactory"),
QueueConnectionFactory.class);
requestQueue =
(Queue)PortableRemoteObject.narrow(ctx.lookup("jms/sampleQueue"),
Queue.class);
conn = factory.createQueueConnection();
readSession = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
rec = readSession.createReceiver(requestQueue);
rec.setMessageListener(this);
//start recieving messages
System.out.println("Receiver: Ready to receive messages");
conn.start();
}
catch (Exception e) {
e.printStackTrace();
//closeJMS
}
}
public static void main(String args[]) {
OrionTestReceiver msgReceiever = new OrionTestReceiver();
}
/** Prints out the message received from the permanent queue,
* creates a writesession and a QueueSender for the temporary queue
* that was send in the JMSReplyTo field
* of the request message, and sends a response message to the
temporary
queue
*
* @param p1 the request message received from the permanent
queue
*/
public void onMessage(final javax.jms.Message p1) {
ObjectMessage msg = (ObjectMessage)p1;
try {
//print out the request message received on the permanent
queue
System.out.print("Receiver: Read this message from the
requestQueue:");
System.out.println((String)msg.getObject());
//create a new session and a QueueSender for the temporary
queue
writeSession = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
System.out.println("Receiver: Created the writeSession");
QueueSender msgSender =
writeSession.createSender((TemporaryQueue)msg.getJMSReplyTo());
System.out.println("Receiver: Created a sender on
JMSReplyTo
destination: " +
((TemporaryQueue)msg.getJMSReplyTo()).getQueueName());
/* create response and send it to the temporary queue -
* is the queue still there after a response is sent to the
* first request?
*/
ObjectMessage respMsg = writeSession.createObjectMessage();
respNumber++;
respMsg.setObject("Response Message" + respNumber);
msgSender.send(respMsg);
System.out.println("Receiver: Sent response number: " +
respNumber);
//free the JMS resources - caused the temp connection to be
disconnected
//closeJMS();
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Reciever: Exception occured");
closeJMS();
}
}
/**
* Close all JMS objects associated with the permanent queue.
*/
public void closeJMS () {
try {
rec.close();
readSession.close();
conn.close();
/*
* closing the objects associated with the temporary queue
* seems to Create a Null Pointer Exception, perhaps
because they
are already
* closed after the reply is sent and consumed by the
QueueTestSender.
*/
// msgSender.close();
// writeSession.close();
// secondConn.close();
}
catch (JMSException jmse)
{
jmse.printStackTrace();
System.out.println("Unable to close all JMS Objects");
}
}
}
queue.jar