Assuming the below JNDI configurations were set up in the previous step, we go ahead to the sender and receiver modules
1) JMS_FACTORY = "jms/MyConnectionFactory";
2) QUEUE = "jms/MyQueue";
Sample Sender Source code
// import statements
package jms;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
// MySender Class
public class MySender {
// context parameters
public static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
public String JMS_FACTORY = "weblogic.examples.jms.QueueConnectionFactory";
public String QUEUE = "weblogic.examples.jms.exampleQueue";
// declaration of Main()
public static void main(String args[]) throws JMSException {
Context ctx;
try {
// Get the initial Context
ctx = getInitialContext("t3://localhost:7001");
QueueConnectionFactory qconFactory;
QueueConnection connection;
QueueSession session;
QueueSender sender;
Queue queue;
TextMessage msg;
// JNDI Lookup on the created Connection Factory
qconFactory = (QueueConnectionFactory) ctx.lookup
("jms/MyConnectionFactory");
connection = qconFactory.createQueueConnection();
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// JNDI Lookup on the created Queue
queue = (Queue) ctx.lookup("jms/MyQueue");
msg = session.createTextMessage();
sender = session.createSender(queue);
// Send 5 sample messages
for (int i = 0; i <>
msg.setText("Message Id-" +i);
System.out.println("Sending Message" + msg.getText());
connection.start();
sender.send(msg);
}
// close the session and the connection
session.close();
connection.close();
}
// exception handling
catch (NamingException e) {
e.printStackTrace();
}
}
// function to return the initial context
private static InitialContext getInitialContext(String url) throws NamingException {
Hashtable
env.put (Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, url);
return new InitialContext(env);
}
}
The next step will be to write a Sample Receiver to actually read the messages from the queue...
No comments:
Post a Comment