@Override public T apply(CancellableTask<K, T> cancellableTask) { return cancellableTask.getScheduledTask(); } });
/** * Gets an instance of the {@link TaskScheduler} with the specified type and ensures that it is started. If * the type is unknown an instance of the default {@link TaskScheduler} will be returned. * * @param type the type of the {@link TaskScheduler} * @param name the name associated threads created by the {@link TaskScheduler} * @param <K> the type of the key for the {@link ScheduledTask} * @param <T> the type of the {@link ScheduledTask} * @return an instance of {@link TaskScheduler} */ public static <K, T extends ScheduledTask<K>> TaskScheduler<K, T> get(String type, Optional<String> name) { TaskSchedulerType taskSchedulerType = TaskSchedulerType.parse(type); return get(taskSchedulerType, name); }
/** * Schedules a subclass of {@link ScheduledTask} to run periodically. * * @param task the subclass of {@link ScheduledTask} to run every period * @param period the period between successive executions of the task * @param unit the time unit of the period parameter */ @Override final CancellableTask<K, T> scheduleImpl(T task, long period, TimeUnit unit) { return new HashedWheelTimerTask<>(executor, task, period, unit); }
@Test public void testCloseCancelsTasks() throws IOException, InterruptedException { Task task = new Task("foo"); try (TaskScheduler<String, Task> taskScheduler = getTaskScheduler("TaskSchedulerTest")) { taskScheduler.schedule(task, 1, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); } task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); Assert.assertEquals(task.getCount(), 1); }
/** * Attempts to cancel the specified {@link ScheduledTask}. * * @param task the {@link ScheduledTask} to cancel * @return true if the {@link ScheduledTask} was canceled; otherwise, false */ public boolean cancel(T task) { CancellableTask<K, T> cancellableTask = this.cancellableTaskMap.getIfPresent(task.getKey()); if (cancellableTask != null && cancellableTask.getScheduledTask() == task && cancellableTask.cancel()) { this.cancellableTaskMap.invalidate(task.getKey()); return true; } return false; }
/** * Closes this {@link TaskScheduler}, ensuring that new tasks cannot be created * and cancelling existing tasks. * * @throws IOException if an I/O error occurs */ @Override final void closeImpl() throws IOException { for (T scheduledTask : this.getScheduledTasks()) { this.cancel(scheduledTask); } }
/** * Schedules a subclass of {@link ScheduledTask} to run periodically. * * @param task the subclass of {@link ScheduledTask} to run every period * @param period the period between successive executions of the task * @param unit the time unit of the period parameter */ @Override final CancellableTask<K, T> scheduleImpl(final T task, long period, TimeUnit unit) { final ScheduledFuture<?> future = this.executorService.scheduleAtFixedRate(new RunnableTask(task), 0, period, unit); return new CancellableScheduledFuture<>(task, future); }
@Override public void runOneIteration() { count++; this.autoResetEvent.set(); } }
public SynchronousFork(TaskContext taskContext, Object schema, int branches, int index, ExecutionModel executionModel) throws Exception { super(taskContext, schema, branches, index, executionModel); this.autoResetEvent = new AutoResetEvent(); }
@Override protected TaskScheduler<String, Task> getTaskScheduler(String name) { return TaskSchedulerFactory.get("HashedWheelTimer", Optional.of(name)); } }
@Override protected void processRecords() throws IOException, DataConversionException { try { this.autoResetEvent.waitOne(); if (this.throwable != null) { Throwables.propagateIfPossible(this.throwable, IOException.class, DataConversionException.class); throw new RuntimeException(throwable); } } catch (InterruptedException ie) { Throwables.propagate(ie); } }
@Override public CancellableTask<K, T> call() { return scheduleImpl(task, period, unit); } });
@Override protected void shutDown() throws Exception { this.scheduler.close(); }
/** * Start the {@link TaskScheduler}. * * @param name the name of the {@link TaskScheduler} */ @Synchronized final void start(Optional<String> name) { if (!this.isStarted) { this.startImpl(name); this.isStarted = true; } }
/** * Closes this {@link TaskScheduler}, ensuring that new tasks cannot be created * and cancelling existing tasks. * * @throws IOException if an I/O error occurs */ @Override @Synchronized public final void close() throws IOException { if (this.isStarted) { this.isStarted = false; this.closeImpl(); } }
@Test public void testTaskRunsOnSchedule() throws IOException, InterruptedException { long startTime; long endTime; Task task = new Task("foo"); try (TaskScheduler<String, Task> taskScheduler = getTaskScheduler("TaskSchedulerTest")) { startTime = System.currentTimeMillis(); taskScheduler.schedule(task, 1, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); task.getAutoResetEvent().waitOne(2, TimeUnit.SECONDS); endTime = System.currentTimeMillis(); } Assert.assertEquals(task.getCount(), 3); Assert.assertTrue(endTime - startTime >= (scheduleFiresImmediately ? 2000 : 3000)); }
@Override protected boolean putRecordImpl(Object record) throws InterruptedException { try { this.processRecord(record); } catch (Throwable t) { this.throwable = t; this.autoResetEvent.set(); } return true; }
public Task(String key) { this.key = key; this.autoResetEvent = new AutoResetEvent(false);; }
@Override protected TaskScheduler<String, Task> getTaskScheduler(String name) { return TaskSchedulerFactory.get("ScheduledExecutorService", Optional.of(name)); } }
@Override public void markParentTaskDone() { super.markParentTaskDone(); this.autoResetEvent.set(); }