Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.grizzly.comet;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.CloseType;
import org.glassfish.grizzly.Closeable;
import org.glassfish.grizzly.CompletionHandler;
import org.glassfish.grizzly.Connection;
import org.glassfish.grizzly.GenericCloseListener;
import org.glassfish.grizzly.ReadHandler;
import org.glassfish.grizzly.http.io.NIOInputStream;
import org.glassfish.grizzly.http.server.Request;
import org.glassfish.grizzly.http.server.Response;
import org.glassfish.grizzly.http.server.TimeoutHandler;
import org.glassfish.grizzly.http.util.Header;
/**
* The main object used by {@link CometHandler} and Servlet to push information amongst suspended request/response. The
* {@link CometContext} is always available for {@link CometHandler} and can be used to {@link #notify}, or share
* information with other {@link CometHandler}. This is the equivalent of server push as the CometContext will invoke
* all registered CometHandler ({@link #addCometHandler}) sequentially.
*
*
* A CometContext can be considered as a topic where CometHandler register for information. A CometContext can be shared
* amongst Servlet of the same application, or globally across all deployed web applications. Normally, a CometContext
* is created using a topic's name like:
*
*
*
*
* CometEngine ce = CometEngine.getEngine();
* CometContext cc = ce.registerContext("MyTopic");
*
* and then inside a Servlet.service() method, you just need to call:
*
* cc.addCometListener(myNewCometListener());
* cc.notify("I'm pushing data to all registered CometHandler");
*
*
*
*
* As soon as {@link #addCometHandler} is invoked, Grizzly will automatically suspend the
* request/response (will not commit the response). A response can be resumed by invoking
* {@link #resumeCometHandler}, which will automatically commit the response and remove the associated CometHandler from
* the CometContext.
*
*
* A CometContext uses a {@link NotificationHandler} to invoke, using the calling thread or a Grizzly thread pool, all
* CometHandler than have been added using the {@link #addCometHandler}. A {@link NotificationHandler} can be used to
* filter or transform the content that will eventually be pushed back to all connected clients. You can also use a
* {@link NotificationHandler} to throttle push like invoking only a subset of the CometHandler, etc.
*
*
* Idle suspended connection can be timed out by configuring the {@link #setExpirationDelay(long)}. The value needs to
* be in milliseconds. If there is no I/O operations and no invocation of {@link #notify} during the expiration delay,
* Grizzly will resume all suspended connection. An application will have a chance to send back data using the
* connection as Grizzly will invoke the {@link CometHandler#onInterrupt} before resuming the connection. Note that
* setting the expiration delay to -1 disable the above mechanism, e.g. idle connection will never get resumed by
* Grizzly.
*
*
* Attributes can be added/removed the same way HttpServletSession is doing. It is not recommended to use attributes if
* this {@link CometContext} is not shared amongst multiple context path (uses HttpServletSession instead).
*
*/
public class CometContext {
/**
* Generic error message
*/
protected final static String INVALID_COMET_HANDLER = "CometHandler cannot be null. "
+ "This CometHandler was probably resumed and an invalid reference was made to it.";
protected final static String ALREADY_REMOVED = "CometHandler already been removed or invalid.";
private final static String COMET_NOT_ENABLED = "Make sure you have enabled Comet or make sure the thread"
+ " invoking that method is the same as the Servlet.service() thread.";
protected final static Logger LOGGER = Logger.getLogger(CometContext.class.getName());
private final Map