reactor.core.publisher.BufferOverflowStrategy Java Examples

The following examples show how to use reactor.core.publisher.BufferOverflowStrategy. 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: DefaultElasticSearchService.java    From jetlinks-community with Apache License 2.0 5 votes vote down vote up
public void init() {
    //最小间隔
    int flushRate = Integer.getInteger("elasticsearch.buffer.rate", 1000);
    //缓冲最大数量
    int bufferSize = Integer.getInteger("elasticsearch.buffer.size", 3000);
    //缓冲超时时间
    Duration bufferTimeout = Duration.ofSeconds(Integer.getInteger("elasticsearch.buffer.timeout", 3));
    //缓冲背压
    int bufferBackpressure = Integer.getInteger("elasticsearch.buffer.backpressure", 64);

    //这里的警告都输出到控制台,输入到slf4j可能会造成日志递归.
    FluxUtils.bufferRate(
        Flux.<Buffer>create(sink -> this.sink = sink),
        flushRate,
        bufferSize,
        bufferTimeout)
        .onBackpressureBuffer(bufferBackpressure,
            drop -> System.err.println("无法处理更多索引请求!"),
            BufferOverflowStrategy.DROP_OLDEST)
        .flatMap(this::doSave)
        .doOnNext((len) -> {
            if (log.isDebugEnabled() && len > 0) {
                log.debug("保存ElasticSearch数据成功,数量:{}", len);
            }
        })
        .onErrorContinue((err, obj) -> System.err.println("保存ElasticSearch数据失败:\n" + org.hswebframework.utils.StringUtils.throwable2String(err)))
        .subscribe();
}
 
Example #2
Source File: BackPressureTest.java    From Hands-On-Reactive-Programming-with-Reactor with MIT License 5 votes vote down vote up
@Test
public  void testBackPressureOps() throws  Exception{
    Flux<Integer> numberGenerator = Flux.create(x -> {
        System.out.println("Requested Events :"+x.requestedFromDownstream());
        int number = 1;
        while(number < 100) {
            x.next(number);
            number++;
        }
        x.complete();
    });

    CountDownLatch latch = new CountDownLatch(1);
    numberGenerator
            .onBackpressureBuffer(2,x -> System.out.println("Dropped :"+x),BufferOverflowStrategy.DROP_LATEST)
            .subscribe(new BaseSubscriber<Integer>() {
        @Override
        protected void hookOnSubscribe(Subscription subscription) {
            request(1);
        }

        @Override
        protected void hookOnNext(Integer value) {
            System.out.println(value);
        }

        @Override
        protected void hookOnError(Throwable throwable) {
            throwable.printStackTrace();
            latch.countDown();
        }

        @Override
        protected void hookOnComplete() {
            latch.countDown();
        }
    });
    assertTrue(latch.await(1L, TimeUnit.SECONDS));
}
 
Example #3
Source File: JobActivityPublisher.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private Flux<JobManagerEvent<?>> jobManagerStream() {
    Scheduler scheduler = Schedulers.newSingle(JOB_ACTIVITY_PUBLISHER_SCHEDULER, true);
    // The TitusRuntime emits stream metrics so we avoid explicitly managing them here
    return ReactorExt.toFlux(runtime.persistentStream(v3JobOperations.observeJobs()))
            .onBackpressureBuffer(jobActivityStreamBufferSize,
                    jobManagerEvent -> {
                        metrics.publishDropError();
                        logger.warn("Dropping events due to back pressure buffer of size {} overflow",
                                jobActivityStreamBufferSize);
                    },
                    BufferOverflowStrategy.DROP_LATEST)
            .subscribeOn(scheduler);
}
 
Example #4
Source File: ClusterMembershipTransactionLogger.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
static Disposable logEvents(Flux<ClusterMembershipEvent> events) {
    Scheduler scheduler = Schedulers.newSingle("ClusterMembershipTransactionLogger");
    return events
            .onBackpressureBuffer(BUFFER_SIZE, BufferOverflowStrategy.ERROR)
            .publishOn(scheduler)
            .onErrorResume(e -> {
                logger.warn("Transactions may be missing in the log. The event stream has terminated with an error and must be re-subscribed: {}", ExceptionExt.toMessage(e));
                return Flux.interval(RETRY_INTERVAL).take(1).flatMap(tick -> events);
            })
            .subscribe(
                    event -> logger.info(doFormat(event)),
                    e -> logger.error("Event stream terminated with an error", e),
                    () -> logger.info("Event stream completed")
            );
}