我IntegrationFlow使用 Spring Integration 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();
}
}
當我嘗試向我的網關發布訊息時
dummyGateway.echo("test");
我得到了例外:
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}]
我想,.handle()在我的流程中做的正是訂閱一個頻道。那么,為什么我會得到這個例外?在這種情況下如何正確訂閱我的頻道?
uj5u.com熱心網友回復:
不,ctor太早了。此時創建了 bean,但它們還沒有開始繁重的作業。您不能從 bean 初始化階段進行低級資源互動(技術上是任何操作)。您需要等到應用程式背景關系完全啟動。請學習 Spring 容器的生命周期:https ://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-factory-lifecycle-processor 。
您可以為 實作SmartLifecycle或 監聽器ContextStartedEvent。但是 bean 初始化階段開始發送訊息還為時過早。
之所以QueueChannel有效,是因為它有自己的內部緩沖區來保存訊息,直到它們被使用。它們在端點啟動時被消耗。如果DirectChannel沒有緩沖區并且當我們發送訊息時立即呼叫消費者。在 bean 初始化階段,該通道上還沒有訂閱者。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422675.html
標籤:
