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.hc.client5.http.impl.cache;
28
29 import java.io.IOException;
30 import java.util.concurrent.ScheduledExecutorService;
31
32 import org.apache.hc.client5.http.schedule.SchedulingStrategy;
33 import org.apache.hc.core5.http.ClassicHttpResponse;
34 import org.apache.hc.core5.http.HttpException;
35 import org.apache.hc.core5.http.HttpStatus;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43 class DefaultCacheRevalidator extends CacheRevalidatorBase {
44
45 private static final Logger LOG = LoggerFactory.getLogger(DefaultCacheRevalidator.class);
46
47 interface RevalidationCall {
48
49 ClassicHttpResponse execute() throws IOException, HttpException;
50 }
51
52
53
54
55
56 public DefaultCacheRevalidator(
57 final CacheRevalidatorBase.ScheduledExecutor scheduledExecutor,
58 final SchedulingStrategy schedulingStrategy) {
59 super(scheduledExecutor, schedulingStrategy);
60 }
61
62
63
64
65
66 public DefaultCacheRevalidator(
67 final ScheduledExecutorService scheduledThreadPoolExecutor,
68 final SchedulingStrategy schedulingStrategy) {
69 this(wrap(scheduledThreadPoolExecutor), schedulingStrategy);
70 }
71
72
73
74
75 public void revalidateCacheEntry(
76 final String cacheKey,
77 final RevalidationCall call) {
78 scheduleRevalidation(cacheKey, () -> {
79 try (ClassicHttpResponse httpResponse = call.execute()) {
80 if (httpResponse.getCode() < HttpStatus.SC_SERVER_ERROR) {
81 jobSuccessful(cacheKey);
82 } else {
83 jobFailed(cacheKey);
84 }
85 } catch (final IOException ex) {
86 jobFailed(cacheKey);
87 LOG.debug("Asynchronous revalidation failed due to I/O error", ex);
88 } catch (final HttpException ex) {
89 jobFailed(cacheKey);
90 LOG.error("HTTP protocol exception during asynchronous revalidation", ex);
91 } catch (final RuntimeException ex) {
92 jobFailed(cacheKey);
93 LOG.error("Unexpected runtime exception thrown during asynchronous revalidation", ex);
94 }
95
96 });
97 }
98
99 }