package examples.jms.sender;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.*;
import javax.jms.*;
import javax.servlet.*;
import javax.servlet.http.*;
import examples.common.utils.ExampleUtils;
public class SenderServlet extends HttpServlet
{
// Defines the JMS connection factory.
public final static String JMS_FACTORY="weblogic.examples.jms.TopicConnectionFactory";
// Defines the topic.
public final static String TOPIC="weblogic.examples.jms.exampleTopic";
// Defines the queue.
public final static String QUEUE="weblogic.examples.jms.exampleQueue";
/**
* Processes HTTP requests.
* @param req HTTP servlet request
* @param res HTTP servlet response
* @exception IOException if problem with I/O operation
* @exception ServletException if problem with servlet
*/
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
res.setHeader("Pragma", "no-cache");
PrintWriter pw = res.getWriter();
boolean persistent;
String topicMsg="";
int priority;
long ttl;
String correlate;
String replyto;
String msgText="";
try {
if (req.getMethod().equals("GET")) {
printForm(pw);
} else {
topicMsg =
req.getParameterValues("dest")[0];
persistent =
req.getParameterValues("persistent")[0].equals("persistent");
priority =
Integer.parseInt(req.getParameterValues("priority")[0]);
ttl =
Long.parseLong(req.getParameterValues("timetolive")[0]);
correlate =
req.getParameterValues("correlate")[0];
replyto =
req.getParameterValues("replyto")[0];
msgText =
req.getParameterValues("msgtext")[0];
if (topicMsg.equals("topic"))
sendTopicMessage(persistent, priority, ttl, correlate, replyto, msgText);
else
sendQueueMessage(persistent, priority, ttl, correlate, replyto, msgText);
pw.println(ExampleUtils.returnHtmlHeader("JMS Message Status"));
pw.println("
");
pw.println("Message Status
");
pw.println("Message Submitted: "+ExampleUtils.encodeXSS(msgText));
}
}
catch (Exception ex) {
ex.printStackTrace(new PrintWriter(pw));
ex.printStackTrace();
}
finally {
pw.println(ExampleUtils.returnHtmlFooter());
}
}
/**
* Displays the web page for message input.
* @param pw print writer
* @exception Exception if problem with printing form
*/
public void printForm(PrintWriter pw)
throws Exception
{
pw.println(ExampleUtils.returnHtmlHeader("JMS Message Submitter"));
pw.println("");
pw.println("Submit a JMS Message
");
pw.println("");
}
/**
* Sends message to the topic.
* @param persistent persistency setting
* @param priority priority setting
* @param ttl time-to-live setting
* @param correlate correlation ID setting
* @param replyto reply-to setting
* @param topicMessage message
* @exception NamingException if problem with JNDI context interface
* @exception JMSException if JMS fails send message due to internal error
*/
public void sendTopicMessage(boolean persistent, int priority,
long ttl, String correlate,
String replyto, String topicMessage)
throws NamingException, JMSException
{
Context ctx = new InitialContext();
TopicConnectionFactory tconFactory;
TopicConnection connection;
TopicSession session;
TopicPublisher publisher;
Topic topic;
Queue queue;
TextMessage msg;
tconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
connection = tconFactory.createTopicConnection();
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(TOPIC);
publisher = session.createPublisher(topic);
msg = session.createTextMessage();
if (correlate.length() > 0)
msg.setJMSCorrelationID(correlate);
if (replyto.equals("topic")) {
msg.setJMSReplyTo(topic);
}
else if (replyto.equals("queue")) {
try {
queue = (Queue) ctx.lookup(QUEUE);
msg.setJMSReplyTo(queue);
} catch (NamingException ne) {
}
}
msg.setText(topicMessage);
connection.start();
publisher.publish(msg,
persistent ? DeliveryMode.PERSISTENT
: DeliveryMode.NON_PERSISTENT,
priority,
ttl);
publisher.close();
session.close();
connection.close();
}
/**
* Sends a message to the queue.
* @param persistent persistency setting
* @param priority priority setting
* @param ttl time-to-live setting
* @param correlate correlation ID setting
* @param replyto reply-to setting
* @param topicMessage message
* @exception NamingException if problem with JNDI context interface
* @exception JMSException if JMS fails send message due to internal error
*/
public void sendQueueMessage(boolean persistent, int priority,
long ttl, String correlate,
String replyto, String topicMessage)
throws NamingException, JMSException
{
Context ctx = new InitialContext();
QueueConnectionFactory qconFactory;
QueueConnection qcon;
QueueSession qsession;
QueueSender qsender;
Queue queue;
Topic topic;
TextMessage msg;
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(QUEUE);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
if (correlate.length() > 0)
msg.setJMSCorrelationID(correlate);
if (replyto.equals("queue")) {
msg.setJMSReplyTo(queue);
}
else if (replyto.equals("topic")) {
topic = (Topic) ctx.lookup(TOPIC);
msg.setJMSReplyTo(topic);
}
msg.setText(topicMessage);
qcon.start();
qsender.send(msg,
persistent ? DeliveryMode.PERSISTENT
: DeliveryMode.NON_PERSISTENT,
priority,
ttl);
qsender.close();
qsession.close();
qcon.close();
}
}