本文整理汇总了Java中reactor.core.scheduler.Scheduler类的典型用法代码示例。如果您正苦于以下问题:Java Scheduler类的具体用法?Java Scheduler怎么用?Java Scheduler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Scheduler类属于reactor.core.scheduler包,在下文中一共展示了Scheduler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readEmployeesByDescAge
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Override
public Flux<Employee> readEmployeesByDescAge() {
Scheduler subWorker = Schedulers.newSingle("sub-thread");
Scheduler pubWorker = Schedulers.newSingle("pub-thread");
Supplier<Flux<Employee>> deferredTask = ()->{
System.out.println("flux:defer task executor: "+ Thread.currentThread().getName());
System.out.println("flux:defer task executor login: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
return Flux.fromIterable(employeeDaoImpl.getEmployees());
};
Comparator<Employee> descAge = (e1, e2) -> {
System.out.println("flux:sort task executor: " + Thread.currentThread().getName());
System.out.println("flux:sort task executor login: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
if(e1.getAge().compareTo(e2.getAge()) == 0){
return 0;
} else if(e1.getAge().compareTo(e2.getAge()) > 0){
return -1;
} else return 1;
};
Flux<Employee> deferred = Flux.defer(deferredTask).sort(descAge).subscribeOn(subWorker).publishOn(pubWorker);
return deferred;
}
示例2: sampleZipTest3
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test
public void sampleZipTest3() throws Exception {
int elements = 1;
CountDownLatch latch = new CountDownLatch(elements + 1);
EmitterProcessor<SensorData> sensorDataProcessor = EmitterProcessor.create();
Scheduler scheduler = Schedulers.single();
sensorDataProcessor.publishOn(scheduler)
.subscribe(d -> latch.countDown(), null, latch::countDown);
Flux.zip(Flux.just(new SensorData(2L, 12.0f)), Flux.just(new SensorData(1L, 14.0f)), this::computeMin)
.log("zip3")
.subscribe(sensorDataProcessor);
awaitLatch(null, latch);
scheduler.dispose();
}
示例3: readEmployeesFlux
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Override
public Flux<Employee> readEmployeesFlux(int age) {
Scheduler subWorker = Schedulers.newSingle("sub-thread");
Scheduler pubWorker = Schedulers.newSingle("pub-thread");
Predicate<Employee> validAge = (e) -> {
System.out.println("flux:filter task executor: " + Thread.currentThread().getName());
System.out.println("flux:filter task executor login: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
return e.getAge() > age;
};
Supplier<Flux<Employee>> deferredTask = ()->{
System.out.println("flux:defer task executor: " + Thread.currentThread().getName());
System.out.println("flux:defer task executor login: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
return Flux.fromIterable(employeeDaoImpl.getEmployees());
};
Flux<Employee> deferred = Flux.defer(deferredTask).filter(validAge).subscribeOn(subWorker).publishOn(pubWorker);
return deferred;
}
示例4: readEmployeesByAscLastName
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Override
public Flux<Employee> readEmployeesByAscLastName() {
Scheduler subWorker = Schedulers.newSingle("sub-thread");
Scheduler pubWorker = Schedulers.newSingle("pub-thread");
Supplier<Flux<Employee>> deferredTask = ()->{
System.out.println("flux:defer task executor: " + Thread.currentThread().getName());
System.out.println("flux:defer task executor login: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
return Flux.fromIterable(employeeDaoImpl.getEmployees());
};
Comparator<Employee> descLName = (e1, e2) -> {
System.out.println("flux:sort task executor: " + Thread.currentThread().getName());
System.out.println("flux:sort task executor login: " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
return e1.getLastName().compareTo(e2.getLastName());
};
Flux<Employee> deferred = Flux.defer(deferredTask).sort(descLName).subscribeOn(subWorker).publishOn(pubWorker);
return deferred;
}
示例5: assertNextWithSubscribeOnDirectProcessor
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test
public void assertNextWithSubscribeOnDirectProcessor() {
Scheduler scheduler = Schedulers.newElastic("test");
DirectProcessor<Integer> processor = DirectProcessor.create();
Mono<Integer> doAction = Mono.fromSupplier(() -> 22)
.doOnNext(processor::onNext)
.subscribeOn(scheduler);
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(
StepVerifier.create(processor)
.then(doAction::subscribe)
.assertNext(v -> assertThat(v).isEqualTo(23))
.thenCancel()
::verify);
}
示例6: FluxPublishOn
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
FluxPublishOn(Flux<? extends T> source,
Scheduler scheduler,
boolean delayError,
int prefetch,
int lowTide,
Supplier<? extends Queue<T>> queueSupplier) {
super(source);
if (prefetch <= 0) {
throw new IllegalArgumentException("prefetch > 0 required but it was " + prefetch);
}
this.scheduler = Objects.requireNonNull(scheduler, "scheduler");
this.delayError = delayError;
this.prefetch = prefetch;
this.lowTide = lowTide;
this.queueSupplier = Objects.requireNonNull(queueSupplier, "queueSupplier");
}
示例7: subscribeOn
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
/**
* Run subscribe, onSubscribe and request on a specified {@link Scheduler}'s {@link Worker}.
* As such, placing this operator anywhere in the chain will also impact the execution
* context of onNext/onError/onComplete signals from the beginning of the chain up to
* the next occurrence of a {@link #publishOn(Scheduler) publishOn}.
* <p>
* <img class="marble" src="https://raw.githubusercontent.com/reactor/reactor-core/v3.1.3.RELEASE/src/docs/marble/subscribeon1.png" alt="">
*
* <blockquote><pre>
* {@code mono.subscribeOn(Schedulers.parallel()).subscribe()) }
* </pre></blockquote>
*
* @param scheduler a {@link Scheduler} providing the {@link Worker} where to subscribe
*
* @return a {@link Flux} requesting asynchronously
* @see #publishOn(Scheduler)
*/
public final Mono<T> subscribeOn(Scheduler scheduler) {
if(this instanceof Callable) {
if (this instanceof Fuseable.ScalarCallable) {
try {
T value = block();
return onAssembly(new MonoSubscribeOnValue<>(value, scheduler));
}
catch (Throwable t) {
//leave MonoSubscribeOnCallable defer error
}
}
@SuppressWarnings("unchecked")
Callable<T> c = (Callable<T>)this;
return onAssembly(new MonoSubscribeOnCallable<>(c,
scheduler));
}
return onAssembly(new MonoSubscribeOn<>(this, scheduler));
}
示例8: testBufferSize1Created
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test(timeout = 5_000)
public void testBufferSize1Created() throws Exception {
TopicProcessor<String> broadcast = TopicProcessor.<String>builder().name("share-name").bufferSize(1).autoCancel(true).build();
int simultaneousSubscribers = 3000;
CountDownLatch latch = new CountDownLatch(simultaneousSubscribers);
Scheduler scheduler = Schedulers.single();
FluxSink<String> sink = broadcast.sink();
Flux<String> flux = broadcast.filter(Objects::nonNull)
.publishOn(scheduler)
.cache(1);
for (int i = 0; i < simultaneousSubscribers; i++) {
flux.subscribe(s -> latch.countDown());
}
sink.next("data");
assertThat(latch.await(4, TimeUnit.SECONDS))
.overridingErrorMessage("Data not received")
.isTrue();
}
示例9: FluxBufferTimeout
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
FluxBufferTimeout(Flux<T> source,
int maxSize,
long timespan,
Scheduler timer,
Supplier<C> bufferSupplier) {
super(source);
if (timespan <= 0) {
throw new IllegalArgumentException("Timeout period must be strictly positive");
}
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize must be strictly positive");
}
this.timer = Objects.requireNonNull(timer, "Timer");
this.timespan = timespan;
this.batchSize = maxSize;
this.bufferSupplier = Objects.requireNonNull(bufferSupplier, "bufferSupplier");
}
示例10: publishOn
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
final Flux<T> publishOn(Scheduler scheduler, boolean delayError, int prefetch, int lowTide) {
if (this instanceof Callable) {
if (this instanceof Fuseable.ScalarCallable) {
@SuppressWarnings("unchecked")
Fuseable.ScalarCallable<T> s = (Fuseable.ScalarCallable<T>) this;
try {
return onAssembly(new FluxSubscribeOnValue<>(s.call(), scheduler));
}
catch (Exception e) {
//leave FluxSubscribeOnCallable defer exception call
}
}
@SuppressWarnings("unchecked")
Callable<T> c = (Callable<T>)this;
return onAssembly(new FluxSubscribeOnCallable<>(c, scheduler));
}
return onAssembly(new FluxPublishOn<>(this, scheduler, delayError, prefetch, lowTide, Queues.get(prefetch)));
}
示例11: testSubmitSession
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test
public void testSubmitSession() throws Exception {
FluxProcessor<Integer, Integer> processor = EmitterProcessor.create();
AtomicInteger count = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(1);
Scheduler scheduler = Schedulers.parallel();
processor.publishOn(scheduler)
.delaySubscription(Duration.ofMillis(1000))
.limitRate(1)
.subscribe(d -> {
count.incrementAndGet();
latch.countDown();
});
FluxSink<Integer> session = processor.sink();
session.next(1);
//System.out.println(emission);
session.complete();
latch.await(5, TimeUnit.SECONDS);
Assert.assertTrue("latch : " + count, count.get() == 1);
scheduler.dispose();
}
示例12: testParallelWithJava8StreamsInput
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
/**
* https://gist.github.com/nithril/444d8373ce67f0a8b853 Contribution by Nicolas Labrot
* @throws InterruptedException on interrupt
*/
@Test
public void testParallelWithJava8StreamsInput() throws InterruptedException {
Scheduler supplier = Schedulers.newParallel("test-p", 2);
int max = ThreadLocalRandom.current()
.nextInt(100, 300);
CountDownLatch countDownLatch = new CountDownLatch(max);
Flux<Integer> worker = Flux.range(0, max)
.publishOn(asyncGroup);
worker.parallel(2)
.runOn(supplier)
.map(v -> v)
.subscribe(v -> countDownLatch.countDown());
countDownLatch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(0, countDownLatch.getCount());
}
示例13: consistentMultithreadingWithPartition
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test
public void consistentMultithreadingWithPartition() throws InterruptedException {
Scheduler supplier1 = Schedulers.newParallel("groupByPool", 2);
Scheduler supplier2 = Schedulers.newParallel("partitionPool", 5);
CountDownLatch latch = new CountDownLatch(10);
/*Disposable c = */Flux.range(1, 10)
.groupBy(n -> n % 2 == 0)
.flatMap(stream -> stream.publishOn(supplier1)
.log("groupBy-" + stream.key()))
.parallel(5)
.runOn(supplier2)
.sequential()
.publishOn(asyncGroup)
.log("join")
.subscribe(t -> {
latch.countDown();
});
latch.await(30, TimeUnit.SECONDS);
assertThat("Not totally dispatched: " + latch.getCount(), latch.getCount() == 0);
supplier1.dispose();
supplier2.dispose();
}
示例14: crossRangePerfDefault
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test
public void crossRangePerfDefault() {
AssertSubscriber<Integer> ts = AssertSubscriber.create();
Scheduler scheduler = Schedulers.fromExecutorService(exec);
int count = 1000;
Flux<Integer> source = Flux.range(1, count)
.flatMap(v -> Flux.range(v, 2), false, 128, 32);
source.publishOn(scheduler)
.subscribe(ts);
if (!ts.await(Duration.ofSeconds(10))
.isTerminated()) {
ts.cancel();
}
ts.assertValueCount(count * 2)
.assertNoError()
.assertComplete();
}
示例15: onNextOnDisposedSchedulerThrows
import reactor.core.scheduler.Scheduler; //导入依赖的package包/类
@Test
public void onNextOnDisposedSchedulerThrows() {
Scheduler scheduler = Schedulers.newSingle("onNextOnDisposedSchedulerThrows");
scheduler.dispose();
Mono<String> source = Mono.just("foo").hide();
try {
StepVerifier.create(new MonoDelayElement<>(source, 2, TimeUnit.SECONDS, scheduler))
.expectSubscription()
.verifyComplete(); //complete not relevant
fail("expected exception here");
}
catch (Throwable e) {
Throwable t = Exceptions.unwrap(e);
assertThat(t).isEqualTo(e)
.isInstanceOf(RejectedExecutionException.class)
.hasMessage("Scheduler unavailable");
assertThat(e).satisfies(Exceptions::isBubbling);
}
}