|
| 1 | +package com.orientechnologies.common.thread; |
| 2 | + |
| 3 | +import java.util.Objects; |
| 4 | +import java.util.concurrent.*; |
| 5 | + |
| 6 | +public class OSourceTraceScheduledExecutorService extends OSourceTraceExecutorService |
| 7 | + implements ScheduledExecutorService { |
| 8 | + |
| 9 | + private final ScheduledExecutorService scheduledService; |
| 10 | + |
| 11 | + public OSourceTraceScheduledExecutorService(ScheduledExecutorService service) { |
| 12 | + super(service); |
| 13 | + Objects.requireNonNull(service); |
| 14 | + this.scheduledService = service; |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { |
| 19 | + final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(command); |
| 20 | + return scheduledService.schedule( |
| 21 | + () -> { |
| 22 | + try { |
| 23 | + command.run(); |
| 24 | + } catch (RuntimeException e) { |
| 25 | + throw OTracedExecutionException.trace(trace, e, command); |
| 26 | + } |
| 27 | + }, |
| 28 | + delay, |
| 29 | + unit); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
| 34 | + final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(callable); |
| 35 | + return scheduledService.schedule( |
| 36 | + () -> { |
| 37 | + try { |
| 38 | + return callable.call(); |
| 39 | + } catch (RuntimeException e) { |
| 40 | + throw OTracedExecutionException.trace(trace, e, callable); |
| 41 | + } |
| 42 | + }, |
| 43 | + delay, |
| 44 | + unit); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public ScheduledFuture<?> scheduleAtFixedRate( |
| 49 | + Runnable command, long initialDelay, long period, TimeUnit unit) { |
| 50 | + final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(command); |
| 51 | + return scheduledService.scheduleAtFixedRate( |
| 52 | + () -> { |
| 53 | + try { |
| 54 | + command.run(); |
| 55 | + } catch (RuntimeException e) { |
| 56 | + throw OTracedExecutionException.trace(trace, e, command); |
| 57 | + } |
| 58 | + }, |
| 59 | + initialDelay, |
| 60 | + period, |
| 61 | + unit); |
| 62 | + } |
| 63 | + |
| 64 | + public ScheduledFuture<?> scheduleWithFixedDelay( |
| 65 | + Runnable command, long initialDelay, long delay, TimeUnit unit) { |
| 66 | + final OTracedExecutionException trace = OTracedExecutionException.prepareTrace(command); |
| 67 | + return scheduledService.scheduleWithFixedDelay( |
| 68 | + () -> { |
| 69 | + try { |
| 70 | + command.run(); |
| 71 | + } catch (RuntimeException e) { |
| 72 | + throw OTracedExecutionException.trace(trace, e, command); |
| 73 | + } |
| 74 | + }, |
| 75 | + initialDelay, |
| 76 | + delay, |
| 77 | + unit); |
| 78 | + } |
| 79 | +} |
0 commit comments