Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyServlet extends HttpServlet {
- public Cache cache;
- public void init() {
- cache = new Cache();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) {
- cache.putAsync(...);
- }
- public void destroy() {
- if (cache != null)
- cache.close();
- }
- }
- public class Cache {
- public ExecutorService executor = Executors.newFixedThreadPool(20);
- public CompletableFuture<Boolean> putAsync(String idInCache, Map<String, Object> parameters) {
- CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> put(idInCache, parameters), executor);
- return future;
- }
- public void close() {
- executor.shutdowns();
- try {
- executor.awaitTermination(3000, TimeUnit.MILLISECONDS);
- } catch (InterruptedException e) {
- loggerCache.log(Level.SEVERE, e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment