View Javadoc
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.Future;
31  import java.util.concurrent.RejectedExecutionException;
32  import java.util.concurrent.ScheduledExecutorService;
33  import java.util.concurrent.atomic.AtomicReference;
34  
35  import org.apache.hc.client5.http.async.AsyncExecCallback;
36  import org.apache.hc.client5.http.impl.Operations;
37  import org.apache.hc.client5.http.schedule.SchedulingStrategy;
38  import org.apache.hc.core5.http.EntityDetails;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.HttpResponse;
41  import org.apache.hc.core5.http.HttpStatus;
42  import org.apache.hc.core5.http.nio.AsyncDataConsumer;
43  import org.apache.hc.core5.util.TimeValue;
44  import org.apache.hc.core5.util.Timeout;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  /**
49   * Class used for asynchronous revalidations to be used when the {@code stale-while-revalidate}
50   * directive is present
51   */
52  class DefaultAsyncCacheRevalidator extends CacheRevalidatorBase {
53  
54      private static final Logger LOG = LoggerFactory.getLogger(DefaultAsyncCacheRevalidator.class);
55  
56      interface RevalidationCall {
57  
58          void execute(AsyncExecCallback asyncExecCallback);
59      }
60  
61      static class InternalScheduledExecutor implements ScheduledExecutor {
62  
63          private final ScheduledExecutor executor;
64  
65          InternalScheduledExecutor(final ScheduledExecutor executor) {
66              this.executor = executor;
67          }
68  
69          @Override
70          public Future<?> schedule(final Runnable command, final TimeValue timeValue) throws RejectedExecutionException {
71              if (timeValue.toMilliseconds() <= 0) {
72                  command.run();
73                  return new Operations.CompletedFuture<Void>(null);
74              }
75              return executor.schedule(command, timeValue);
76          }
77  
78          @Override
79          public void shutdown() {
80              executor.shutdown();
81          }
82  
83          @Override
84          public void awaitTermination(final Timeout timeout) throws InterruptedException {
85              executor.awaitTermination(timeout);
86          }
87  
88      }
89  
90      /**
91       * Create DefaultCacheRevalidator which will make ache revalidation requests
92       * using the supplied {@link SchedulingStrategy} and {@link ScheduledExecutor}.
93       */
94      public DefaultAsyncCacheRevalidator(
95              final ScheduledExecutor scheduledExecutor,
96              final SchedulingStrategy schedulingStrategy) {
97          super(new InternalScheduledExecutor(scheduledExecutor), schedulingStrategy);
98      }
99  
100     /**
101      * Create CacheValidator which will make ache revalidation requests
102      * using the supplied {@link SchedulingStrategy} and {@link ScheduledExecutorService}.
103      */
104     public DefaultAsyncCacheRevalidator(
105             final ScheduledExecutorService executorService,
106             final SchedulingStrategy schedulingStrategy) {
107         this(wrap(executorService), schedulingStrategy);
108     }
109 
110     /**
111      * Schedules an asynchronous re-validation
112      */
113     public void revalidateCacheEntry(
114             final String cacheKey ,
115             final AsyncExecCallback asyncExecCallback,
116             final RevalidationCall call) {
117         scheduleRevalidation(cacheKey, () -> call.execute(new AsyncExecCallback() {
118 
119             private final AtomicReference<HttpResponse> responseRef = new AtomicReference<>();
120 
121             @Override
122             public AsyncDataConsumer handleResponse(
123                     final HttpResponse response,
124                     final EntityDetails entityDetails) throws HttpException, IOException {
125                 responseRef.set(response);
126                 return asyncExecCallback.handleResponse(response, entityDetails);
127             }
128 
129             @Override
130             public void handleInformationResponse(
131                     final HttpResponse response) throws HttpException, IOException {
132                 asyncExecCallback.handleInformationResponse(response);
133             }
134 
135             @Override
136             public void completed() {
137                 final HttpResponse httpResponse = responseRef.getAndSet(null);
138                 if (httpResponse != null && httpResponse.getCode() < HttpStatus.SC_SERVER_ERROR && !isStale(httpResponse)) {
139                     jobSuccessful(cacheKey);
140                 } else {
141                     jobFailed(cacheKey);
142                 }
143                 asyncExecCallback.completed();
144             }
145 
146             @Override
147             public void failed(final Exception cause) {
148                 if (cause instanceof IOException) {
149                     LOG.debug("Asynchronous revalidation failed due to I/O error", cause);
150                 } else if (cause instanceof HttpException) {
151                     LOG.error("HTTP protocol exception during asynchronous revalidation", cause);
152                 } else {
153                     LOG.error("Unexpected runtime exception thrown during asynchronous revalidation", cause);
154                 }
155                 try {
156                     jobFailed(cacheKey);
157                 } finally {
158                     asyncExecCallback.failed(cause);
159                 }
160             }
161 
162         }));
163     }
164 
165 }