Java Code Examples for reactor.core.scheduler.Scheduler#Worker

The following examples show how to use reactor.core.scheduler.Scheduler#Worker . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FluxIntervalTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
  public void scanIntervalRunnable() {
Scheduler.Worker worker = Schedulers.single().createWorker();

try {
      CoreSubscriber<Long> actual = new LambdaSubscriber<>(null, e -> {}, null, null);
      FluxInterval.IntervalRunnable test = new FluxInterval.IntervalRunnable(actual, worker);

      assertThat(test.scan(Scannable.Attr.RUN_ON)).isSameAs(worker);
      assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);
      assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
      test.cancel();
      assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
}
finally {
	worker.dispose();
}
  }
 
Example 2
Source File: MonoSubscribeOn.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(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()));
		}
	}
	return null;
}
 
Example 3
Source File: StepVerifierTests.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void externalGetOrSetTakenIntoAccount() {
	Scheduler.Worker subscriptionWorker = VirtualTimeScheduler.getOrSet().createWorker();
	List<String> source = Stream.of("first", "second", "third").collect(Collectors.toList());


	StepVerifier.withVirtualTime(() -> {
		FluxIdentityProcessor<String> fluxEmitter = Processors.multicast();

		subscriptionWorker.schedulePeriodically(() -> {
			if (source.size() > 0) {
				fluxEmitter.onNext(source.remove(0));
			}
			else {
				fluxEmitter.onComplete();
			}
		}, 0, 10, TimeUnit.MILLISECONDS);
		return fluxEmitter;
	})
	            .expectNext("first")
	            .expectNoEvent(Duration.ofMillis(10))
	            .expectNext("second")
	            .expectNoEvent(Duration.ofMillis(10))
	            .expectNext("third")
	            .expectNoEvent(Duration.ofMillis(10))
	            .expectComplete()
	            .verify(Duration.ofSeconds(2));
}
 
Example 4
Source File: MonoSubscribeOnTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void scanSubscribeOnSubscriber() {
	Scheduler.Worker worker = Schedulers.single().createWorker();

	try {
		final Flux<String> source = Flux.just("foo");
		CoreSubscriber<String>
				actual = new LambdaMonoSubscriber<>(null, e -> {}, null, null);
		MonoSubscribeOn.SubscribeOnSubscriber<String> test = new MonoSubscribeOn.SubscribeOnSubscriber<>(
				source, actual, worker);
		Subscription parent = Operators.emptySubscription();
		test.onSubscribe(parent);

		test.requested = 3L;
		assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(3L);

		assertThat(test.scan(Scannable.Attr.RUN_ON)).isSameAs(worker);
		assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
		assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);

		assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
		test.cancel();
		assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
	}
	finally {
		worker.dispose();
	}
}
 
Example 5
Source File: FluxBufferTimeoutTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void scanSubscriber() {
	CoreSubscriber<List<String>> actual = new LambdaSubscriber<>(null, e -> {}, null, null);

	final Scheduler.Worker worker = Schedulers.boundedElastic()
	                                          .createWorker();
	FluxBufferTimeout.BufferTimeoutSubscriber<String, List<String>> test = new FluxBufferTimeout.BufferTimeoutSubscriber<String, List<String>>(
					actual, 123, 1000, TimeUnit.MILLISECONDS,
			worker, ArrayList::new);

	try {
		Subscription subscription = Operators.emptySubscription();
		test.onSubscribe(subscription);

		test.requested = 3L;
		test.index = 100;

		assertThat(test.scan(Scannable.Attr.RUN_ON)).isSameAs(worker);
		assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(subscription);
		assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);

		assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(3L);
		assertThat(test.scan(Scannable.Attr.CAPACITY)).isEqualTo(123);
		assertThat(test.scan(Scannable.Attr.BUFFERED)).isEqualTo(23);

		assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
		assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();

		test.onError(new IllegalStateException("boom"));
		assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
		assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
	}
	finally {
		worker.dispose();
	}
}
 
