Guest User

Untitled

a guest
May 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. public class MyServlet extends HttpServlet {
  2.  
  3. public Cache cache;
  4.  
  5. public void init() {
  6. cache = new Cache();
  7. }
  8.  
  9. protected void doGet(HttpServletRequest request, HttpServletResponse response) {
  10.  
  11. cache.putAsync(...);
  12.  
  13. }
  14.  
  15. public void destroy() {
  16. if (cache != null)
  17. cache.close();
  18. }
  19. }
  20.  
  21. public class Cache {
  22. public ExecutorService executor = Executors.newFixedThreadPool(20);
  23.  
  24. public CompletableFuture<Boolean> putAsync(String idInCache, Map<String, Object> parameters) {
  25.  
  26. CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> put(idInCache, parameters), executor);
  27. return future;
  28. }
  29.  
  30. public void close() {
  31. executor.shutdowns();
  32. try {
  33. executor.awaitTermination(3000, TimeUnit.MILLISECONDS);
  34. } catch (InterruptedException e) {
  35. loggerCache.log(Level.SEVERE, e.getMessage());
  36. }
  37. }
  38. }
Add Comment
Please, Sign In to add comment