首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >终止外部事件的订阅

终止外部事件的订阅
EN

Stack Overflow用户
提问于 2021-01-27 22:11:12
回答 1查看 170关注 0票数 0

在应用程序中,我对外部HTTP端点使用长轮询。我使用Spring的反应式WebClient来做这件事。为了在应用程序停止时干净利落地关闭(并避免难看的Netty堆栈跟踪),我将takeUntil()与一个EmitterProcessor实例一起使用,当Spring停止我的bean (我实现了SmartLifecycle)时,我在该实例上调用onNext()

整个过程是这样的:

代码语言:javascript
复制
@Component
@RequiredArgsConstructor
@Slf4j
public class LongPollingMessageReceiver implements SmartLifecycle {
    private boolean running = true;

    private final EmitterProcessor<Boolean> shutdown = EmitterProcessor.create();

    private final BackendMessageReceiver backendMessageReceiver;

    public void waitForMessages() {
        Mono.defer(() -> backendMessageReceiver.receiveMessages()) // Calls WebClient
            .repeat()
            .takeUntilOther(shutdown)
            .subscribe(event -> {
                // do something when the http endpoint answers
            });
    }

    @Override
    public int getPhase() {
        // We need to cancel the subscriptions before Reactor/Netty shuts down.
        // Using @PreDestroy does not work because it is called *after* the Reactor/Netty shutdown.
        return 0;
    }

    @Override
    public void start() {
        // Not needed
    }

    @Override
    public void stop() {
        log.info("Stopping message subscriptions");
        shutdown.onNext(true);
        shutdown.onComplete();
        running = false;
    }

    @Override
    public boolean isRunning() {
        return running;
    }
}

就目前而言,整个机制运行良好。但是,EmitterProcessor被标记为@Deprecated,并且javadoc要求使用SinkSink不实现Publisher接口,因此无法传递到takeUntilOther()中。

我应该怎么做才能解决这个问题,而不会永远被困在小于3.5的项目中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-28 17:09:39

Sinks旨在作为以编程方式触发反应式事件的面向开发人员的应用编程接口。但是,如果没有一种方法将它们作为典型的FluxMono呈现给应用程序的其余部分,那么这就没有多大用处。

Sinks.Many有一个asFlux()视图来实现这一效果。类似地,Sinks.OneSinks.Empty也有一个asMono()视图。

这就是你可以用来传递给takeUntilOther的东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65920864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档