[英]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.