运行环境:WebLogic Server 10.3.5 + Oracle Database 10g Express Edition 10.2.0.1。
[domain_name]-> Services-> Messaging -> JMS Servers

1. WebLogic Server中JMS架构说明:
(1)JMS Server是符合JMS规范的Server,它是target到WebLogic Server上的,不能独立运行。
(2)JMS Module中包含所有JMS资源信息(Conntion Factory,Queue,Topic),它是target到WebLogic Server上的。
(3)Queue 或 Topic 是target到JMS Server上的。
(4)SubDeployment 是JMS Module的一个属性,它是target到JMS Server上的。
2. 发布消息到Queue中。
JMS传输消息方式之一是点对点方式(Point-to-Point),这种方式的特点是:
(1)消息的生产者把消息发送到Queue中。
(2)消息的消费者从Queue中读取消息。
(3)消息队列中的消息默认遵循先进先出原则(First-In, First-Out (FIFO))。
(4)一条消息只能被一个消费者读取,一旦读取后,其它消费者就“看不到了”。
点对点方式的典型应用场景是客服系统:所有用户的请求都依次发到Queue中,最先发送的请求最先处理,每个客服人员处理某个请求后,其他客服人员就不用处理该请求了。
发送消息到Queue的代码如下:
package com.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.*;
import javax.naming.*;
public class postToQueue extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
String message = request.getParameter("message");
if (message.equals(""))
{
out.print("Enter a message to post to the queue");
}
else
{
try {
QueueConnectionFactory qconFactory;
QueueConnection qcon;
QueueSession qsession;
QueueSender qsender;
Queue queue;
TextMessage msg;
InitialContext ic = new InitialContext();
qconFactory = (QueueConnectionFactory) ic.lookup("javax.jms.QueueConnectionFactory");
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ic.lookup("dizzyworldQueue");
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
msg.setText(message);
qsender.send(msg);
System.out.println("The message, "+ message +", has been sent to the dizzyworldQueue.");
out.print("<FONT SIZE='4' COLOR='navy'>");
out.print("Your message has been posted<BR>");
out.print("Monitor the message in the Administration Console<BR>");
out.print("</FONT>");
qsender.close();
qsession.close();
qcon.close();
} catch (Exception e) {System.out.print("error " + e); }
}
out.print("<BR><A HREF='./welcome.html'>Back To Home Page</A><BR>");
}
}
在Console监控Queue:点击Queue的名称,点击Monitoring Tab,选择Queue,点击“Show Messages”。


如果没有消费者读取该Queue中的信息,可以在Console中看到发送到Queue中的所有消息。
也就是说,WebLogic Server会保留Queue中的所有消息,直到有消费者来读取信息。
3. 发布消息到Topic中。
JMS传输消息方式之二是发布-订阅方式(Publish-Subscribe),这种方式的特点是:
(1)消息的生产者把消息发送到Topic中。
(2)消息的消费者从Topic中读取消息。
(3)消息主题中的消息默认不持久化,因此如果发送消息时,消费者没有“监听”该Topic,消息将自动丢弃。
(4)一条消息可以同时被多个消费者读取,只要它们订阅了该信息,并且“正在监听”该Topic。

发布-订阅方式的典型应用场景是股票价格发布-订阅系统:消息的生产者会随时发布股票价格,订阅了某股票的消费者,会收到该股票的价格信息。如果消息消费者刚刚登录在线,那么之前发布的价格信息就收不到了。
发送消息到Topic的代码如下:
package com.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.*;
import javax.naming.*;
public class postToTopic extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
String message = request.getParameter("message");
if (message.equals(""))
{
out.print("Enter a message to post to the Topic");
}
else
{
try {
TopicConnectionFactory tconFactory;
TopicConnection tcon;
TopicSession tsession;
TopicPublisher tpublish;
Topic topic;
TextMessage msg;
InitialContext ic = new InitialContext();
tconFactory = (TopicConnectionFactory) ic.lookup("javax.jms.TopicConnectionFactory");
tcon = tconFactory.createTopicConnection();
tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ic.lookup("dizzyworldTopic");
tpublish = tsession.createPublisher(topic);
msg = tsession.createTextMessage();
tcon.start();
msg.setText(message);
tpublish.publish(msg);
System.out.println("The message, " + message + ", has been sent to dizzyworldTopic.");
out.print("<FONT SIZE='4' COLOR='navy'>");
out.print("Your message has been posted<BR>");
out.print("Monitor the message in the Administration Console<BR>");
out.print("</FONT>");
tpublish.close();
tsession.close();
tcon.close();
} catch (Exception e) {out.print("error " + e); }
}
out.print("<BR><A HREF='./welcome.html'>Back To Home Page</A><BR>");
}
}
在Console监控Topic:点击Topic的名称,点击Monitoring Tab。

与Queue不同,在Topic中的消息如果没有消费者读取,WebLogic Server会删除这些信息。
4. 发布消息到Distribute Queue中。
package com.servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.jms.*;
import javax.naming.*;
public class postToDistQueue extends HttpServlet
{
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
String message = request.getParameter("message");
if (message.equals(""))
{
out.print("Enter a message to post to the distributed queue");
}
else
{
try {
QueueConnectionFactory qconFactory;
QueueConnection qcon;
QueueSession qsession;
QueueSender qsender;
Queue queue;
TextMessage msg;
InitialContext ic = new InitialContext();
qconFactory = (QueueConnectionFactory) ic.lookup("dizzyworldConnectionFactory");
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ic.lookup("dizzyworldDistributedQueue");
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
msg.setText(message);
qsender.send(msg);
System.out.println("The message, "+ message +", has been sent to the dizzyworldDistributedQueue.");
out.print("<FONT SIZE='4' COLOR='navy'>");
out.print("Your message has been posted<BR>");
out.print("Monitor the message in the Administration Console<BR>");
out.print("</FONT>");
qsender.close();
qsession.close();
qcon.close();
} catch (Exception e) {System.out.print("error " + e); }
}
out.print("<BR><A HREF='./welcome.html'>Back To Home Page</A><BR>");
}
}
Project下载:test_jms.zip
没有评论:
发表评论