/** * Suspend the response */ private void suspend(CometContext ctx,Action action,HttpServletRequest req, HttpServletResponse res ){ VoidCometHandler c = new VoidCometHandler(req, res); ctx.setExpirationDelay(action.timeout); ctx.addCometHandler(c); req.getSession().setAttribute(ATMOSPHERE, c.hashCode()); }
/** * Resume the underlying response, * @param req an {@link HttpServletRequest} * @param ctx a {@link CometContext} */ private void resume(HttpServletRequest req, CometContext ctx){ CometHandler handler = ctx .getCometHandler((Integer)req.getSession().getAttribute(ATMOSPHERE)); req.getSession().removeAttribute(ATMOSPHERE); if (handler != null){ ctx.resumeCometHandler(handler); } }
private void notifyAll(Object obj, String channel) throws IOException { if (enforceSubscriptionUnderPush){ for (Entry<String,CometContext> entry:activeCometContexts.entrySet()){ entry.getValue().notify(obj); } } else { CometContext cc = getCometContext(channel); // not applying standard logging rules for // this message as it's not displayed unless // a system property is explicitly set. if (logger.isLoggable(level)){ log("Notifying " + channel + " to " + cc.getCometHandlers().size() + " CometHandler with message\n" + obj); } cc.notify(obj); } }
/** * Create a Grizzly Comet {@link CometContext} * @param topic The name of the {@link CometContext} * @return a cached or newly created {@link CometContext} */ protected final CometContext createCometContext(String topic){ CometContext context = CometEngine.getEngine().register(topic); if (notificationHandler != null){ context.setNotificationHandler(notificationHandler); } context.setExpirationDelay(expirationDelay); context.setBlockingNotification(true); return context; }
context.notify(message); context.addCometHandler(cometHandler); return cometType;
/** * Resume the Comet request and remove it from the active {@link CometHandler} list. Once resumed, * a CometHandler must never manipulate the <code>HttpServletRequest</code> or <code>HttpServletResponse</code> as * those object will be recycled and may be re-used to serve another request. * * If you cache them for later reuse by another thread there is a * possibility to introduce corrupted responses next time a request is made. * @param handler The CometHandler to resume. * @return <tt>true</tt> if the operation succeeded. */ public void resumeCometHandler(CometHandler handler) { super.resumeCometHandler(wrapCometHandler(handler)); }
private CometContext createCometContext(String channel){ CometContext cc = CometEngine.getEngine().register(channel); cc.setExpirationDelay(-1); cc.setBlockingNotification(true); return cc; }
for (String channel: dataHandler.getChannels()){ CometContext cc = getCometContext(channel); if (!cc.isActive(dataHandler)){ cc.addCometHandler(dataHandler); dataHandler.setSuspended(true);
public int addCometHandler(CometHandler handler, boolean alreadySuspended) { return super.addCometHandler(wrapCometHandler(handler), alreadySuspended); }
/** * Init Grizzly's {@link CometContext} that will be used to suspend and * resume the response. * @param sc the {@link ServletContext} * @throws javax.servlet.ServletException */ @Override public void init(ServletConfig sc) throws ServletException{ super.init(sc); atmosphereCtx = sc.getServletContext().getContextPath() + ATMOSPHERE; CometEngine cometEngine = CometEngine.getEngine(); CometContext context = cometEngine.register(atmosphereCtx); context.setExpirationDelay(-1); if (logger.isLoggable(Level.FINE)){ logger.fine("Created CometContext for " + atmosphereCtx); } }
/** * Resume the connection. * @throws java.io.IOException */ private void removeThisFromContext() throws IOException { cometContext.removeCometHandler(this); } }
CometHandler cometHandler = new MessagesBusCometHandler(context, cometType); cometHandler.attach(res); context.addCometHandler(cometHandler); } else if (!suspendOnTheFly) { if (logger.isLoggable(Level.INFO)) { context.notify(message);
/** * Invoked when a {@link CometContext#notify} is executing a push. * @param event * @throws java.io.IOException */ public void onEvent(CometEvent event) throws IOException { if (event.getType() == CometEvent.NOTIFY) { String output = (String) event.attachment(); response.getWriter().println(output); response.getWriter().flush(); if (cometType == CometType.LONG_POLLING){ cometContext.resumeCometHandler(this); } } }
public boolean removeCometHandler(CometHandler handler, boolean resume) { return super.removeCometHandler(wrapCometHandler(handler), resume); }
ended = deliverRes.isLast(); if (deliverRes.isFinished()){ event.getCometContext().resumeCometHandler(this); ended = true; event.getCometContext().resumeCometHandler(this);