I want to publish message for which I am writing a JMS application which will publish messages to Tibco EMS queues. There are two queues one is for normal logging and another for exception logging. Now how to send message to two different queues in JMS. Can anyone help me with this as it is very critical?
-
I don't see where is the problem to implement that? – losusovic Nov 16 '17 at 12:10
-
Generally we send messages to one queue. But in here I need to send two diferent kinds of messages to two different queue. One queue for logging and another for errors. – Kumaresh Roy Nov 17 '17 at 05:41
2 Answers
The very basic JMS API code to send message to queue is shown below. You need to adjust the connection factory as well queue name as per your environment. Also need to adjust initial context settings.
void sendMessage() {
Connection con = null;
try {
// Get the initial context
Hashtable<String, String> hTable = new Hashtable<String, String>();
hTable.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
hTable.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(hTable);
// Create ConnectionFactory
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("JMS-JNDI-ConFactory");
// Create connection
con = cf.createConnection();
// Create Non transacted Session with auto ack
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination
Queue queue = (Queue) ctx.lookup("JMS-JNDI-Queue");
// Create MessageProducer for the destination
MessageProducer producer = session.createProducer(queue);
// Create empty Message with header and properties only
TextMessage message = session.createTextMessage();
// set the message body
message.setText("Message-1");
// Send the message
producer.send(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

- 273
- 1
- 11
-
Thanks.. But I need to send two different kinds of messages to two different queue. One queue for logging and another for errors. – Kumaresh Roy Nov 17 '17 at 05:41
-
This is just a sample detailing how to send message. You can reuse the code to send any type of message to any other queues. – Har Krishan Nov 17 '17 at 06:10
This is an old topic, but maybe it can help.
To send a message to a JMS queue actually need the followings:
Context context = null;
QueueConnection queueConnection = null;
QueueSession queueSession = null;
Queue queue;
And this is how it works:
context = getContext(host, port, user, password);
queueConnection = getConnectionFactory(context, connectionFactoryJndi);
queueSession = getQueueSession(queueConnection);
queue = getQueue(context, queueJndi);
// send a text message
queueConnection.start();
String message = "hello";
sendMessageToQueue(verbose, message, queueSession, queue);
queueConnection.stop();
To obtain the context you need to connect to the server:
private Context getContext(String host, int port, String user, String password) throws NamingException {
String url = String.format("%s://%s:%d", protocol, host, port);
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, user);
env.put(Context.SECURITY_CREDENTIALS, password);
return new InitialContext(env);
}
Get the connection factory:
private QueueConnection getConnectionFactory(Context context, String jndiName)
throws NamingException, JMSException {
QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(jndiName);
return connectionFactory.createQueueConnection();
}
Open a queue session:
private QueueSession getQueueSession(QueueConnection queueConnection) throws JMSException {
return queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
}
Get the queue:
private Queue getQueue(Context context, String jndiName) throws NamingException {
return (Queue) context.lookup(jndiName);
}
And finally, send your message to the queue:
private static void sendMessageToQueue(boolean verbose, String message, QueueSession queueSession, Queue queue) throws JMSException {
TextMessage textMessage = queueSession.createTextMessage(message);
try (QueueSender queueSender = queueSession.createSender(queue)) {
queueSender.send(textMessage);
}
}
These code snippets come from here: https://github.com/zappee/jms-message-sender This is a JMS sender command-line tool, you can use this project as an example.
Hope that it helps.

- 20,148
- 14
- 73
- 129