代码之家  ›  专栏  ›  技术社区  ›  Deepboy

Spring集成+SpringBoot JUnit意外尝试连接到DB

  •  0
  • Deepboy  · 技术社区  · 6 年前

    请参阅随附的系统图。

    system diagram here

    问题:当我试图将消息发布到输入通道时,代码试图连接到数据库,并抛出一个无法连接的异常。

    内部代码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连接。

    当我试图将消息发布到producer channel时,代码试图连接到DB,并抛出一个无法连接的异常。

    我不希望这种情况发生,因为JUnit永远不应该与DB相关,应该随时随地运行。

    How do I fix this exception?
    

    注: 不确定这是否重要,该应用程序是一个Spring引导应用程序。我在代码中使用了Spring集成来读写队列。

    2 回复  |  直到 6 年前
        1
  •  0
  •   Gary Russell    6 年前

    由于公共通道是一个发布/订阅通道,因此消息将同时发送到两个流。

    如果这是对 this question/answer ,可以通过调用 stop() sendToDb 流动(只要你设定 ignoreFailures 就像我在酒吧/娱乐频道建议的那样。

    ((Lifecycle) sendToDb).stop();
    
        2
  •  0
  •   Deepboy    6 年前

    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**********永远不会被打印出来。