Friday, March 3, 2017

Test JMS Connections


Below a simple code to test the jms connection. This will time the initialcontext and the queue connection steps. You can get a queue and factory details from the weblogic console. Services - Messaging - JMSModules. Pre-reqs are like, you need to create a JMS server, JMSModule, ConnectionFactory, Queue.


import java.io.*;
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TestJMS
{

public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
public final static String JMS_FACTORY="testFactory";
public final static String QUEUE="testQ";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private Queue queue;

long t1,t2;

public void initialize(Context ctx, String queueName)throws NamingException, JMSException
{

t1=System.currentTimeMillis();
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
t2=System.currentTimeMillis();
System.out.println(t2-t1);// this is to measure how long does the connection takes
}


public void close() throws JMSException {
qcon.close();
}


public static void main(String[] args) throws Exception {
long t1,t2;


Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, args[0]);
t1=System.currentTimeMillis();
InitialContext ic = new InitialContext(env);
t2=System.currentTimeMillis();
System.out.println("inittime:"+(t2-t1));// this is to measure how long does the initial context takes

TestJMS qs = new TestJMS();
qs.initialize(ic, QUEUE);
qs.close();
}

}



Before executing this program, the environment variables needs to be set. This can be done by running setDomainEnv.sh under weblogic home-userprojs-domains..bin and run the java code from here by passing the t3 url as argument.