Search in sources :

Example 1 with IDLE_RESUME

use of org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_RESUME in project atmosphere by Atmosphere.

the class LifecycleHandler method on.

public LifecycleHandler on(final DefaultBroadcaster broadcaster) {
    final BroadcasterLifeCyclePolicy lifeCyclePolicy = broadcaster.getBroadcasterLifeCyclePolicy();
    final Collection<AtmosphereResource> resources = broadcaster.getAtmosphereResources();
    if (broadcaster.getID().contains("{") && broadcaster.getID().contains("}")) {
        logger.trace("Ignoring wildcard {} with lifecycle policy: {}", broadcaster.getID(), lifeCyclePolicy.getLifeCyclePolicy().name());
        return this;
    }
    final BroadcasterConfig bc = broadcaster.getBroadcasterConfig();
    final AtomicBoolean recentActivity = broadcaster.recentActivity();
    if (logger.isTraceEnabled()) {
        logger.trace("{} new lifecycle policy: {}", broadcaster.getID(), lifeCyclePolicy.getLifeCyclePolicy().name());
    }
    if (broadcaster.currentLifecycleTask() != null) {
        broadcaster.currentLifecycleTask().cancel(false);
    }
    ScheduledExecutorService scheduler = bc.getScheduledExecutorService();
    if (scheduler == null) {
        logger.error("No Broadcaster's SchedulerExecutorService has been configured on {}. BroadcasterLifeCyclePolicy won't work.", broadcaster.getID());
        return this;
    }
    if (lifeCyclePolicy.getLifeCyclePolicy() == IDLE || lifeCyclePolicy.getLifeCyclePolicy() == IDLE_RESUME || lifeCyclePolicy.getLifeCyclePolicy() == IDLE_DESTROY || lifeCyclePolicy.getLifeCyclePolicy() == IDLE_EMPTY_DESTROY) {
        recentActivity.set(false);
        int time = lifeCyclePolicy.getTimeout();
        if (time == -1) {
            throw new IllegalStateException("BroadcasterLifeCyclePolicy time is not set");
        }
        logger.debug("{} new lifecycle policy: {} [expire {} in {}]", broadcaster.getID(), lifeCyclePolicy.getLifeCyclePolicy().name(), time, lifeCyclePolicy.getLifeCyclePolicy());
        Future<?> currentLifecycleTask = scheduler.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                try {
                    // Check for activity since the last execution.
                    if (recentActivity.getAndSet(false)) {
                        return;
                    } else if (lifeCyclePolicy.getLifeCyclePolicy() == IDLE) {
                        notifyIdleListener(broadcaster);
                        broadcaster.releaseExternalResources();
                        logger.debug("Applying BroadcasterLifeCyclePolicy IDLE policy to Broadcaster {}", broadcaster.getID());
                        if (broadcaster.currentLifecycleTask() != null) {
                            broadcaster.currentLifecycleTask().cancel(true);
                        }
                    } else if (lifeCyclePolicy.getLifeCyclePolicy() == IDLE_DESTROY) {
                        notifyIdleListener(broadcaster);
                        destroy(false);
                        logger.debug("Applying BroadcasterLifeCyclePolicy IDLE_DESTROY policy to Broadcaster {}", broadcaster.getID());
                    } else if (lifeCyclePolicy.getLifeCyclePolicy() == IDLE_RESUME) {
                        notifyIdleListener(broadcaster);
                        destroy(true);
                        logger.debug("Applying BroadcasterLifeCyclePolicy IDLE_RESUME policy to Broadcaster {}", broadcaster.getID());
                    } else if (resources.isEmpty() && lifeCyclePolicy.getLifeCyclePolicy() == IDLE_EMPTY_DESTROY) {
                        notifyIdleListener(broadcaster);
                        destroy(false);
                        logger.debug("Applying BroadcasterLifeCyclePolicy IDLE_EMPTY_DESTROY policy to Broadcaster {}", broadcaster.getID());
                    }
                } catch (Throwable t) {
                    if (broadcaster.isDestroyed()) {
                        logger.trace("Scheduled BroadcasterLifeCyclePolicy exception", t);
                    } else {
                        logger.warn("Scheduled BroadcasterLifeCyclePolicy exception", t);
                    }
                }
            }

            void destroy(boolean resume) {
                if (resume) {
                    logger.info("All AtmosphereResource will now be resumed from Broadcaster {}", broadcaster.getID());
                    broadcaster.resumeAll();
                } else {
                    broadcaster.getAtmosphereResources().parallelStream().forEach(f -> {
                        try {
                            f.close();
                        } catch (IOException e) {
                            logger.trace("", e);
                        }
                    });
                }
                broadcaster.destroy();
                /**
                 * The value may be null if the timeout is too low. Hopefully next execution will
                 * cancel the task properly.
                 */
                if (broadcaster.currentLifecycleTask() != null) {
                    broadcaster.currentLifecycleTask().cancel(true);
                }
            }
        }, time, time, lifeCyclePolicy.getTimeUnit());
        broadcaster.currentLifecycleTask(currentLifecycleTask);
    }
    return this;
}
Also used : BroadcasterLifeCyclePolicy(org.atmosphere.cpr.BroadcasterLifeCyclePolicy) Logger(org.slf4j.Logger) Broadcaster(org.atmosphere.cpr.Broadcaster) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) BroadcasterConfig(org.atmosphere.cpr.BroadcasterConfig) DefaultBroadcaster(org.atmosphere.cpr.DefaultBroadcaster) EMPTY_DESTROY(org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.EMPTY_DESTROY) Future(java.util.concurrent.Future) IDLE(org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IDLE_RESUME(org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_RESUME) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) BroadcasterLifeCyclePolicyListener(org.atmosphere.cpr.BroadcasterLifeCyclePolicyListener) IDLE_DESTROY(org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_DESTROY) IDLE_EMPTY_DESTROY(org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_EMPTY_DESTROY) EMPTY(org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.EMPTY) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) BroadcasterLifeCyclePolicy(org.atmosphere.cpr.BroadcasterLifeCyclePolicy) AtmosphereResource(org.atmosphere.cpr.AtmosphereResource) BroadcasterConfig(org.atmosphere.cpr.BroadcasterConfig) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

IOException (java.io.IOException)1 Collection (java.util.Collection)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtmosphereResource (org.atmosphere.cpr.AtmosphereResource)1 Broadcaster (org.atmosphere.cpr.Broadcaster)1 BroadcasterConfig (org.atmosphere.cpr.BroadcasterConfig)1 BroadcasterLifeCyclePolicy (org.atmosphere.cpr.BroadcasterLifeCyclePolicy)1 EMPTY (org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.EMPTY)1 EMPTY_DESTROY (org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.EMPTY_DESTROY)1 IDLE (org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE)1 IDLE_DESTROY (org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_DESTROY)1 IDLE_EMPTY_DESTROY (org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_EMPTY_DESTROY)1 IDLE_RESUME (org.atmosphere.cpr.BroadcasterLifeCyclePolicy.ATMOSPHERE_RESOURCE_POLICY.IDLE_RESUME)1 BroadcasterLifeCyclePolicyListener (org.atmosphere.cpr.BroadcasterLifeCyclePolicyListener)1 DefaultBroadcaster (org.atmosphere.cpr.DefaultBroadcaster)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1