登录
首页 >  文章 >  java教程

RxJava中如何优雅地在onComplete调用时取消订阅?

时间:2024-12-13 20:28:03 462浏览 收藏

怎么入门文章编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《RxJava中如何优雅地在onComplete调用时取消订阅?》,涉及到,有需要的可以收藏一下

RxJava中如何优雅地在onComplete调用时取消订阅?

如何在 rxjava 中在 oncomplete 调用时取消订阅

在 rxjava 中,通常希望在 observable 的 oncomplete 事件上取消订阅,以防止进一步的事件发射。这可以通过设置 completablefuture 来实现。

非 android 环境中使用 rxjava 2.x 的示例

flowable 由持久层方法返回,作为调用方中的 result.getall(dbname.get(), strategy) 和 result.gettablecolumn(table)。

public class consoleschemaflowableoutput implements schemaflowableoutput {
    private static final logger logger = loggerfactory.getlogger(consoleschemaflowableoutput.class);
    private volatile completablefuture<string> future = new completablefuture<>();
    private atomicinteger count = new atomicinteger(0);

    @override
    public disposable flush(information information, flowable<table> table) throws schemaexportexception {
        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;
    }

    @override
    public completablefuture<string> getfuture() {
        return future;
    }
    ...
}

调用方

public void export(Information info, SchemaFlowableOutput out) throws SchemaExportException {
    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());
    }
}

private void clearHander(Disposable disposable, String reason) {
    logger.info(reason);
    if (null != disposable && !disposable.isDisposed()) {
        disposable.dispose();
    } else {
        if (null != disposable) {
            logger.info("[CH]disposable status:" + disposable.isDisposed());
        } else {
            logger.info("[CH]disposable is null:");
        }
    }
    // 结束后的回调,执行一些清理工作
    completeHandler.apply();
}

通过将 completablefuture 设置为 "ok" 来通知调用方 observable 已结束,这将取消订阅并执行后续处理。

到这里,我们也就讲完了《RxJava中如何优雅地在onComplete调用时取消订阅?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

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