Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015 Async-IO.org
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.atmosphere.interceptor;
import org.atmosphere.cpr.Action;
import org.atmosphere.cpr.AtmosphereConfig;
import org.atmosphere.cpr.AtmosphereInterceptor;
import org.atmosphere.cpr.AtmosphereResource;
import org.atmosphere.cpr.AtmosphereResourceEvent;
import org.atmosphere.cpr.AtmosphereResourceEventImpl;
import org.atmosphere.cpr.AtmosphereResourceImpl;
import org.atmosphere.cpr.Broadcaster;
import org.atmosphere.cpr.BroadcasterCache;
import org.atmosphere.cpr.BroadcasterFactory;
import org.atmosphere.cpr.BroadcasterListenerAdapter;
import org.atmosphere.util.ExecutorsFactory;
import org.atmosphere.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.atmosphere.cpr.ApplicationConfig.STATE_RECOVERY_TIMEOUT;
import static org.atmosphere.cpr.AtmosphereResourceEventListenerAdapter.OnSuspend;
/**
* This interceptor associates a {@link AtmosphereResource} to all {@link Broadcaster} the resource was added before
* the underlying connection got closed and resume. This allow an application to restore the state of the client before the
* disconnection occurred, and for the long-polling transport to return to it's previous state.
*
* @author Jeanfrancois Arcand
*/
public class AtmosphereResourceStateRecovery implements AtmosphereInterceptor {
private final static Logger logger = LoggerFactory.getLogger(AtmosphereResourceStateRecovery.class);
private final ConcurrentHashMap states = new ConcurrentHashMap();
private BroadcasterFactory factory;
private ScheduledExecutorService stateTracker;
private long timeout = 5 * 1000 * 60;
private Future> trackerFuture;
@Override
public void configure(AtmosphereConfig config) {
factory = config.getBroadcasterFactory();
factory.addBroadcasterListener(new B());
stateTracker = ExecutorsFactory.getScheduler(config);
String s = config.getInitParameter(STATE_RECOVERY_TIMEOUT);
if (s != null) {
timeout = Long.parseLong(s);
}
startStateTracker();
logger.trace("{} started.", AtmosphereResourceStateRecovery.class.getName());
}
public AtmosphereResourceStateRecovery timeout(long timeout){
this.timeout = timeout;
trackerFuture.cancel(false);
startStateTracker();
return this;
}
public long timeout(){
return timeout;
}
protected void startStateTracker() {
trackerFuture = stateTracker.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
long now = System.currentTimeMillis();
for (Map.Entry t : states.entrySet()) {
// The resource may still be suspended but we don't want to keep a reference to it, so we swap
// the state and will recover.
if (now - t.getValue().lastTick() > timeout) {
logger.trace("AtmosphereResource {} state destroyed.", t.getKey());
states.remove(t.getKey());
}
}
}
}, timeout, timeout, TimeUnit.MILLISECONDS);
}
@Override
public Action inspect(final AtmosphereResource r) {
if (!Utils.pollableTransport(r.transport()) && !Utils.webSocketMessage(r)){
final BroadcasterTracker tracker = track(r).tick();
List