This program compiles under the Sun Application Server but not under
JBOSS. The code creates an instance of a ConnectionFactory but never casts it to a TopicConnectionFactory or a QueueConnectionFactory. It tries to perform a connectionFactory.getConnection. The getConnection is a method of the sublcass QueueConnectionFactory and TopicConnectionFactory but not of ConnectionFactory. I'm puzzled why it compiles if it wasn't cast to one of the subclasses before trying to execute QueueConnectionFactory. Heres' the code.
import javax.jms.*;
import javax.naming.*;
public class SimpleProducer {
/**
* Main method.
*
* @param args the destination used by the example,
* its type, and, optionally, the number of
* messages to send
*/
public static void main(
String[] args) {
final int NUM_MSGS;
if ( (args.length < 2) || (args.length > 3) ) {
System.out.println("Program takes two or three arguments: " +
"<dest_name> <queue|topic> " +
"[<number-of-messages>");
System.exit(1);
}
String destName = new String(args[0]);
String destType = new String(args[1]);
System.out.println("Destination name is " + destName +
", type is " + destType);
if (args.length == 3){
NUM_MSGS = (new Integer(args[2])).intValue();
} else {
NUM_MSGS = 1;
}
/*
* Create a JNDI API InitialContext object if none exists
* yet.
*/
Context jndiContext = null;
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
System.out.println("Could not create JNDI API " +
"context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and destination. If either
* does not exist, exit. If you look up a
* TopicConnectionFactory instead of a
* QueueConnectionFactory, program behavior is the same.
*/
ConnectionFactory connectionFactory = null;
Destination dest = null;
try {
connectionFactory = (ConnectionFactory) //this is the uncasted reference
jndiContext.lookup("jms/QueueConnectionFactory");
if (destType.equals("queue")) {
dest = (Queue) jndiContext.lookup(destName);
} else if (destType.equals("topic")) {
dest = (Topic) jndiContext.lookup(destName);
} else {
throw new Exception("Invalid destination type" +
"; must be queue or topic");
}
} catch (Exception e) {
System.out.println("JNDI API lookup failed: " +
e.toString());
e.printStackTrace();
System.exit(1);
}
/*
* Create connection.
* Create session from connection; false means session is
* not transacted.
* Create producer and text message.
* Send messages, varying text slightly.
* Send end-of-messages message.
* Finally, close connection.
*/
Connection connection = null;
MessageProducer producer = null;
try {
connection = connectionFactory.createConnection(); //this fails under jboss but not sun app server
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(dest);
TextMessage message = session.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
producer.send(message);
}
/*
* Send a non-text control message indicating end of
* messages.
*/
producer.send(session.createMessage());
} catch (JMSException e) {
System.out.println("Exception occurred: " +
e.toString());
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {}
}
}
}
}