Spring Integration + SpringBoot JUnit tries to connect to DB unexpectedly

[亡魂溺海] 提交于 2019-12-13 03:45:02

问题


Please refer to system diagram attached.

system diagram here

ISSUE: When I try to post message to input channel, the code tries to connect to the DB and throws an exception that it is unable to connect.

Code inside 5 -> Read from a channel, apply Business Logic (empty for now) and send the response to another channel.

@Bean
public IntegrationFlow sendToBusinessLogictoNotifyExternalSystem() {

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

I have written the JUnit for 5 as given below,

@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());
    }

ISSUE: As you can see from the system diagram, my code also has a DB connection on a different flow.

When I try to post message to producer channel, the code tries to connect to the DB and throws an exception that it is unable to connect.

I do not want this to happen, because the JUnit should never be related to the DB, and should run anywhere, anytime.

How do I fix this exception?

NOTE: Not sure if it matters, the application is a Spring Boot application. I have used Spring Integration inside the code to read and write from/to queues.


回答1:


Since the common channel is a publish/subscribe channel, the message goes to both flows.

If this is a follow-up to this question/answer, you can prevent the DB flow from being invoked by calling stop() on the sendToDb flow (as long as you set ignoreFailures to true on the pub/sub channel, like I suggested there.

((Lifecycle) sendToDb).stop();



回答2:


JUNIT TEST CASE - UPDATED:

@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());
    }

CODE FOR 4: The flow that handles message to 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();
    }   
}

NOTE: ******************* Inside SendResponsetoDBConfig.sendToDb *********** never gets printed.



来源:https://stackoverflow.com/questions/50545304/spring-integration-springboot-junit-tries-to-connect-to-db-unexpectedly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!