Spring Integration + SpringBoot JUnit tries to con

2019-08-23 02:00发布

问题:

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.