aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Why this compiles in Sun App Server but not JBOSS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Why this compiles in Sun App Server but not JBOSS" Watch "Why this compiles in Sun App Server but not JBOSS" New topic
Author

Why this compiles in Sun App Server but not JBOSS

jim stone
Greenhorn

Joined: Apr 26, 2005
Posts: 18
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) {}
}
}
}
}
jim stone
Greenhorn

Joined: Apr 26, 2005
Posts: 18
bump.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30957
    
158

Jim,
Pardon me if I'm missing the obvious, but what compiler error do you get in JBoss?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
jim stone
Greenhorn

Joined: Apr 26, 2005
Posts: 18
It states that createConnection() is not a method of ConnectionFactory. I can see why that is as the ConnectionFactory object was not cast to a QueueConnectionFactory or TopicConnectionFactory (unless the cast takes place under the covers). The subclass actually defines the createConnection() method. I can understand why it fails under the JBOSS IDE compiler. I guess I'm puzzled as to why it compiles under the Sun J2EE 1.4 compiler.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30957
    
158

Jim,
Sun App Server is correct. According to the API, the ConnectionFactory interface has a getConnection() method. Through polymorphism in Java, it doesn't need a more detailed type.

I suspect JBoss is pointing to an older verion of the j2ee jar.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I would expect every EJB Server provider to include a JAR file containing the files needed for J2EE development and running. Do not use the j2ee.jar file downloaded from Sun's website, every such file on your machine should be removed or renamed. And change your classpath so that this file is not referred to.

I strongly suspect that this is the problem, because Sun's javax.jms.ConnectionFactory interface does not declare any methods whilst JBoss's javax.jms.ConnectionFactory interface declares two createConnection methods.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
jim stone
Greenhorn

Joined: Apr 26, 2005
Posts: 18
I do see the createConnection() under the ConnectionFactory interface now. Perhaps I was looking at the J2EE 1.3 API when I was looking into this earlier. This makes me feel better now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this compiles in Sun App Server but not JBOSS