I am new to JMS, after a long re search i googled out a code to connect to JMS and post a msg.
The problem is i need to post the message in a remote queue, But i am not sure how to establish connection to it and post the message.
SERVER TYPE : TIBCO EMS
SERVER HOST : **.*****.net
PORT : *
**USername : user
passsword : user123
Queue : *.*....Order.Management..1
I would like to establish connection, post a simple msg and retrieve it back. Kindly help! thanks in advance
CODE WHICH I GOT FROM INTERNET
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class emm {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY="com.tibco.tibjms.naming.TibjmsInitialContextFactory";
// Defines the JMS context factory.
public final static String JMS_FACTORY="jms/TestConnectionFactory";
// Defines the queue.
public final static String QUEUE="CPW.GBR.POR.Public.Request.Order.Management.UpdateProvisioningStatus.1";
private QueueConnectionFactory qconFactory;
private ConnectionFactory conFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
/**
* Creates all the necessary objects for sending
* messages to a JMS queue.
*
* @param ctx JNDI initial context
* @param queueName name of queue
* @exception NamingException if operation cannot be performed
* @exception JMSException if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
/** main() method.
*
* @param args WebLogic Server URL
* @exception Exception if operation fails
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: java examples.jms.queue.QueueSend WebLogicURL");
return;
}
InitialContext ic = getInitialContext(args[0]);
emm qs = new emm();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(emm qs)
throws IOException, JMSException
{
BufferedReader msgStream = new BufferedReader(new InputStreamReader(System.in));
String line=null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): \n");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: "+line+"\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (! quitNow);
}
private static InitialContext getInitialContext(String url)
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
Finally from different sources i found the best possible to way for this. This code will definitely work. I have tried this out and is running currently on my machine
import java.util.Properties;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.*;
public class JMSExample {
static String serverUrl = "tcp://10.101.111.101:10001"; // values changed
static String userName = "user";
static String password = "pwd";
static QueueConnection connection;
static QueueReceiver queueReceiver;
static Queue queue;
static TextMessage message;
public static void sendTopicMessage(String topicName, String messageStr) {
Connection connection = null;
Session session = null;
MessageProducer msgProducer = null;
Destination destination = null;
try {
TextMessage msg;
System.out.println("Publishing to destination '" + topicName
+ "'\n");
ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(
serverUrl);
connection = factory.createConnection(userName, password);
session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(topicName);
msgProducer = session.createProducer(null);
msg = session.createTextMessage();
msg.setStringProperty("SourceId", userName);
msg.setStringProperty("BusinessEvent", password);
msg.setText(messageStr);
msgProducer.send(destination, msg);
System.out.println("Published message: " + messageStr);
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws JMSException {
// TODO Auto-generated method stub
JMSExample.sendTopicMessage("***.***.***.**.**.Order.Management.***.1",
"Hi");
//System.out.println(getMessage());
}