Example 6
Source File: FluxDelaySequenceTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void scanSubscriber() {
	Scheduler.Worker worker = Schedulers.immediate().createWorker();

	CoreSubscriber<String> actual = new LambdaSubscriber<>(null, null, null, null);
	Subscription s = Operators.emptySubscription();

	FluxDelaySequence.DelaySubscriber test = new DelaySubscriber<>(actual, Duration.ofSeconds(1), worker);

	test.onSubscribe(s);

	assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(s);
	SerializedSubscriber serializedSubscriber = (SerializedSubscriber) test.scan(Scannable.Attr.ACTUAL);
	assertThat(serializedSubscriber)
			.isNotNull()
			.satisfies(ser -> assertThat(ser.actual).isSameAs(actual));

	assertThat(test.scan(Scannable.Attr.RUN_ON)).isEqualTo(worker);

	assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
	assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();

	test.cancel();
	assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
	assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();

	test.done = true;
	assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
}
 
Example 7
Source File: FluxBufferTimeout.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
BufferTimeoutSubscriber(CoreSubscriber<? super C> actual,
		int maxSize,
		long timespan,
		TimeUnit unit,
		Scheduler.Worker timer,
		Supplier<C> bufferSupplier) {
	this.actual = actual;
	this.timespan = timespan;
	this.unit = unit;
	this.timer = timer;
	this.flushTask = () -> {
		if (terminated == NOT_TERMINATED) {
			int index;
			for(;;){
				index = this.index;
				if(index == 0){
					return;
				}
				if(INDEX.compareAndSet(this, index, 0)){
					break;
				}
			}
			flushCallback(null);
		}
	};

	this.batchSize = maxSize;
	this.bufferSupplier = bufferSupplier;
}
 
Example 8
Source File: FluxDelaySequence.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
DelaySubscriber(CoreSubscriber<? super T> actual, Duration delay, Scheduler.Worker w) {
	super();
	this.actual = new SerializedSubscriber<>(actual);
	this.w = w;
	this.delay = delay.toNanos();
	this.timeUnit = TimeUnit.NANOSECONDS;
}
 
Example 9
Source File: TimerWithWorker.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private static <T> void timer(Supplier<T> action, Scheduler.Worker worker, Duration delay, MonoSink<T> sink) {
    worker.schedule(() -> {
        try {
            T result = action.get();
            if (result != null) {
                sink.success(result);
            } else {
                sink.success();
            }
        } catch (Exception e) {
            sink.error(e);
        }
    }, delay.toMillis(), TimeUnit.MILLISECONDS);
}
 
Example 10
Source File: TimerWithWorker.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
static <T> Mono<T> timer(Supplier<T> action, Scheduler.Worker worker, Duration delay) {
    if (worker.isDisposed()) {
        return Mono.empty();
    }

    return Mono.create(sink -> {
        if (worker.isDisposed()) {
            sink.success();
        } else {
            timer(action, worker, delay, sink);
        }
    });
}
 
Example 11
Source File: ReactorExt.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
/**
 * Runs an action on the provided worker.
 */
public static <T> Mono<T> onWorker(Supplier<T> action, Scheduler.Worker worker) {
    return TimerWithWorker.timer(action, worker, Duration.ZERO);
}
 
Example 12
Source File: FluxDelaySequence.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super T> actual) {
	Scheduler.Worker w = scheduler.createWorker();

	return new DelaySubscriber<T>(actual, delay, w);
}
 
Example 13
Source File: UiUtils.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public Scheduler.Worker createWorker() {
  return this.worker;
}
 
Example 14
Source File: TimerWithWorker.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
static Mono<Void> timer(Runnable action, Scheduler.Worker worker, Duration delay) {
    return timer(() -> {
        action.run();
        return null;
    }, worker, delay);
}
 
Example 15
Source File: ReactorExt.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
/**
 * Runs an action on the provided worker with the provided delay.
 */
public static Mono<Void> onWorker(Runnable action, Scheduler.Worker worker, Duration delay) {
    return TimerWithWorker.timer(action, worker, delay);
}
 
Example 16
Source File: ReactorExt.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
/**
 * Runs an action on the provided worker.
 */
public static Mono<Void> onWorker(Runnable action, Scheduler.Worker worker) {
    return TimerWithWorker.timer(action, worker, Duration.ZERO);
}
 
Example 17
Source File: ReactorExt.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
/**
 * Runs an action on the provided worker with the provided delay.
 */
public static <T> Mono<T> onWorker(Supplier<T> action, Scheduler.Worker worker, Duration delay) {
    return TimerWithWorker.timer(action, worker, delay);
}