alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (CometServlet.java)

This example Jetty source code file (CometServlet.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

cometservlet, continuation, continuation, http, httpservlet, httpservletresponse, httpservletresponse, integer, io, ioexception, ioexception, request, response, servlet, servletexception, servletexception, unsupportedoperationexception

The Jetty CometServlet.java source code

package com.acme;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mortbay.util.ajax.Continuation;
import org.mortbay.util.ajax.ContinuationSupport;

/** CometServlet
 * This servlet implements the Comet API from tc6.x with the exception of the read method.
 * 
 * @author gregw
 *
 */
public class CometServlet extends HttpServlet
{
    public void begin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        request.setAttribute("org.apache.tomcat.comet",Boolean.TRUE);
    }

    public void end(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        synchronized(request)
        {
            request.removeAttribute("org.apache.tomcat.comet");
            
            Continuation continuation=ContinuationSupport.getContinuation(request,request);
            if (continuation.isPending())
                continuation.resume();
        }
    }

    public void error(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        end(request,response);
    }

    public boolean read(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        throw new UnsupportedOperationException();
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        synchronized(request)
        {
            // TODO: wrap response so we can reset timeout on writes.
            
            Continuation continuation=ContinuationSupport.getContinuation(request,request);
            
            if (!continuation.isPending())
                begin(request,response);
            
            Integer timeout=(Integer)request.getAttribute("org.apache.tomcat.comet.timeout");
            boolean resumed=continuation.suspend(timeout==null?60000:timeout.intValue());
            
            if (!resumed)
                error(request,response);
        }
    }

    public void setTimeout(HttpServletRequest request, HttpServletResponse response, int timeout) throws IOException, ServletException,
            UnsupportedOperationException
    {
        request.setAttribute("org.apache.tomcat.comet.timeout",new Integer(timeout));
    }
}

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty CometServlet.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.