Use following code snippet to post message to Weblogic JMS:
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MsgProducer {
public MsgProducer() {
super();
}
public static void main(String[] args) {
MsgProducer msgProducer = new MsgProducer();
String str = "Testing JMS connectivity. Posting sample JMS message";
Context ctx;
ConnectionFactory cf;
Destination queue;
Connection conn = null;
Session session = null;
MessageProducer msgProd = null;
TextMessage msg = null;
try {
ctx = msgProducer.getInitialContext();
cf = (ConnectionFactory)ctx.lookup("<Connection Factory JNDI Name>");
conn = cf.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Destination)ctx.lookup("<JMS Queue JNDI Name>");
msgProd = session.createProducer(queue);
msg = session.createTextMessage(str);
msgProd.send(msg);
} catch (NamingException e){
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
private Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://serverhost:port(8001)");
return new InitialContext(env);
}
}
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MsgProducer {
public MsgProducer() {
super();
}
public static void main(String[] args) {
MsgProducer msgProducer = new MsgProducer();
String str = "Testing JMS connectivity. Posting sample JMS message";
Context ctx;
ConnectionFactory cf;
Destination queue;
Connection conn = null;
Session session = null;
MessageProducer msgProd = null;
TextMessage msg = null;
try {
ctx = msgProducer.getInitialContext();
cf = (ConnectionFactory)ctx.lookup("<Connection Factory JNDI Name>");
conn = cf.createConnection();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Destination)ctx.lookup("<JMS Queue JNDI Name>");
msgProd = session.createProducer(queue);
msg = session.createTextMessage(str);
msgProd.send(msg);
} catch (NamingException e){
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
private Context getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "t3://serverhost:port(8001)");
return new InitialContext(env);
}
}
No comments:
Post a Comment
Provide your thoughts !