private CometContext createCometContext(String channel){ CometContext cc = CometEngine.getEngine().register(channel); cc.setExpirationDelay(-1); cc.setBlockingNotification(true); return cc; }
public Set<DataHandler> lookupClientHandlers(String channelId) { final CometContext context = CometEngine.getEngine().getCometContext(channel); if (context != null) { return context.getCometHandlers(); } return Collections.EMPTY_SET; }
/** * Resume the connection. * @throws java.io.IOException */ private void removeThisFromContext() throws IOException { cometContext.removeCometHandler(this); } }
@SuppressWarnings("unchecked") public void onEvent(CometEvent event) throws IOException{ Object obj = event.attachment(); try{ if (obj instanceof DeliverResponse){ ended = deliverRes.isLast(); if (deliverRes.isFinished()){ event.getCometContext().resumeCometHandler(this); ended = true; event.getCometContext().resumeCometHandler(this);
CometContext context = CometEngine.getEngine().getCometContext(topic); if (context == null && suspendOnTheFly) { context = createCometContext(topic); CometType cometType = CometType.LONG_POLLING; 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); } } }
/** * 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); } }
public CometEvent(final com.sun.grizzly.comet.CometEvent<E> copyEvent) { this.type = copyEvent.getType(); this.attachment = copyEvent.attachment(); this.cometContext = (CometContext) copyEvent.getCometContext(); }
/** * {@inheritDoc} */ public void action(AtmosphereEventImpl actionEvent){ if (actionEvent.action().type == Action.TYPE.RESUME){ CometContext ctx = CometEngine.getEngine().getCometContext(atmosphereCtx); resume(actionEvent.getRequest(), ctx); } }
/** * 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); } }
/** * 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)); }
public int addCometHandler(CometHandler handler, boolean alreadySuspended) { return super.addCometHandler(wrapCometHandler(handler), alreadySuspended); }
/** * {@inheritDoc} */ @Override protected void setThreadPool(ExecutorService threadPool){ super.setThreadPool(threadPool); } }
@Override public SelectorThread createSelectorThread(String[] args) throws Exception{ SelectorThread st = super.createSelectorThread(args); st.setEnableAsyncExecution(true); AsyncHandler asyncHandler = new DefaultAsyncHandler(); asyncHandler.addAsyncFilter(new CometAsyncFilter()); st.setAsyncHandler(asyncHandler); return st; }
CometContext context = CometEngine.getEngine().getCometContext(topic); if (context == null) { context = createCometContext(topic); cometHandler.attach(res); context.notify(message); context.addCometHandler(cometHandler); return cometType;
/** * 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; }
/** * {@inheritDoc} */ public Action service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { CometContext ctx = CometEngine.getEngine().getCometContext(atmosphereCtx); Action action = suspended(req, res); if (action.type == Action.TYPE.SUSPEND) { if (logger.isLoggable(Level.FINE)){ logger.fine("Suspending" + res); } suspend(ctx, action, req, res); } else if (action.type == Action.TYPE.RESUME){ if (logger.isLoggable(Level.FINE)){ logger.fine("Resuming" + res); } resume(req,ctx); Action nextAction = resumed(req, res); if (nextAction.type == Action.TYPE.SUSPEND) { if (logger.isLoggable(Level.FINE)){ logger.fine("Suspending after Resuming" + res); } suspend(ctx, action, req, res); } } return action; }
public boolean removeCometHandler(CometHandler handler, boolean resume) { return super.removeCometHandler(wrapCometHandler(handler), resume); }