No Hope - JMS over HTTP 4.0.1
gberish Mar 21, 2005 1:46 PMI would sure apprecitate someone running the code below and giving me a hint as to why I am getting the error I do.
Getting started 4.0 and everything else I found says it is easy to send JMS messages over HTTP, but I cannot.
The code is a client that cycles through 2 attempts to send and retreive a message from queue/A [which comes with the default server].
Please see ScreenPrint Output beneath code.
1. Regular JMS. Works great.
2. Over HTTP. Fails miserably.
import java.lang.reflect.Method;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.ConnectionFactory;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.QueueSender;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MessageTest {
public static void main (String[] args) {
MessageTest mb = new MessageTest ();
for (int i = REG; i <= HTTP; i++) {
mb.doMessage (i);
}
}
// method
public void doMessage (int type) {
System.out.println ("\n\nType = " + TYPE [type]);
try {
// get initial context
Hashtable<String, String> env= new Hashtable<String, String> ();
switch (type) {
case REG:
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL,
"jnp://localhost:1099");
env.put(Context.URL_PKG_PREFIXES,
"org.jnp.interfaces");
break;
case HTTP:
env.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.HttpNamingContextFactory");
env.put(Context.PROVIDER_URL,
"http://localHost:1099/invoker/JMXInvokerServlet");
env.put(javax.naming.Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
break;
}
System.out.println ("Calling InitialContext");
ctx = new InitialContext(env);
System.out.println ("InitialContext Found");
// lookup connection factory
factory = (QueueConnectionFactory)ctx.lookup("ConnectionFactory");
System.out.println ("Connection Factory instantiated");
// lookup destination
destination = (Queue)ctx.lookup("queue/A");
System.out.println ("Destination instantiated");
// create connection
connection = factory.createQueueConnection();
System.out.println ("Connection instantiated");
// create session
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println ("Session instantiated");
// create sender
sender = session.createSender(destination);
System.out.println ("Sender instantiated");
// create receiver
receiver = session.createReceiver(destination);
System.out.println ("Receiver instantiated");
// create message w/text & selector property
String m = "Success";
message = session.createTextMessage(m);
System.out.println ("Message instantiated w/ text = \"" + m + "\"");
message.setStringProperty ("Player", "Great");
System.out.println ("Message Property \"Player\" set to \"Great\"");
// starting connection
connection.start ();
System.out.println ("Connection Started");
// send message
sender.send(message);
System.out.println ("Message Sent:");
// receive message
message = (TextMessage) receiver.receive ();
System.out.println ("Message Received:");
System.out.println ("Message text = \"" + message.getText () + "\"");
System.out.println ("Message Property \"Player\" = \"" + message.getStringProperty ("Player")+ "\"");
} catch (NamingException e) {
System.out.println ("Naming Exception");
e.printStackTrace ();
} catch (JMSException e) {
System.out.println ("JMS Execption");
e.printStackTrace ();
} finally {
if (connection != null) {
try {
connection.stop ();
connection.close();
} catch (JMSException e) {
}
}
}
System.out.println("main () End:");
}
// members
static final int REG = 0;
static final int HTTP = 1;
static final String[] TYPE = {"Regular JMS Message", "JMS Message Over HTTP"};
int type;
Context ctx;
Queue destination;
QueueConnection connection;
QueueConnectionFactory factory;
QueueReceiver receiver;
QueueSession session;
QueueSender sender;
TextMessage message;
}Screen Print OutputType = Regular JMS Message Calling InitialContext InitialContext Found Connection Factory instantiated Destination instantiated Connection instantiated Session instantiated Sender instantiated Receiver instantiated Message instantiated w/ text = "Success" Message Property "Player" set to "Great" Connection Started Message Sent: Message Received: Message text = "Success" Message Property "Player" = "Great" main () End: Type = JMS Message Over HTTP Calling InitialContext Naming Exception javax.naming.NamingException: Failed to retrieve Naming interface [Root exception is java.lang.Class CastException: java.rmi.MarshalledObject] at org.jboss.naming.HttpNamingContextFactory.getInitialContext(HttpNamingContextFactory.java :69) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247) at javax.naming.InitialContext.init(InitialContext.java:223) at javax.naming.InitialContext.<init>(InitialContext.java:197) at MessageTest.doMessage(MessageTest.java:53) at MessageTest.main(MessageTest.java:23) Caused by: java.lang.ClassCastException: java.rmi.MarshalledObject at org.jboss.naming.HttpNamingContextFactory.getNamingServer(HttpNamingContextFactory.java:1 20) at org.jboss.naming.HttpNamingContextFactory.getInitialContext(HttpNamingContextFactory.java :65) ... 6 more main () End: Press any key to continue . . .
THNAX!