• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why this compiles in Sun App Server but not JBOSS

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bump.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,
Pardon me if I'm missing the obvious, but what compiler error do you get in JBoss?
 
jim stone
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
jim stone
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic