📄 senderservlet.java
字号:
//声明这个类的包examples.jms.sender
package examples.jms.sender;
//本类引入的其它类和包
import java.io.*;
import javax.naming.*;
import javax.jms.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* 这个实例演示从一个Java servlet发送消息。
*/
public class SenderServlet extends HttpServlet
{
/**
* 定义JMS连接构造器
*/
public final static String JMS_FACTORY="weblogic.examples.jms.TopicConnectionFactory";
/**
* 定义主题
*/
public final static String TOPIC="weblogic.examples.jms.exampleTopic";
/**
* 定义队列
*/
public final static String QUEUE="weblogic.examples.jms.exampleQueue";
/**
* 处理HTTP请求
* @参数 req HTTP servlet 请求
* @参数 res HTTP servlet 响应
* @异常 IOException I/O操作错误
* @异常 ServletException servlet错误
*/
public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
String literal;
boolean persistent;
boolean topicMsg;
int priority;
long ttl;
String correlate;
String replyto;
String msgText="";
try {
if (req.getMethod().equals("GET")) {
//get方法
PrintWriter pw = new PrintWriter(res.getOutputStream(),true);
//打印html表单到客户端
printForm(pw);
} else {
//post方法,处理客户端请求
topicMsg =
req.getParameterValues("dest")[0].equals("topic");
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) {
//如果是主题消息,则调用sendTopicMessage方法
sendTopicMessage(persistent, priority, ttl, correlate, replyto, msgText);
} else {
//如果是队列消息,则调用sendQueueMessage方法
sendQueueMessage(persistent, priority, ttl, correlate, replyto, msgText);
}
//设置文档类型
res.setContentType("text/html");
//设置头信息
res.setHeader("Pragma", "no-cache");
//返回客户端html页面
PrintWriter pw = new PrintWriter(res.getOutputStream(),true);
pw.println("<HTML><HEAD><TITLE>Message Status</TITLE></HEAD>");
pw.println("<BODY BGCLOR=#FFFFFF><p><img src=images/BEA_Button_Final_web.gif align=right>");
pw.println("<h1><FONT COLOR=#DB1260 FACE=\"Helvetica\">");
pw.println("Message Status</FONT></h1>");
pw.println("Message Submitted: "+msgText);
pw.println("</BODY></HTML>");
pw.close();
return;
}
}
catch (Exception e) {e.printStackTrace();
}
}
/**
* 显示web页,消息输入
* @参数 pw print writer
* @异常 Exception 如果打印表单错误
*/
public void printForm(PrintWriter pw)
throws Exception
{
//html页面
pw.println("<html><head><title>JMS Message Submitter</title></head>");
pw.println("<body BGCOLOR=#FFFFFF><FONT FACE=\"Helvetica\">");
pw.println("<p><img src=images/BEA_Button_Final_web.gif align=right>");
pw.println("<h1><font color=#DB1260 face=\"HELVETICA\">");
pw.println("Submit a JMS Message</font></h1>");
pw.println("<FORM METHOD=\"POST\" ACTION=\"./jmssender\">");
pw.println("<TABLE bgcolor=#EEEEEE cellpadding=2 border=0 align=center>");
pw.println("<TR><TD>Destination:</TD>");
pw.println("<TD><select name=dest>");
pw.println("<option value=topic selected>JMS Topic");
pw.println("<option value=queue>JMS Queue");
pw.println("</select></TR>");
pw.println("<TR><TD>Message Type:</TD>");
pw.println("<TD><select name=persistent>");
pw.println("<option value=persistent selected>Persistent");
pw.println("<option value=nonpersistent>Non Persistent");
pw.println("</select></TR>");
pw.println("<TR><TD>Priority:</td>");
pw.println("<TD><select name=priority>");
pw.println("<option value=0>0");
pw.println("<option value=1>1");
pw.println("<option value=2>2");
pw.println("<option value=3>3");
pw.println("<option value=4 selected>4");
pw.println("<option value=5>5");
pw.println("<option value=6>6");
pw.println("<option value=7>7");
pw.println("<option value=8>8");
pw.println("<option value=9>9");
pw.println("</select>");
pw.println("</TR><TR><TD>Time to live:</td>");
pw.println("<TD><select name=timetolive>");
pw.println("<option value=0 selected>Never expires");
pw.println("<option value=60000>one minute");
pw.println("<option value=600000>ten minutes");
pw.println("<option value=3600000>one hour");
pw.println("<option value=86400000>one day");
pw.println("</select>");
pw.println("</TR>");
pw.println("<TR><TD>Correlation ID:</TD><TD>");
pw.println("<INPUT NAME=\"correlate\" SIZE=20></TD></TR>");
pw.println("<TR><TD>Reply to:</TD><TD>");
pw.println("<select name=replyto>");
pw.println("<option value=none selected>none");
pw.println("<option value=queue>ExampleQueue");
pw.println("<option value=topic>ExampleTopic");
pw.println("</select></TR>");
pw.println("<TR><TD>Message Text:</TD><TD><INPUT NAME=\"msgtext\" SIZE=60></TD></TR>");
pw.println("<TR><TD colspan=2 align=center>");
pw.println("<INPUT TYPE=\"submit\" VALUE=\"Send Message\"></TD></TR>");
pw.println("</TABLE></FORM></FONT></BODY></HTML>");
}
/**
* 发送主题消息
* @参数 persistent 持久性设置
* @参数 priority 优先级设置
* @参数 ttl 生存期设置
* @参数 correlate 相关 ID 设置
* @参数 replyto 相应设置
* @参数 topicMessage 主题消息
* @异常 NamingException JNDI 上下文接口问题
* @异常 JMSException JMS 发送消息错误抛出的异常
*/
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();
}
/**
* 发送主题消息
* @参数 persistent 持久性设置
* @参数 priority 优先级设置
* @参数 ttl 生存期设置
* @参数 correlate 相关 ID 设置
* @参数 replyto 相应设置
* @参数 topicMessage 主题消息
* @异常 NamingException JNDI 上下文接口问题
* @异常 JMSException JMS 发送消息错误抛出的异常
*/
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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -