public synchronized void cancel() throws IOException {
    action.type(Action.TYPE.RESUME);
    if (isCancelled.getAndSet(true)) return;

    if (asyncSupport instanceof AsynchronousProcessor) {
      try {
        AsynchronousProcessor.class.cast(asyncSupport).resumed(req, response);
      } catch (ServletException e) {
        logger.trace("", e);
      }
    }
    asyncSupport.action(this);
    // We must close the underlying WebSocket as well.
    if (AtmosphereResponse.class.isAssignableFrom(response.getClass())) {
      AtmosphereResponse.class.cast(response).close();
      AtmosphereResponse.class.cast(response).destroy();
    }

    if (AtmosphereRequest.class.isAssignableFrom(req.getClass())) {
      AtmosphereRequest.class.cast(req).destroy();
    }

    // TODO: Grab some measurement.
    //        req = null;
    //        response = null;

    // Just in case
    if (broadcaster != null) {
      broadcaster.removeAtmosphereResource(this);
    }
    event.destroy();
  }
  @Test
  public void testEmptyDestroy() throws IOException, ServletException {
    Broadcaster b = framework.getBroadcasterFactory().lookup(B.class, "/test", true);
    b.setBroadcasterLifeCyclePolicy(BroadcasterLifeCyclePolicy.EMPTY_DESTROY);
    AR ah = new AR();

    framework.addAtmosphereHandler("/*", ah, b).init();
    AtmosphereRequest request =
        new AtmosphereRequestImpl.Builder().pathInfo("/a").method("GET").build();
    framework.doCometSupport(request, AtmosphereResponseImpl.newInstance());
    b.removeAtmosphereResource(ah.resource);

    assertFalse(B.class.cast(b).releaseExternalResources.get());
    assertTrue(B.class.cast(b).destroy.get());
  }
 /** {@inheritDoc} */
 @Override
 public void removeAllAtmosphereResource(AtmosphereResource r) {
   // Remove inside all Broadcaster as well.
   try {
     if (store.size() > 0) {
       for (Broadcaster b : lookupAll()) {
         try {
           // Prevent deadlock
           if (b.getAtmosphereResources().contains(r)) {
             b.removeAtmosphereResource(r);
           }
         } catch (IllegalStateException ex) {
           logger.trace(ex.getMessage(), ex);
         }
       }
     }
   } catch (Exception ex) {
     logger.warn(ex.getMessage(), ex);
   }
 }
