Use the below code to produce a Message in JMS queue using Java code. You can alter the code for lot of automations.
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueProducer
{
// JNDI context factory.- its default
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
// JMS context factory- JNDI Name.
public final static String JMS_FACTORY="jms/aia/TestResourceCF";
// Queue JNDI.
public final static String QUEUE="jms/aia/AIA_TEST_REQ_IN_JMSQ";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private static QueueSender qsender;
private Queue queue;
private static Queue replytoqueue;
private static TextMessage msg;
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();
}
public static void send(String message) throws Exception {
msg.setText(message);
// Replytoname rn=new Replytoname(); //Optional
// Queue replyToQueue = rn.getreplyto(); //Optional
// msg.setJMSReplyTo(replytoqueue); //Optional
// msg.setStringProperty("_wls_mimehdrContent_Type","text/xml; charset=UTF-8"); You can set any JMS Properties Here
// msg.setJMSCorrelationID("testConnectivity");
qsender.send(msg);
}
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueProducer qs = new QueueProducer();
String test="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/> <soapenv:Body/></soapenv:Envelope>"; //XML Message
qs.init(ic, QUEUE); // Open the Queue Session
send(test); // Produce the message
qs.close(); // Close the session
}
private static InitialContext getInitialContext()
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "t3://Host:port"); // Your server credentials
env.put(Context.SECURITY_PRINCIPAL, "weblogic");
env.put(Context.SECURITY_CREDENTIALS, "password");
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 QueueProducer
{
// JNDI context factory.- its default
public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
// JMS context factory- JNDI Name.
public final static String JMS_FACTORY="jms/aia/TestResourceCF";
// Queue JNDI.
public final static String QUEUE="jms/aia/AIA_TEST_REQ_IN_JMSQ";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private static QueueSender qsender;
private Queue queue;
private static Queue replytoqueue;
private static TextMessage msg;
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();
}
public static void send(String message) throws Exception {
msg.setText(message);
// Replytoname rn=new Replytoname(); //Optional
// Queue replyToQueue = rn.getreplyto(); //Optional
// msg.setJMSReplyTo(replytoqueue); //Optional
// msg.setStringProperty("_wls_mimehdrContent_Type","text/xml; charset=UTF-8"); You can set any JMS Properties Here
// msg.setJMSCorrelationID("testConnectivity");
qsender.send(msg);
}
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueProducer qs = new QueueProducer();
String test="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header/> <soapenv:Body/></soapenv:Envelope>"; //XML Message
qs.init(ic, QUEUE); // Open the Queue Session
send(test); // Produce the message
qs.close(); // Close the session
}
private static InitialContext getInitialContext()
throws NamingException
{
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, "t3://Host:port"); // Your server credentials
env.put(Context.SECURITY_PRINCIPAL, "weblogic");
env.put(Context.SECURITY_CREDENTIALS, "password");
return new InitialContext(env);
}
}
No comments:
Post a Comment