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      private final CacheKeyGenerator cacheKeyGenerator;
91  
92      /**
93       * Create DefaultCacheRevalidator which will make ache revalidation requests
94       * using the supplied {@link SchedulingStrategy} and {@link ScheduledExecutor}.
95       */
96      public DefaultAsyncCacheRevalidator(
97              final ScheduledExecutor scheduledExecutor,
98              final SchedulingStrategy schedulingStrategy) {
99          super(new InternalScheduledExecutor(scheduledExecutor), schedulingStrategy);
100         this.cacheKeyGenerator = CacheKeyGenerator.INSTANCE;
101 
102     }
103 
104     /**
105      * Create CacheValidator which will make ache revalidation requests
106      * using the supplied {@link SchedulingStrategy} and {@link ScheduledExecutorService}.
107      */
108     public DefaultAsyncCacheRevalidator(
109             final ScheduledExecutorService executorService,
110             final SchedulingStrategy schedulingStrategy) {
111         this(wrap(executorService), schedulingStrategy);
112     }
113 
114     /**
115      * Schedules an asynchronous re-validation
116      */
117     public void revalidateCacheEntry(
118             final String cacheKey ,
119             final AsyncExecCallback asyncExecCallback,
120             final RevalidationCall call) {
121         scheduleRevalidation(cacheKey, () -> call.execute(new AsyncExecCallback() {
122 
123             private final AtomicReference<HttpResponse> responseRef = new AtomicReference<>();
124 
125             @Override
126             public AsyncDataConsumer handleResponse(
127                     final HttpResponse response,
128                     final EntityDetails entityDetails) throws HttpException, IOException {
129                 responseRef.set(response);
130                 return asyncExecCallback.handleResponse(response, entityDetails);
131             }
132 
133             @Override
134             public void handleInformationResponse(
135                     final HttpResponse response) throws HttpException, IOException {
136                 asyncExecCallback.handleInformationResponse(response);
137             }
138 
139             @Override
140             public void completed() {
141                 final HttpResponse httpResponse = responseRef.getAndSet(null);
142                 if (httpResponse != null && httpResponse.getCode() < HttpStatus.SC_SERVER_ERROR && !isStale(httpResponse)) {
143                     jobSuccessful(cacheKey);
144                 } else {
145                     jobFailed(cacheKey);
146                 }
147                 asyncExecCallback.completed();
148             }
149 
150             @Override
151             public void failed(final Exception cause) {
152                 if (cause instanceof IOException) {
153                     LOG.debug("Asynchronous revalidation failed due to I/O error", cause);
154                 } else if (cause instanceof HttpException) {
155                     LOG.error("HTTP protocol exception during asynchronous revalidation", cause);
156                 } else {
157                     LOG.error("Unexpected runtime exception thrown during asynchronous revalidation", cause);
158                 }
159                 try {
160                     jobFailed(cacheKey);
161                 } finally {
162                     asyncExecCallback.failed(cause);
163                 }
164             }
165 
166         }));
167     }
168 
169 }