Skip to content

Instantly share code, notes, and snippets.

@mmacphail
Created July 26, 2017 22:31
Show Gist options
  • Save mmacphail/80e7d3ad4158b87fba1c34f72da4e9d7 to your computer and use it in GitHub Desktop.
Save mmacphail/80e7d3ad4158b87fba1c34f72da4e9d7 to your computer and use it in GitHub Desktop.
Connect to Universal Messaging using JMS
import java.util.Hashtable;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class HelloUMJNDI {
public static void main(String[] args) throws NamingException, JMSException {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.pcbsys.nirvana.nSpace.NirvanaContextFactory");
env.put(Context.PROVIDER_URL, "nsp://local-dev:9000");
Context ctx = new InitialContext(env);
ConnectionFactory connFactory = (ConnectionFactory) ctx.lookup("local_um");
Connection conn = connFactory.createConnection();
Topic topic = (Topic) ctx.lookup("externalClientTopic");
Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(topic);
Message message = session.createTextMessage("Hello World !");
producer.send(message);
conn.close();
ctx.close();
}
}
@wuriyanto48
Copy link

@mmacphail, i am getting an error [Root exception is com.pcbsys.nirvana.client.nSessionNotConnectedException: The session is not currently connected to the server. Unable to perform the request:Session has been closed via another thread].

@mmacphail
Copy link
Author

mmacphail commented Oct 12, 2020

Both threads use the same session, but it seems one thread closed the session. You want to open the session in each independent thread, or do not close the session at all in the threads and close it when the app finishes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment