1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
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 * Class used for asynchronous revalidations to be used when
41 * the {@code stale-while-revalidate} directive is present
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 * Create DefaultCacheRevalidator which will make ache revalidation requests
54 * using the supplied {@link SchedulingStrategy} and {@link ScheduledExecutor}.
55 */
56 public DefaultCacheRevalidator(
57 final CacheRevalidatorBase.ScheduledExecutor scheduledExecutor,
58 final SchedulingStrategy schedulingStrategy) {
59 super(scheduledExecutor, schedulingStrategy);
60 }
61
62 /**
63 * Create CacheValidator which will make ache revalidation requests
64 * using the supplied {@link SchedulingStrategy} and {@link ScheduledExecutorService}.
65 */
66 public DefaultCacheRevalidator(
67 final ScheduledExecutorService scheduledThreadPoolExecutor,
68 final SchedulingStrategy schedulingStrategy) {
69 this(wrap(scheduledThreadPoolExecutor), schedulingStrategy);
70 }
71
72 /**
73 * Schedules an asynchronous re-validation
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 }