Sunday, October 14, 2012

Sending message to weblogic queue

A java class for sending text message to a weblogic queue
===============================


package test;

import java.util.Hashtable;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class QSendTest {
public static void main(String[] args)
throws Exception {
 QueueConnectionFactory qconFactory = null;
 QueueConnection qcon = null;
 QueueSession qsession = null;
 Queue queue = null;
 TextMessage msg = null;

 InitialContext ctx = getInitialContext();
 qconFactory = (QueueConnectionFactory)
 ctx.lookup("jms/MyTestConnectionFactory");
 qcon = qconFactory.createQueueConnection();
 qsession = qcon.createQueueSession(false,
  QueueSession.AUTO_ACKNOWLEDGE);

 queue = (Queue) ctx.lookup("jms/MyTestQueue");

//Posting message to Queue
 QueueSender qsender = qsession.createSender(queue);
 msg = qsession.createTextMessage();
 msg.setStringProperty("MSG_ID", "12345678947");
 msg.setText("Hello at " + System.currentTimeMillis());
 qsender.send(msg);
 System.out.println("Sent");
 qsender.close();

}

private static InitialContext getInitialContext()
throws NamingException {
 Hashtable<String, String> env = new Hashtable<String, String>();
 env.put(Context.INITIAL_CONTEXT_FACTORY,
   "weblogic.jndi.WLInitialContextFactory");
 env.put(Context.PROVIDER_URL, "t3://localhost:8001/");
 return new InitialContext(env);
}
}

No comments:

Post a Comment