登录
首页 >  文章 >  java教程

RxJava2.x中,如何优雅地在Observable的onComplete回调后取消订阅?

时间:2024-12-20 20:45:49 245浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是文章学习者,那么本文《RxJava2.x中,如何优雅地在Observable的onComplete回调后取消订阅?》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

RxJava2.x中,如何优雅地在Observable的onComplete回调后取消订阅?

rxjava的observable订阅时如何实现oncomplete取消订阅

使用rxjava 2.x时,要在oncomplete被调用时取消订阅,可以使用completablefuture.complete向调用方发送完成信号。

示例:

flowable由持久层方法返回,以下是调用方中的代码:

...
private volatile completablefuture<string> future = new completablefuture<>();
private atomicinteger count = new atomicinteger(0);
@override
public disposable flush(information information, flowable<table> table) throws schemaexportexception {
    logger.info("start flowable flush");
    disposable export_flush_complete = table.subscribe(tableins -> {
        system.out.println(printasciitable(tableins));
        system.out.println(printasciicolumns(tableins.getcolumns()));
        system.out.println("\r\n");
        count.addandget(1);
    }, throwable -> {
        logger.debug("export break, reason: " + throwable.getmessage());
        future.cancel(true);
        throw new schemaexportexception(throwable);
    }, new action() {
        @override
        public void run() throws exception {
            logger.debug("export complete, affect size:" + count.get());
            future.complete("ok");
        }
    });
    return export_flush_complete;
}
...

调用方的代码:

...
    long startStamp = System.currentTimeMillis();
    // Flowable
    Flowable<Table> tableFlowable = result.getAll(dbName.get(), strategy).flatMap(new Function<Table, Publisher<Table>>() {
        @Override
        public Publisher<Table> apply(@NonNull Table table) throws Exception {
            return result.getTableColumn(table).flatMap(new Function<List<Column>, SingleSource<Table>>() {
                @Override
                public SingleSource<Table> apply(@NonNull List<Column> columns) throws Exception {
                    return Single.just(table.fillColumn(columns));
                }
            }).flatMapPublisher(new Function<Table, Publisher<? extends Table>>() {
                @Override
                public Publisher<? extends Table> apply(@NonNull Table table) throws Exception {
                    return Flowable.just(table);
                }
            });
        }
    });
    Disposable disposable = null;
    try {
        disposable = out.flush(info, tableFlowable);
        CompletableFuture<String> future = out.getFuture();
        while (!future.isDone()) {
            logger.info("[ERE-Flowable]未完成,线程休眠1秒");
            Thread.currentThread().sleep(1000, 0);
        }
        String result = future.get();
        logger.info("[ERE-Flowable]完成, 结果:" + result);
        if (result.equals("OK")) {
            long finishStamp = System.currentTimeMillis();
            clearHander(disposable, "[ERE-Flowable]RxJava disposed because complete, WithTime: " + (finishStamp - startStamp));
        }
    } catch (Exception e) {
        clearHander(disposable, "[ERE-Flowable]RxJava disposed has Exception: " + e.getMessage());
    }
...

今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>