浏览 6515 次
锁定老帖子 主题:glassfish服务器推送技术
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-04-18
昨天就了一下glassfish例子 ,与大家分享一下 新建一个servlet: package org.comet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.enterprise.web.connector.grizzly.comet.CometContext; import com.sun.enterprise.web.connector.grizzly.comet.CometEngine; public class CometChat extends HttpServlet { /** * Constructor of the object. */ CometEngine engine = null; CometContext metaContext = null; public CometChat() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CometResponseHandler handler = null; response.setContentType("text/plain"); response.setStatus(HttpServletResponse.SC_OK); if(request.getRequestURI().equals(request.getContextPath()+"/servlet/CometChat")){ handler = new CometResponseHandler(request.getContextPath()+"/servlet/CometChat"); handler.attach(response); metaContext.addCometHandler(handler); } PrintWriter writer = response.getWriter(); writer.flush(); writer.println("---START---"); writer.flush(); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here super.init(); engine = CometEngine.getEngine(); metaContext = engine.register("/servlet/CometChat"); Thread t = new Thread() { public int i = 0; public void run() { while (true) { try { Thread.sleep(10000); metaContext.notify(new Integer(i++)); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); return; } } } }; t.start(); } } 然后有一个JavaBean package org.comet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse; import com.sun.enterprise.web.connector.grizzly.comet.CometContext; import com.sun.enterprise.web.connector.grizzly.comet.CometEngine; import com.sun.enterprise.web.connector.grizzly.comet.CometEvent; import com.sun.enterprise.web.connector.grizzly.comet.CometHandler; public class CometResponseHandler implements CometHandler<HttpServletResponse> { private HttpServletResponse httpServletResponse; private String contextPath = null; public CometResponseHandler(String contextPath) { } public void attach(HttpServletResponse httpServletResponse) { this.httpServletResponse = httpServletResponse; } public void onEvent(CometEvent event) throws IOException { System.out.println("==== onEvent ====="); PrintWriter printWriter = httpServletResponse.getWriter(); printWriter.println("Handler:" + this.toString() + " - event{type:\"" + event.getType() + "\",\"" + event.attachment().toString() + "\"} IP="); printWriter.flush(); } public void onInitialize(CometEvent event) throws IOException { System.out.println("==== onInitialize ====="); } public void onTerminate(CometEvent event) throws IOException { System.out.println("==== onTerminate ====="); onInterrupt(event); } public void onInterrupt(CometEvent event) throws IOException { System.out.println("==== onInterrupt ====="); CometEngine cometEngine = CometEngine.getEngine(); CometContext cometContext = cometEngine.getCometContext(contextPath); cometContext.removeCometHandler(this); } } deploy在glassfish吧 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-04-28
这其实是http协议的一个特性,估计是被glassfish封装了吧
但是移植怎么办 |
|
返回顶楼 | |
发表时间:2008-04-29
目前这类的方案都是不可移植,也其它的服务器已经移植了Comet。如resin等。
Servlet 3.0应该会添加这方面的等价物。 |
|
返回顶楼 | |
发表时间:2008-05-04
如果移植的确不行,但是你可以考虑使用lightStreamer,也是一个comet开发包,它也支持.net平台
|
|
返回顶楼 | |
发表时间:2008-09-04
我想问你com.sun.enterprise.web.connector.grizzly.comet的API说明文档在哪儿可以下到
|
|
返回顶楼 | |
发表时间:2008-09-05
很好,我们公司也在考虑换glassfish,就是为了推..
|
|
返回顶楼 | |