Guava 的加载缓存可以为您解决超时和并发修改:https : //code.google.com/p/guava-libraries/wiki/CachesExplained 通过如下设置将您的请求映射交换为 LoadingCache:
LoadingCache<Request, Connection> requests = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterAccess(1, TimeUnit.MINUTES)
.removalListener(MY_LISTENER)
.build(
new CacheLoader<Request, Connection>() {
public Connection load(Request request) throws AnyException {
return clientConnectionForRequest(request);
}
});
当一个请求进来时,你将它加载到缓存中:
requests.get(request);
在此之后,请求将坐在那里等待处理。如果开始处理,则获取连接并使请求无效,因此将其从缓存中删除。①</p>
Connection c = requests.getIfPresent(request);
if (c != null) {
requests.invalidate(request); // remove from the waiting area
// proceeed with processing the request
} else {
// the request was evicted from the cache as it expired
}
在删除侦听器中,您需要实现一些简单的逻辑来侦听驱逐。(如果您明确地使无效,那么wasEvicted()
将返回 false。)
MY_LISTENER = new RemovalListener<Request, Connection>() {
@Override
public void onRemovaRequest RemovalNotification<Request, Connection> notification) {
if (notification.wasEvicted()) {
Connection c = notification.getValue();
// send timeout response to client
}
}
};
您可以通过将请求放入队列并执行①中描述的方法来对请求进行排序,该方法还将负责仅执行那些尚未超时的请求,您不需要额外的内部维护。