Search in sources :

Example 1 with CancelListener

use of org.neo4j.scheduler.CancelListener in project neo4j by neo4j.

the class PooledJobHandle method cancel.

@Override
public void cancel() {
    future.cancel(false);
    for (CancelListener cancelListener : cancelListeners) {
        cancelListener.cancelled();
    }
    registry.remove(registryKey);
}
Also used : CancelListener(org.neo4j.scheduler.CancelListener)

Example 2 with CancelListener

use of org.neo4j.scheduler.CancelListener in project neo4j by neo4j.

the class CentralJobSchedulerTest method scheduledTasksCanBeTheirOwnCancellationListeners.

@Test
void scheduledTasksCanBeTheirOwnCancellationListeners() {
    life.start();
    AtomicInteger cancelled = new AtomicInteger();
    class CancelCallback implements CancelListener {

        @Override
        public void cancelled() {
            cancelled.incrementAndGet();
        }
    }
    class RunnableAndCancellable extends CancelCallback implements Runnable {

        @Override
        public void run() {
        }
    }
    class CallableAndCancellable extends CancelCallback implements Callable<Void> {

        @Override
        public Void call() throws Exception {
            return null;
        }
    }
    scheduler.schedule(Group.CHECKPOINT, NOT_MONITORED, new RunnableAndCancellable()).cancel();
    assertThat(cancelled).hasValue(1);
    cancelled.set(0);
    scheduler.schedule(Group.CHECKPOINT, NOT_MONITORED, new RunnableAndCancellable(), 1, SECONDS).cancel();
    assertThat(cancelled).hasValue(1);
    cancelled.set(0);
    scheduler.scheduleRecurring(Group.CHECKPOINT, NOT_MONITORED, new RunnableAndCancellable(), 1, SECONDS).cancel();
    assertThat(cancelled).hasValue(1);
    cancelled.set(0);
    scheduler.scheduleRecurring(Group.CHECKPOINT, NOT_MONITORED, new RunnableAndCancellable(), 1, 1, SECONDS).cancel();
    assertThat(cancelled).hasValue(1);
    cancelled.set(0);
    scheduler.schedule(Group.CHECKPOINT, NOT_MONITORED, new CallableAndCancellable()).cancel();
    assertThat(cancelled).hasValue(1);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancelListener(org.neo4j.scheduler.CancelListener) Callable(java.util.concurrent.Callable) Test(org.junit.jupiter.api.Test)

Example 3 with CancelListener

use of org.neo4j.scheduler.CancelListener in project neo4j by neo4j.

the class ScheduledJobHandle method cancel.

@Override
public void cancel() {
    monitoredJobs.remove(this);
    state.set(FAILED);
    JobHandle handle = latestHandle;
    if (handle != null) {
        handle.cancel();
    }
    for (CancelListener cancelListener : cancelListeners) {
        cancelListener.cancelled();
    }
    scheduler.cancelTask(this);
    // Release the handle to allow waitTermination() to observe the cancellation.
    handleRelease.release();
}
Also used : JobHandle(org.neo4j.scheduler.JobHandle) CancelListener(org.neo4j.scheduler.CancelListener)

Aggregations

CancelListener (org.neo4j.scheduler.CancelListener)3 Callable (java.util.concurrent.Callable)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.jupiter.api.Test)1 JobHandle (org.neo4j.scheduler.JobHandle)1