use of org.atmosphere.cpr.DefaultBroadcaster 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;
}
use of org.atmosphere.cpr.DefaultBroadcaster in project atmosphere by Atmosphere.
the class BroadcasterLifecyclePolicyHandler method onPostCreate.
/**
* {@inheritDoc}
*/
@Override
public void onPostCreate(Broadcaster b) {
if (DefaultBroadcaster.class.isAssignableFrom(b.getClass())) {
DefaultBroadcaster broadcaster = DefaultBroadcaster.class.cast(b);
broadcaster.lifecycleHandler(liferCycleHandler.on(broadcaster));
}
}
Aggregations