问题描述
- tomcate cometProcessor 报错
-
我想做一个服务器每一秒主动向浏览器推送一个时间的demo.程序如下:servlet
public class CometTest extends HttpServlet implements CometProcessor { private static final long serialVersionUID = 1L; public CometTest() { super(); } @Override public void event(CometEvent ce) throws IOException, ServletException{ HttpServletRequest request = ce.getHttpServletRequest(); HttpServletResponse response = ce.getHttpServletResponse(); response.setHeader("Content-type", "text/html; charset=utf8"); if(ce.getEventType() == EventType.BEGIN){ HttpSession session = request.getSession(); System.out.println(session.getId()+"已初始化"); }else if(ce.getEventType() == EventType.READ){ Sender s = new Sender(response); s.start(); }else if(ce.getEventType() == EventType.ERROR){ System.out.println(ce.getEventSubType()); } } }
Sender(用于推送消息的线程类)
public class Sender extends Thread{ private HttpServletResponse response; private PrintWriter pw; public Sender(HttpServletResponse response) throws IOException{ this.response = response; pw = this.response.getWriter(); } @Override public void run() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while(true){ pw.write(sdf.format(new Date())); pw.flush(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript" src="jquery-1.9.1.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <script> function go() { $.ajax({ url : "http://localhost:8080/cometTest/comet", type : "post", dataType : "text", data : { "name" : "zdc" }, complete : function(xh, data) { $("#text1").html(xh.responseText); }, }); } </script> </head> <body> <input type="button" id="b1" onclick="go()" value="click" /> <label id="text1"></label><br/> </body> </html>
目的是进行循环的推时间到客户端,而结果是客户只收到一次推送后,服务器端报错(如下):
7719C3E7EC73EC3DEE51DA998BBFEC0F已初始化
IOEXCEPTION
九月 15, 2015 10:06:06 下午 org.apache.catalina.connector.CoyoteAdapter checkRecycled
信息: Encountered a non-recycled response and recycled it forcedly.
org.apache.catalina.connector.CoyoteAdapter$RecycleRequiredException
at org.apache.catalina.connector.CoyoteAdapter.checkRecycled(CoyoteAdapter.java:592)
at org.apache.coyote.http11.AbstractHttp11Processor.recycle(AbstractHttp11Processor.java:1814)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.release(Http11NioProtocol.java:218)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:703)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1753)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1712)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
解决方案
http://bdk82924.iteye.com/blog/1832746
解决方案二:
编程是一种美德,是促使一个人不断向上发展的一种原动力。