首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot +SpringBoot JUnit意外地尝试连接到DB

SpringBoot +SpringBoot JUnit意外地尝试连接到DB
EN

Stack Overflow用户
提问于 2018-05-26 17:21:20
回答 2查看 77关注 0票数 0

请参阅所附的系统图。

系统图

问题:当我试图将消息发送到输入通道时,代码尝试连接到DB,并抛出它无法连接的异常。

从通道读取5 ->中的代码,应用业务逻辑(目前为空)并将响应发送到另一个通道。

代码语言:javascript
复制
@Bean
public IntegrationFlow sendToBusinessLogictoNotifyExternalSystem() {

    return IntegrationFlows
            .from("CommonChannelName")
            .handle("Business Logic Class name") // Business Logic empty for now
            .channel("QueuetoAnotherSystem")
                            .get();
    } 

我已经为5编写了JUnit,如下所示,

代码语言:javascript
复制
@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相关,应该在任何地方、任何时候运行。

代码语言:javascript
复制
How do I fix this exception?

注意:不确定是否重要,应用程序是Spring应用程序。我使用代码中的从队列中读取和写入队列。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-26 18:25:49

因为公共通道是发布/订阅通道,所以消息会同时流向这两个流。

如果这是对这个问题/答案的后续,则可以通过在sendToDb流上调用stop()来防止调用DB流(只要您在pub/sub上将ignoreFailures设置为true,就像我在这里建议的那样)。

代码语言:javascript
复制
((Lifecycle) sendToDb).stop();
票数 0
EN

Stack Overflow用户

发布于 2018-05-26 23:39:09

JUNIT测试用例更新:

代码语言:javascript
复制
@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的消息的流

代码语言:javascript
复制
    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 *从未打印过。

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

https://stackoverflow.com/questions/50545304

复制
相关文章

相似问题

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