简体   繁体   English

Spring 在 IntegrationFlow 中使用 DirectChannel 进行集成会引发“调度程序没有订阅者”

[英]Spring Integration using DirectChannel in IntegrationFlow throws "Dispatcher has no subscribers"

I have this very basic setup of IntegrationFlow with Spring Integration Java DSL:我有这个非常基本的IntegrationFlow设置和 Spring 集成 Java DSL:

@IntegrationComponentScan
@EnableIntegration
@Configuration
public class DummyConfig {

    @MessagingGateway
    public interface DummyGateway {
        @Gateway(requestChannel = "dummyInChannel")
        void echo(String payload);
    }

    @Bean(name = "dummyInChannel")
    public MessageChannel dummyInChannel() {
        return MessageChannels.direct().get();
    }

    @Bean
    public IntegrationFlow dummyFlow() {
        return IntegrationFlows.from(dummyInChannel())
            .handle(String.class, (payload, headers) -> {
                System.out.println(payload);
                return "";
            })
            .get();
    }

}

When I try to post a message to my gateway当我尝试向我的网关发布消息时

dummyGateway.echo("test");

I'm getting and exception:我得到了例外:

Caused by: org.springframework.messaging.MessageDeliveryException: 
Dispatcher has no subscribers for channel 'application.dummyInChannel'.; nested exception 
is org.springframework.integration.MessageDispatchingException: Dispatcher 
has no subscribers, failedMessage=GenericMessage [payload=test, 
headers={replyChannel=nullChannel, id=6e4302e4-95f0-bf5a-c1a3-e8cd587c23fb, timestamp=1643269549272}]

I thought, that doing .handle() in my flow is exactly subscribing to a channel.我想,在我的流程中执行.handle()正是订阅了一个频道。 Then, why am I getting this exception?那么,为什么我会得到这个异常? How to properly subscribe to my channel in this scenario?在这种情况下如何正确订阅我的频道?

No, the ctor is too early.不,ctor太早了。 The beans are created at this point, but they have not started their heavy lifting.此时创建了 bean,但它们还没有开始繁重的工作。 You cannot do low-level resources interaction (technically any actions) from the bean initialization phase.您不能从 bean 初始化阶段进行低级资源交互(技术上是任何操作)。 You need to wait until application context is fully started.您需要等到应用程序上下文完全启动。 Please, learn a lifecycle of Spring container: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-lifecycle-processor .请了解 Spring 容器的生命周期: https://docs.spring.io/spring-framework/docs/current/reference/html-cycle.html#beans

You may implement a SmartLifecycle , or listener for the ContextStartedEvent .您可以为ContextStartedEvent实现SmartLifecycle或侦听器。 But bean initialization phase is really too early to start emitting messages.但是 bean 初始化阶段开始发送消息还为时过早。

The QueueChannel works because it has its own internal buffer to keep messages until they are consumed. QueueChannel有效,是因为它有自己的内部缓冲区来保存消息直到它们被消费。 And they are consumed when the endpoint is started.它们在端点启动时被消耗。 In case of DirectChannel there is no buffer and consumer is called immediately, when we send a message.DirectChannel没有缓冲区的情况下,当我们发送消息时会立即调用消费者。 There is just no subscriber yet on that channel within bean initialization phase.在 bean 初始化阶段,该通道上还没有订阅者。

public static void main(String[] args) {
        SpringApplication application = new SpringApplication(DummyConfig.class);
        ConfigurableApplicationContext ctx = application.run(args);
         ctx.getBean(DummyGateway.class).echo("MyAwesomeString");
        ctx.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Integration Error - Dispatcher没有通道订阅者 - Spring Integration Error - Dispatcher has no subscribers for channel 调度程序没有订阅者 - Spring集成JMS - dispatcher has no subscribers - spring integration JMS Spring集成:分派器没有该频道的订阅者 - Spring Integration : Dispatcher has no subscribers for channel Spring Integration Java DSL-IntegrationFlow多个订户 - Spring Integration Java DSL - IntegrationFlow multiple subscribers TCP Spring集成-Dispatcher没有“响应”通道的订阅者 - TCP Spring Integration - Dispatcher has no subscribers for 'response' channel 接收:MessageDeliveryException:分派器没有订阅者在2个不同的spring集成模块中使用入站通道适配器 - Receiving: MessageDeliveryException: Dispatcher has no subscribers using inbound-channel-adapter in 2 different spring integration modules Spring 集成 TcpOutboundGateway、ServiceActivator、消息通道和错误 MessageDispatchingException:调度程序没有订阅者 - Spring Integration TcpOutboundGateway, ServiceActivator, Message Channel and Error MessageDispatchingException: Dispatcher has no subscribers 调度员没有订阅者 - Dispatcher has no subscribers MessageDeliveryException:调度程序没有订阅者 - MessageDeliveryException: Dispatcher has no subscribers Spring Integration - 路由到 DirectChannel 时丢失消息 - Spring Integration - Missing messages while routing to DirectChannel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM