glassfish服务器推送技术

阅读更多

昨天就了一下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. 
*/ 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.
* * @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
{
	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吧

你可能感兴趣的:(Glassfish,Comet,Servlet,Web,SUN)