http://blog.csdn.net/zhangxs_3/archive/2009/03/29/4034788.aspx
http://docs.sun.com/app/docs/doc/820-4610/abdbo?l=zh&a=view
jms-activemq
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
public class MessageReceiveServlet extends HttpServlet { //接收消息
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
HttpSession httpsession=req.getSession();
String userName=(String)httpsession.getAttribute("userName");
//创建一个Queue
// Queue queue = new ActiveMQQueue("testQueue");
Queue queue = new ActiveMQQueue(userName);
//创建一个Session
try{
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//注册消费者1
MessageConsumer comsumer1 = session.createConsumer(queue);
comsumer1.setMessageListener(new MessageListener(){
public void onMessage(Message m) {
try {
System.out.println("Consumer1 get " + ((TextMessage)m).getText());
//out.println("Message: "+" --"+((TextMessage)m).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
/* MessageConsumer comsumer = session.createConsumer(queue);//反应太慢
Message recvMessage = comsumer.receive();
System.out.println(((TextMessage)recvMessage).getText());*/
}catch(JMSException e){
e.printStackTrace();
}
}
}
public class MessageSendServlet extends HttpServlet { //发送消息
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req,resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String receiveName=req.getParameter("receiveName"); //zw
String content=req.getParameter("content");
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
try{
Connection connection = factory.createConnection();
connection.start();
// Queue queue = new ActiveMQQueue("testQueue"); //里面的字符,和接收的是一样的,才能一对一的获取消息
Queue queue=new ActiveMQQueue(receiveName); //一般是用户名,必须唯一
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
//就算用户不在线,只要服务器不关闭,发送的消息就存在,一上线就会出来
producer.send(session.createTextMessage(content));
// producer.send(session.createTextMessage("A persistent Message"));
// producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); //发送一个non persistent消息, 而刚好这个时候没有消费者在监听, 这个消息也会丢失
// producer.send(session.createTextMessage("A non persistent Message"));
producer.close();
session.close();
connection.close();
}
catch(JMSException e){
e.printStackTrace();
}
System.out.println("Send messages sucessfully!");
resp.sendRedirect("index.jsp");
}
}
有个问题:就是如何让消息显示出来?在后台,消息一发送,另一用户能知道。但是在MessageListener监听器中不能传递消息,只知道有消息了。