Пример #4
0
  public void cancel() throws IOException {

    if (!isCancelled.getAndSet(true)) {
      logger.trace("Cancelling {}", uuid);
      action.type(Action.TYPE.RESUME);
      asyncSupport.action(this);
      // We must close the underlying WebSocket as well.
      if (AtmosphereResponse.class.isAssignableFrom(response.getClass())) {
        AtmosphereResponse.class.cast(response).close();
        AtmosphereResponse.class.cast(response).destroy();
      }

      if (AtmosphereRequest.class.isAssignableFrom(req.getClass())) {
        AtmosphereRequest.class.cast(req).destroy();
      }

      if (broadcaster != null) {
        broadcaster.removeAtmosphereResource(this);
      }
      event.destroy();
    }
  }
  @Override
  public AtmosphereResource resume() {

    if (!isSuspended()) {
      logger.warn("AtmosphereResource {} not suspend, cannot resume it.", uuid());
      return this;
    }

    try {
      if (!isResumed.getAndSet(true) && isInScope.get()) {
        logger.trace("AtmosphereResource {} is resuming", uuid());

        action.type(Action.TYPE.RESUME);

        // We need it as Jetty doesn't support timeout
        Broadcaster b = getBroadcaster(false);
        if (!b.isDestroyed() && b instanceof DefaultBroadcaster) {
          ((DefaultBroadcaster) b).broadcastOnResume(this);
        }

        notifyListeners();

        try {
          if (!b.isDestroyed()) {
            broadcaster.removeAtmosphereResource(this);
          }
        } catch (IllegalStateException ex) {
          logger.warn("Unable to resume", this);
          logger.debug(ex.getMessage(), ex);
        }

        if (b.getScope() == Broadcaster.SCOPE.REQUEST) {
          logger.debug("Broadcaster's scope is set to request, destroying it {}", b.getID());
          b.destroy();
        }

        // Resuming here means we need to pull away from all other Broadcaster, if they exists.
        if (config.getBroadcasterFactory() != null) {
          config.getBroadcasterFactory().removeAllAtmosphereResource(this);
        }

        try {
          req.setAttribute(ApplicationConfig.RESUMED_ON_TIMEOUT, Boolean.FALSE);
        } catch (Exception ex) {
          logger.debug("Resume exception: Cannot resume an already resumed/cancelled request", ex);
        } finally {
          try {
            Meteor m = (Meteor) req.getAttribute(METEOR);
            if (m != null) {
              m.destroy();
            }
          } catch (Exception ex) {
            logger.debug(
                "Meteor resume exception: Cannot resume an already resumed/cancelled request", ex);
          }
        }

        if (req.getAttribute(PRE_SUSPEND) == null) {
          asyncSupport.action(this);
        }
      } else {
        logger.trace("Already resumed {}", this);
        return this;
      }
    } catch (Throwable t) {
      logger.trace("Wasn't able to resume a connection {}", this, t);
    }
    listeners.clear();
    return this;
  }
 @AfterMethod
 public void unSetUp() throws Exception {
   broadcaster.removeAtmosphereResource(ar);
   atmosphereHandler.value.set(new HashSet());
 }
  /** {@inheritDoc} */
  @Override
  public synchronized AtmosphereResource resume() {
    // We need to synchronize the method because the resume may occurs at the same time a message is
    // published
    // and we will miss that message. The DefaultBroadcaster synchronize on that method before
    // writing a message.
    try {
      if (!isResumed && isInScope) {
        action.type(Action.TYPE.RESUME);
        isResumed = true;

        // We need it as Jetty doesn't support timeout
        Broadcaster b = getBroadcaster(false);
        if (!b.isDestroyed() && b instanceof DefaultBroadcaster) {
          ((DefaultBroadcaster) b).broadcastOnResume(this);
        }

        notifyListeners();

        try {
          if (!b.isDestroyed()) {
            broadcaster.removeAtmosphereResource(this);
          }
        } catch (IllegalStateException ex) {
          logger.warn("Unable to resume", this);
          logger.debug(ex.getMessage(), ex);
        }

        if (b.getScope() == Broadcaster.SCOPE.REQUEST) {
          logger.debug("Broadcaster's scope is set to request, destroying it {}", b.getID());
          b.destroy();
        }

        // Resuming here means we need to pull away from all other Broadcaster, if they exists.
        if (config.getBroadcasterFactory() != null) {
          config.getBroadcasterFactory().removeAllAtmosphereResource(this);
        }

        try {
          req.setAttribute(ApplicationConfig.RESUMED_ON_TIMEOUT, Boolean.FALSE);
        } catch (Exception ex) {
          logger.debug("Resume exception: Cannot resume an already resumed/cancelled request", ex);
        } finally {
          try {
            Meteor m = (Meteor) req.getAttribute(METEOR);
            if (m != null) {
              m.destroy();
            }
          } catch (Exception ex) {
            logger.debug(
                "Meteor resume exception: Cannot resume an already resumed/cancelled request", ex);
          }
        }

        if (req.getAttribute(PRE_SUSPEND) == null) {
          asyncSupport.action(this);
        }
      } else {
        logger.debug("Cannot resume an already resumed/cancelled request {}", this);
      }
    } catch (Throwable t) {
      logger.trace("Wasn't able to resume a connection {}", this, t);
    }
    notifyListeners(new AtmosphereResourceEventImpl(this, true, false));
    listeners.clear();
    return this;
  }