请参阅所附的系统图。
问题:当我试图将消息发送到输入通道时,代码尝试连接到DB,并抛出它无法连接的异常。。
从通道读取5 ->中的代码,应用业务逻辑(目前为空)并将响应发送到另一个通道。
@Bean
public IntegrationFlow sendToBusinessLogictoNotifyExternalSystem() {
return IntegrationFlows
.from("CommonChannelName")
.handle("Business Logic Class name") // Business Logic empty for now
.channel("QueuetoAnotherSystem")
.get();
} 我已经为5编写了JUnit,如下所示,
@Autowired
PublishSubscribeChannel CommonChannelName;
@Autowired
MessageChannel QueuetoAnotherSystem;
@Test
public void sendToBusinessLogictoNotifyExternalSystem() {
Message<?> message = (Message<?>) MessageBuilder.withPayload("World")
.setHeader(MessageHeaders.REPLY_CHANNEL, QueuetoAnotherSystem).build();
this.CommonChannelName.send((org.springframework.messaging.Message<?>) message);
Message<?> receive = QueuetoAnotherSystem.receive(5000);
assertNotNull(receive);
assertEquals("World", receive.getPayload());
}问题:从系统图中可以看到,我的代码在不同的流上也有一个DB连接。
当我试图将消息发送到生产者通道时,代码尝试连接到DB,并抛出它无法连接的异常。
我不希望这种情况发生,因为JUnit不应该与DB相关,应该在任何地方、任何时候运行。
How do I fix this exception?注意:不确定是否重要,应用程序是Spring应用程序。我使用代码中的从队列中读取和写入队列。
发布于 2018-05-26 18:25:49
因为公共通道是发布/订阅通道,所以消息会同时流向这两个流。
如果这是对这个问题/答案的后续,则可以通过在sendToDb流上调用stop()来防止调用DB流(只要您在pub/sub上将ignoreFailures设置为true,就像我在这里建议的那样)。
((Lifecycle) sendToDb).stop();发布于 2018-05-26 23:39:09
JUNIT测试用例更新:
@Autowired
PublishSubscribeChannel CommonChannelName;
@Autowired
MessageChannel QueuetoAnotherSystem;
@Autowired
SendResponsetoDBConfig sendResponsetoDBConfig;
@Test
public void sendToBusinessLogictoNotifyExternalSystem() {
Lifecycle flowToDB = ((Lifecycle) sendResponsetoDBConfig.sendToDb());
flowToDB.stop();
Message<?> message = (Message<?>) MessageBuilder.withPayload("World")
.setHeader(MessageHeaders.REPLY_CHANNEL, QueuetoAnotherSystem).build();
this.CommonChannelName.send((org.springframework.messaging.Message<?>) message);
Message<?> receive = QueuetoAnotherSystem.receive(5000);
assertNotNull(receive);
assertEquals("World", receive.getPayload());
}4的代码:处理到DB的消息的流
public class SendResponsetoDBConfig {
@Bean
public IntegrationFlow sendToDb() {
System.out.println("******************* Inside SendResponsetoDBConfig.sendToDb ***********");
return IntegrationFlows
.from("Common Channel Name")
.handle("DAO Impl to store into DB")
.get();
}
}注:*在SendResponsetoDBConfig.sendToDb *从未打印过。
https://stackoverflow.com/questions/50545304
复制相似问题