Search in sources :

Example 6 with Worker

use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.

the class FluxInterval method subscribe.

@Override
public void subscribe(CoreSubscriber<? super Long> actual) {
    Worker w = timedScheduler.createWorker();
    IntervalRunnable r = new IntervalRunnable(actual, w);
    actual.onSubscribe(r);
    try {
        w.schedulePeriodically(r, initialDelay, period, unit);
    } catch (RejectedExecutionException ree) {
        if (!r.cancelled) {
            actual.onError(Operators.onRejectedExecution(ree, r, null, null, actual.currentContext()));
        }
    }
}
Also used : Worker(reactor.core.scheduler.Scheduler.Worker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 7 with Worker

use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.

the class FluxSubscribeOn method subscribe.

@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T> actual) {
    Worker worker;
    try {
        worker = Objects.requireNonNull(scheduler.createWorker(), "The scheduler returned a null Function");
    } catch (Throwable e) {
        Operators.error(actual, Operators.onOperatorError(e, actual.currentContext()));
        return;
    }
    SubscribeOnSubscriber<T> parent = new SubscribeOnSubscriber<>(source, actual, worker, requestOnSeparateThread);
    actual.onSubscribe(parent);
    try {
        worker.schedule(parent);
    } catch (RejectedExecutionException ree) {
        if (parent.s != Operators.cancelledSubscription()) {
            actual.onError(Operators.onRejectedExecution(ree, parent, null, null, actual.currentContext()));
        }
    }
}
Also used : Worker(reactor.core.scheduler.Scheduler.Worker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 8 with Worker

use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.

the class ParallelRunOn method subscribe.

@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T>[] subscribers) {
    if (!validate(subscribers)) {
        return;
    }
    int n = subscribers.length;
    CoreSubscriber<T>[] parents = new CoreSubscriber[n];
    boolean conditional = subscribers[0] instanceof Fuseable.ConditionalSubscriber;
    for (int i = 0; i < n; i++) {
        Worker w = scheduler.createWorker();
        if (conditional) {
            parents[i] = new FluxPublishOn.PublishOnConditionalSubscriber<>((Fuseable.ConditionalSubscriber<T>) subscribers[i], scheduler, w, true, prefetch, prefetch, queueSupplier);
        } else {
            parents[i] = new FluxPublishOn.PublishOnSubscriber<>(subscribers[i], scheduler, w, true, prefetch, prefetch, queueSupplier);
        }
    }
    source.subscribe(parents);
}
Also used : CoreSubscriber(reactor.core.CoreSubscriber) Worker(reactor.core.scheduler.Scheduler.Worker)

Example 9 with Worker

use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.

the class TimedSchedulerTest method massCancel.

@Test
public void massCancel() throws InterruptedException {
    Scheduler timer = Schedulers.newSingle("test-timer");
    try {
        Worker w1 = timer.createWorker();
        AtomicInteger counter = new AtomicInteger();
        Runnable task = counter::getAndIncrement;
        int tasks = 10;
        Disposable[] c = new Disposable[tasks];
        for (int i = 0; i < tasks; i++) {
            c[i] = w1.schedulePeriodically(task, 500, 500, TimeUnit.MILLISECONDS);
        }
        w1.dispose();
        for (int i = 0; i < tasks; i++) {
            assertThat(c[i].isDisposed()).isTrue();
        }
        Assert.assertEquals(0, counter.get());
    } finally {
        timer.dispose();
    }
}
Also used : Disposable(reactor.core.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Worker(reactor.core.scheduler.Scheduler.Worker) Test(org.junit.Test)

Example 10 with Worker

use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.

the class DelegateServiceSchedulerTest method notScheduledRejects.

@Test
public void notScheduledRejects() {
    Scheduler s = Schedulers.fromExecutorService(Executors.newSingleThreadExecutor());
    assertThatExceptionOfType(RejectedExecutionException.class).isThrownBy(() -> s.schedule(() -> {
    }, 100, TimeUnit.MILLISECONDS)).describedAs("direct delayed scheduling").isSameAs(Exceptions.failWithRejectedNotTimeCapable());
    assertThatExceptionOfType(RejectedExecutionException.class).isThrownBy(() -> s.schedulePeriodically(() -> {
    }, 100, 100, TimeUnit.MILLISECONDS)).describedAs("direct periodic scheduling").isSameAs(Exceptions.failWithRejectedNotTimeCapable());
    Worker w = s.createWorker();
    assertThatExceptionOfType(RejectedExecutionException.class).isThrownBy(() -> w.schedule(() -> {
    }, 100, TimeUnit.MILLISECONDS)).describedAs("worker delayed scheduling").isSameAs(Exceptions.failWithRejectedNotTimeCapable());
    assertThatExceptionOfType(RejectedExecutionException.class).isThrownBy(() -> w.schedulePeriodically(() -> {
    }, 100, 100, TimeUnit.MILLISECONDS)).describedAs("worker periodic scheduling").isSameAs(Exceptions.failWithRejectedNotTimeCapable());
}
Also used : Worker(reactor.core.scheduler.Scheduler.Worker) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Test(org.junit.Test)

Aggregations

Worker (reactor.core.scheduler.Scheduler.Worker)12 Test (org.junit.Test)6 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CoreSubscriber (reactor.core.CoreSubscriber)1 Disposable (reactor.core.Disposable)1 Scannable (reactor.core.Scannable)1 Scheduler (reactor.core.scheduler.Scheduler)1