Tomcat7.0.0已经出来了,关注它已经支持servlet3.0规范,servlet3.0规范有一个很值得期待的特性就是,支持异步IO通信,何为异步响应,就是保持长连接,让servlet实现原先的TCP Server才能做到的事,就像我以前写的一个WEB IM。没有用到comet,使用Ajax轮询聊天,反应慢不说,很多时候轮询的资源是被浪费掉的,杯具啊
现在好了Tomcat7.0.0已经原生支持comet和异步IO,但是需要APR 或者NIO HTTP连接器,应该在新的servlet-api,会提供tomcat7.0-guide的原文如下:
Usage of these features requires using the APR or NIO HTTP connectors. The classic java.io HTTP connector and the AJP connectors do not support them,
实际是使用,需要增加对NIO的支持,要做的仅仅是在server.xml里边修改connector:
<connector protocol="org.apache.coyote.http11.Http11NioProtocol" port="8080" redirectport="8443" connectiontimeout="20000">
要用好这个comet需要了解comet的几个事件
protected ArrayList connections = new ArrayList(); synchronized(connections) { connections.add(response); }
这端代码很重要,因为只有保存了用户响应的句柄,才是异步comet的关键所在,你可以决定什么时候,什么内容什么顺序把消息分发给哪一个用户.
synchronized(connections) { connections.remove(response); }
下面这个是模拟TCP CHART Server的Servlet聊天服务器,是基于异步长连接的
public class ChatServlet extends HttpServlet implements CometProcessor { protected ArrayList connections = new ArrayList(); protected MessageSender messageSender = null; public void init() throws ServletException { //聊天服务器servlet启动的时候自动启动一个线程来接收用户的聊天消息,并广播出去 messageSender = new MessageSender(); Thread messageSenderThread = new Thread(messageSender, "MessageSender[" + getServletContext().getContextPath() + "]"); messageSenderThread.setDaemon(true); messageSenderThread.start(); } public void destroy() { //清除资源 connections.clear(); messageSender.stop(); messageSender = null; } /** * Process the given Comet event. * * @param event The Comet event that will be processed * @throws IOException * @throws ServletException */ public void event(CometEvent event) throws IOException, ServletException { HttpServletRequest request = event.getHttpServletRequest(); HttpServletResponse response = event.getHttpServletResponse(); if (event.getEventType() == CometEvent.EventType.BEGIN) { //加入刚收到这个用户的请求,触发CometEvent.EventType.BEGIN事件,先打印出一些消息头,并且把这个用户的//response保存在缓存容器中,以备广播用 log("Begin for session: " + request.getSession(true).getId()); PrintWriter writer = response.getWriter(); writer.println(""-//w3c//dtd html 4.0 transitional//en\">"); writer.println("JSP Chat"); writer.flush(); synchronized(connections) { connections.add(response); } } 假如IO错误了,当然释放这个连接,清空这个缓存的response句柄,该用户web im应该提示服务器错误,或者超时 else if (event.getEventType() == CometEvent.EventType.ERROR) { log("Error for session: " + request.getSession(true).getId()); synchronized(connections) { connections.remove(response); } event.close(); } //结束时候也是释放连接,清空这个缓存的response句柄,值得注意的是,这时候用户已经断开聊天服务器 else if (event.getEventType() == CometEvent.EventType.END) { log("End for session: " + request.getSession(true).getId()); synchronized(connections) { connections.remove(response); } PrintWriter writer = response.getWriter(); writer.println("</body></html>"); event.close(); } else if (event.getEventType() == CometEvent.EventType.READ) { InputStream is = request.getInputStream(); byte[] buf = new byte[512]; do { int n = is.read(buf); //can throw an IOException//由于已经在线程中已经读消息和广播消息了,这里这个事件主要用来log输出用户发来的聊天内容 if (n > 0) { log("Read " + n + " bytes: " + new String(buf, 0, n) + " for session: " + request.getSession(true).getId()); } else if (n < 0) { error(event, request, response); return; } } while (is.available() > 0); } }
//该线程用来接收用户的消息,和向所有用户广播消息,应该不是很难 public class MessageSender implements Runnable { protected boolean running = true; protected ArrayList<String> messages = new ArrayList<String>(); public MessageSender() { } public void stop() { running = false; } /** * Add message for sending. */ public void send(String user, String message) { synchronized (messages) { messages.add("[" + user + "]: " + message); messages.notify(); } } public void run() { while (running) { if (messages.size() == 0) { try { synchronized (messages) { messages.wait(); } } catch (InterruptedException e) { // Ignore } } synchronized (connections) { String[] pendingMessages = null; synchronized (messages) { pendingMessages = messages.toArray(new String[0]); messages.clear(); } // 传说中的广播 for (int i = 0; i < connections.size(); i++) { try { PrintWriter writer = connections.get(i).getWriter(); for (int j = 0; j < pendingMessages.length; j++) { writer.println(pendingMessages[j] + "<br>"); } writer.flush(); } catch (IOException e) { log("IOExeption sending message", e); } } } } } } }