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 2019 The JoyQueue Authors.
*
* 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.joyqueue.toolkit.delay;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.joyqueue.toolkit.concurrent.NamedThreadFactory;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class DelayedOperationManager {
private Timer timeoutTimer;
private String purgatoryName;
private int purgeInterval = 1000;
private int watchers = 512;
private ExecutorService taskExecutor;
private List watchersList;
// the number of estimated total operations in the purgatory
private AtomicInteger estimatedTotalOperations = new AtomicInteger(0);
/* background thread expiring operations that have timed out */
private ExpiredOperationReaper expirationReaper;
public DelayedOperationManager(final String purgatoryName) {
this(purgatoryName, 1000, true);
}
public DelayedOperationManager(final String purgatoryName, int purgeInterval, boolean reaperEnable) {
this.taskExecutor = Executors.newFixedThreadPool(1, new ThreadFactory() {
public Thread newThread(Runnable r) {
NamedThreadFactory threadFactory = new NamedThreadFactory("joyqueue-delayed-operation-executor-" + purgatoryName);
Thread thread = threadFactory.newThread(r);
return thread;
}
});
this.purgatoryName = purgatoryName;
this.timeoutTimer = new Timer(this.taskExecutor);
this.purgeInterval = purgeInterval;
this.watchersList = initWatchersList();
}
/**
* start the expire reaper thread
*/
public void start() {
expirationReaper = new ExpiredOperationReaper(String.format("ExpirationReaper-%s", purgatoryName));
expirationReaper.start();
}
/**
* Shutdown the expire reaper thread
*/
public void shutdown() {
if (expirationReaper != null) {
expirationReaper.shutdown();
}
if (taskExecutor != null) {
taskExecutor.shutdown();
}
}
protected List initWatchersList() {
List watchersList = Lists.newArrayListWithCapacity(watchers);
for (int i = 0; i < watchers; i++) {
watchersList.add(new WatchersList());
}
return watchersList;
}
protected WatchersList selectWatchersList(Object key) {
return watchersList.get(Math.abs(key.hashCode() % watchersList.size()));
}
/**
* Check if the operation can be completed, if not watch it based on the given watch keys
*
* Note that a delayed operation can be watched on multiple keys. It is possible that
* an operation is completed after it has been added to the watch list for some, but
* not all of the keys. In this case, the operation is considered completed and won't
* be added to the watch list of the remaining keys. The expiration reaper thread will
* remove this operation from any watcher list in which the operation exists.
*
* @param operation the delayed operation to be checked
* @param watchKeys keys for bookkeeping the operation
* @return true iff the delayed operations can be completed by the caller
*/
public boolean tryCompleteElseWatch(T operation, Set