1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.http.impl.client.cache;
28
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.Set;
32 import java.util.concurrent.ArrayBlockingQueue;
33 import java.util.concurrent.ExecutorService;
34 import java.util.concurrent.RejectedExecutionException;
35 import java.util.concurrent.ThreadPoolExecutor;
36 import java.util.concurrent.TimeUnit;
37
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40 import org.apache.http.HttpHost;
41 import org.apache.http.client.cache.HttpCacheContext;
42 import org.apache.http.client.cache.HttpCacheEntry;
43 import org.apache.http.client.methods.HttpRequestWrapper;
44
45
46
47
48
49 class AsynchronousAsyncValidator {
50 private final CachingHttpAsyncClient cachingAsyncClient;
51 private final ExecutorService executor;
52 private final Set<String> queued;
53 private final CacheKeyGenerator cacheKeyGenerator;
54
55 private final Log log = LogFactory.getLog(getClass());
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public AsynchronousAsyncValidator(final CachingHttpAsyncClient cachingClient, final CacheConfig config) {
73 this(cachingClient, new ThreadPoolExecutor(config.getAsynchronousWorkersCore(),
74 config.getAsynchronousWorkersMax(), config.getAsynchronousWorkerIdleLifetimeSecs(),
75 TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(
76 config.getRevalidationQueueSize())));
77 }
78
79
80
81
82
83
84
85
86
87
88 AsynchronousAsyncValidator(final CachingHttpAsyncClient cachingClient, final ExecutorService executor) {
89 this.cachingAsyncClient = cachingClient;
90 this.executor = executor;
91 this.queued = new HashSet<String>();
92 this.cacheKeyGenerator = new CacheKeyGenerator();
93 }
94
95
96
97
98 public synchronized void revalidateCacheEntry(final HttpHost target, final HttpRequestWrapper request,
99 final HttpCacheContext clientContext, final HttpCacheEntry entry) {
100
101 final String uri = this.cacheKeyGenerator.getVariantURI(target, request, entry);
102
103 if (!this.queued.contains(uri)) {
104 final AsynchronousAsyncValidationRequest asyncRevalidationRequest = new AsynchronousAsyncValidationRequest(
105 this, this.cachingAsyncClient, target, request, clientContext, entry, uri);
106
107 try {
108 this.executor.execute(asyncRevalidationRequest);
109 this.queued.add(uri);
110 } catch (final RejectedExecutionException ree) {
111 this.log.debug("Revalidation for [" + uri + "] not scheduled: " + ree);
112 }
113 }
114 }
115
116
117
118
119
120
121
122 synchronized void markComplete(final String identifier) {
123 this.queued.remove(identifier);
124 }
125
126 Set<String> getScheduledIdentifiers() {
127 return Collections.unmodifiableSet(this.queued);
128 }
129
130 ExecutorService getExecutor() {
131 return this.executor;
132 }
133
134 }