Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LoggingScheduledExecutorService.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util;
  18. import com.google.common.util.concurrent.ThreadFactoryBuilder;
  19. import java.util.concurrent.CancellationException;
  20. import java.util.concurrent.ExecutionException;
  21. import java.util.concurrent.Future;
  22. import java.util.concurrent.ScheduledThreadPoolExecutor;
  23. import java.util.function.BiConsumer;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;
  26. import static com.dmdirc.util.LogUtils.APP_ERROR;
  27. /**
  28. * An scheduled executor service that takes logs failed executions either via eventbus errors or a
  29. * custom error function.
  30. */
  31. public class LoggingScheduledExecutorService extends ScheduledThreadPoolExecutor {
  32. private static final Logger LOG = LoggerFactory.getLogger(LoggingScheduledExecutorService.class);
  33. private final BiConsumer<Runnable, Throwable> afterExecute;
  34. /**
  35. * Creates a new instance of this executor service.
  36. *
  37. * @param coreSize The number of threads to keep in the pool, even if they are idle, unless
  38. * {@code allowCoreThreadTimeOut} is set
  39. * @param poolName The naming format to use when naming threads
  40. */
  41. public LoggingScheduledExecutorService(final int coreSize, final String poolName) {
  42. this(coreSize, (r, t) -> LOG.error(APP_ERROR, t.getMessage(), t), poolName);
  43. }
  44. /**
  45. * Creates a new instance of this executor service.
  46. *
  47. * @param coreSize The number of threads to keep in the pool, even if they are idle,
  48. * unless {@code allowCoreThreadTimeOut} is set
  49. * @param afterExecute The function to call when an exception occurs
  50. * @param poolName The naming format to use when naming threads
  51. */
  52. public LoggingScheduledExecutorService(final int coreSize,
  53. final BiConsumer<Runnable, Throwable> afterExecute, final String poolName) {
  54. super(coreSize, new ThreadFactoryBuilder().setNameFormat(poolName + "-%d").build());
  55. this.afterExecute = afterExecute;
  56. }
  57. @Override
  58. protected void afterExecute(final Runnable r, final Throwable t) {
  59. super.afterExecute(r, t);
  60. if (t == null && r instanceof Future<?>) {
  61. try {
  62. if (((Future<?>) r).isDone()) {
  63. // If a periodic task completes successfully it is reset before this method
  64. // is called, and get() will block indefinitely. If it is not scheduled to run
  65. // again, or if it has thrown an exception and been stopped by the executor,
  66. // then isDone() will return true and we can safely call get().
  67. ((Future<?>) r).get();
  68. }
  69. } catch (CancellationException | InterruptedException ex) {
  70. //Ignore
  71. } catch (ExecutionException ex) {
  72. afterExecute.accept(r, ex);
  73. }
  74. }
  75. if (t != null) {
  76. afterExecute.accept(r, t);
  77. }
  78. }
  79. }