use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.
the class MonoSubscribeOn method subscribe.
@Override
public void subscribe(CoreSubscriber<? super T> actual) {
Scheduler.Worker worker = scheduler.createWorker();
SubscribeOnSubscriber<T> parent = new SubscribeOnSubscriber<>(source, actual, worker);
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()));
}
}
}
use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.
the class FluxPublishOn 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 worker");
} catch (Throwable e) {
Operators.error(actual, Operators.onOperatorError(e, actual.currentContext()));
return;
}
if (actual instanceof ConditionalSubscriber) {
ConditionalSubscriber<? super T> cs = (ConditionalSubscriber<? super T>) actual;
source.subscribe(new PublishOnConditionalSubscriber<>(cs, scheduler, worker, delayError, prefetch, lowTide, queueSupplier));
return;
}
source.subscribe(new PublishOnSubscriber<>(actual, scheduler, worker, delayError, prefetch, lowTide, queueSupplier));
}
use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.
the class SingleSchedulerTest method lotsOfTasks.
@Test
public void lotsOfTasks() throws Exception {
System.gc();
Thread.sleep(200);
long before = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
Scheduler s = Schedulers.newSingle("scheduler");
try {
Worker w = s.createWorker();
try {
CountDownLatch cdl = new CountDownLatch(1_000_000);
Runnable r = cdl::countDown;
for (int i = 0; i < 1_000_000; i++) {
w.schedule(r);
}
Assert.assertTrue(cdl.await(5, TimeUnit.SECONDS));
System.gc();
Thread.sleep(200);
long after = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
Assert.assertTrue(String.format("%,d -> %,d", before, after), before + 20_000_000 > after);
} finally {
w.dispose();
}
} finally {
s.dispose();
}
}
use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.
the class TimedSchedulerTest method independentWorkers.
@Test
public void independentWorkers() throws InterruptedException {
Scheduler timer = Schedulers.newSingle("test-timer");
try {
Worker w1 = timer.createWorker();
Worker w2 = timer.createWorker();
CountDownLatch cdl = new CountDownLatch(1);
w1.dispose();
try {
w1.schedule(() -> {
});
Assert.fail("Failed to reject task");
} catch (Throwable ex) {
// ingoring
}
w2.schedule(cdl::countDown);
if (!cdl.await(1, TimeUnit.SECONDS)) {
Assert.fail("Worker 2 didn't execute in time");
}
w2.dispose();
} finally {
timer.dispose();
}
}
use of reactor.core.scheduler.Scheduler.Worker in project reactor-core by reactor.
the class DelegateServiceSchedulerTest method scheduledDoesntReject.
@Test
public void scheduledDoesntReject() {
Scheduler s = Schedulers.fromExecutorService(Executors.newSingleThreadScheduledExecutor());
assertThat(s.schedule(() -> {
}, 100, TimeUnit.MILLISECONDS)).describedAs("direct delayed scheduling").isNotNull();
assertThat(s.schedulePeriodically(() -> {
}, 100, 100, TimeUnit.MILLISECONDS)).describedAs("direct periodic scheduling").isNotNull();
Worker w = s.createWorker();
assertThat(w.schedule(() -> {
}, 100, TimeUnit.MILLISECONDS)).describedAs("worker delayed scheduling").isNotNull();
assertThat(w.schedulePeriodically(() -> {
}, 100, 100, TimeUnit.MILLISECONDS)).describedAs("worker periodic scheduling").isNotNull();
}
Aggregations