-
Hi I am trying to implement an jms listener for the jms server which is running in different machine. How do I connect/bind to receive the message.
Even though I have trying to run standalone mode publish/subscribe scenarion also not sucedd the error is javax.naming.NoInitialContextException. I am not aware about the jndi part and I think that was the only problem creator.Please can anybody help me on this regard as I am trying to run my program from eclipse-ganymede release.It is show stopper to me!please....
-
Hi,
Try something like following. Make sure you have required client jar files from Jboss in your class path.
private static javax.jms.QueueConnection getQueueConnection( java.util.Hashtable environment ) throws javax.naming.NamingException, javax.jms.JMSException
{
// Obtain initial context
javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
try {
java.lang.Object objRef = initialContext.lookup(CONNECTION_FACTORY_JNDI_NAME);//CONNECTION_FACTORY_JNDI_NAME = jms/testQCF
return ((javax.jms.QueueConnectionFactory) objRef).createQueueConnection();
} finally {
initialContext.close();
}
}
private static javax.jms.Queue getQueue( java.util.Hashtable environment ) throws javax.naming.NamingException
{
// Obtain initial context
javax.naming.InitialContext initialContext = new javax.naming.InitialContext(environment);
try {
java.lang.Object objRef = initialContext.lookup(DESTINATION_JNDI_NAME); //DESTINATION_JNDI_NAME = jms/testQueue
return (javax.jms.Queue) objRef;
} finally {
initialContext.close();
}
}
/**
* Sends an object to the message queue.
*
* @param object The object to be sent to the queue.
*/
private boolean sendObjectToQueue(Serializable object) {
boolean success = false;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Hashtable env = new Hashtable(5);
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.PROVIDER_URL, "localhost:1099");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );
try {
queueConnection = getQueueConnection(env);
queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
QueueSender sender = queueSession.createSender(getQueue(env));
// create the message
ObjectMessage message = queueSession.createObjectMessage();
message.setObject(object);
// send the message
sender.send(message);
sender.close();
success = true;
} catch (JMSException e) {
//do something
} catch (NamingException e) {
//do something
} catch (FFPBEException aae) {
//do something
} catch (Exception e) {
//do something
} finally {
if (queueConnection != null) {
try {
queueConnection.close();
} catch (JMSException e) {
//do something
}
}
if (queueSession != null) {
try {
queueSession.close();
} catch (JMSException e) {
//do something
}
}
}
return success;
}