publishSubscribeChannel单元测试无法正常工作

时间:2018-12-28 11:55:00

标签: spring-integration spring-integration-dsl

我的集成配置类在下面,当我对它们进行一些单元测试时,发现: 当我向UserRecipientSubscribeCacheChannel发送消息时,它工作正常; 当我向通道userReportWriteCompletedRouteChannel的更高级别发送消息时,它工作失败,并且还没有引发任何异常。我不明白。当然,我发送的消息是相同的。

由于失败部分,下一个处理程序无法正常工作。 ty !!

下面可以正常工作,它打印===>ip location channel message:GenericMessage [payload=[MailRecipientActionDocumen...===>user recipient channel message:GenericMessage [payload=[UserRecipientSubscribeDataRedisStructure...

 @Test
public void test_sendMessageUserRecipientSubscribeCacheChannel(){
    UserRecipientSubscribeCacheChannel.send(createMessageWithIp());
}  

下面的工作失败,仅打印===>ip location channel message:GenericMessage [payload=[MailRecipientActionDocumen...

注意:失败部分,在处理程序前面有一个转换器。

@Test
public void test_sendMessageToRouteChannel() {
    userReportWriteCompletedRouteChannel.send(createMessageWithIp());
}

我的代码配置如下:

@Bean  
public SubscribableChannel userReportWriteCompletedSubscribeChannel() {  
    return new DirectChannel();  
} 

@Bean
public QueueChannel userReportWriteCompletedRouteChannel() {
    return new QueueChannel();
}

@Bean
public MessageChannel ipLocationResolveCacheChannel() {
    return new DirectChannel();
}

@Bean
public MessageChannel userRecipientSubscribeCacheChannel() {
    return new DirectChannel();
}

@MessagingGateway(name = "userReportWriteCompletedListener",
        defaultRequestChannel = "userReportWriteCompletedRouteChannel")
public interface UserReportWriteCompletedListener {
    @Gateway
    void receive(List<UserMailRecipientActionDocument> docs);
}

@Bean
public IntegrationFlow bridgeFlow() {
    return flow -> flow.channel("userReportWriteCompletedRouteChannel")
            .bridge(bridgeSpe -> bridgeSpe
                    .poller(pollerFactory -> pollerFactory.fixedRate(500).maxMessagesPerPoll(1)))

            .channel("userReportWriteCompletedSubscribeChannel")
            ;
}

@Bean
public IntegrationFlow subscribeFlow() {
    return IntegrationFlows.from("userReportWriteCompletedSubscribeChannel")
            .publishSubscribeChannel(publishSubscribeSpec -> publishSubscribeSpec
                    .subscribe(flow -> flow
                            .channel(IP_LOCATION_RESOLVE_CACHE_CHANNEL)
                    )
                    .subscribe(flow -> flow
                            .channel(USER_RECIPIENT_SUBSCRIBE_CACHE_CHANNEL)
                    ))
            .get();
}

@Bean
public RedisStoreWritingMessageHandler ipLocationResolveCacheHandler(RedisTemplate<String, ?> redisTemplate) {
    final RedisStoreWritingMessageHandler ipLocationResolveCacheHandler =
            new RedisStoreWritingMessageHandler(redisTemplate);
    ipLocationResolveCacheHandler.setKey("IP_LOCATION_RESOLVE_CACHE");
    return ipLocationResolveCacheHandler;
}

@Bean
public RedisStoreWritingMessageHandler userRecipientSubscribeCacheHandler(RedisTemplate<String, ?> redisTemplate) {
    final RedisStoreWritingMessageHandler userRecipientSubscribeCacheHandler =
            new RedisStoreWritingMessageHandler(redisTemplate);
    userRecipientSubscribeCacheHandler.setKey("USER_RECIPIENT_SUBSCRIBE_CACHE");
    return userRecipientSubscribeCacheHandler;
}

@Bean
public IpLocationResolveRedisStructureFilterAndTransformer recipientActionHasIpFilterAndTransformer() {
    return new IpLocationResolveRedisStructureFilterAndTransformer();
}

@Bean
public UserRecipientSubscribeDataRedisStructureTransformer subscribeDataRedisStructureTransformer(
        IpLocationClient ipLocationClient) {
    return new UserRecipientSubscribeDataRedisStructureTransformer(ipLocationClient);
}

@Bean
public IntegrationFlow ipLocationResolveCacheFlow(
        @Qualifier("ipLocationResolveCacheHandler") RedisStoreWritingMessageHandler writingMessageHandler) {
    return flow -> flow.channel(IP_LOCATION_RESOLVE_CACHE_CHANNEL)
            .handle(message -> {
                System.out.println("===>ip location  channel message:" + message);
            })

            ;
}

@Bean
public IntegrationFlow userRecipientActionDataCacheFlow(
        @Qualifier("userRecipientSubscribeCacheHandler") RedisStoreWritingMessageHandler messageHandler,
        UserRecipientSubscribeDataRedisStructureTransformer transformer) {
    return flow -> flow.channel(USER_RECIPIENT_SUBSCRIBE_CACHE_CHANNEL)
            .transform(transformer)
                   .handle(message -> {
                System.out.println("===>user recipient  channel message:" + message);
            })      
}  

我希望有2条打印消息信息,但仅打印1条。

1 个答案:

答案 0 :(得分:0)

今天,我发现网桥流可能有问题,当我将处理程序移到通道<!DOCTYPE html> <html> <head> <title>JavaScript Password Meter</title> <script> window.addEventListener('load', function() { var password = document.getElementById("pwd"); password.addEventListener('change', function() { // Reset if password length is zero if (password.length === 0) { document.getElementById("progresslabel").innerHTML = ""; document.getElementById("progress").value = "0"; return; } // Password requirements var match = new Array(); match.push("[$@$!%*#?&]"); // Special Chars match.push("[A-Z]"); // Uppercase match.push("[0-9]"); // Numbers match.push("[a-z]"); // Lowercase // Check progress var prog = 0; for (var i = 0; i < match.length; i++) { if (new RegExp(match[i]).test(password)) { prog++; } } //Length must be at least 8 chars if(prog > 2 && password.length > 7){ prog++; } // Display it var progress = ""; var strength = ""; switch (prog) { case 0: case 1: case 2: strength = "25%"; progress = "25"; break; case 3: strength = "50%"; progress = "50"; break; case 4: strength = "75%"; progress = "75"; break; case 5: strength = "100% - Password strength is good"; progress = "100"; break; } document.getElementById("progresslabel").innerHTML = strength; document.getElementById("progress").value = progress; }); }); </script> </head> <body> <form> <div> <label for="pwd">Password:</label> <input type="text" id="pwd"> <progress id="progress" value="0" max="100">70</progress> <span id="progresslabel"></span></div> </form> </body> </html>后时,它无法打印任何消息; 当我删除频道并直接添加处理程序时,它将打印消息。 我用错了桥吗?

userReportWriteCompletedSubscribeChannel

测试:

   @Bean
public IntegrationFlow bridgeFlow() {
    return flow -> flow.channel("userReportWriteCompletedRouteChannel")
            .bridge(bridgeSpe -> bridgeSpe
                    .poller(pollerFactory -> pollerFactory.fixedRate(100).maxMessagesPerPoll(1)))
            .handle(message -> {
                System.out.println("===>route  channel message:" + message);
            })   // handle ok ,  will print message
            .channel("userReportWriteCompletedSubscribeChannel")
            // .handle(message -> {
            //    System.out.println("===>route  channel message:" + message);
            // })       // handle fail ,  will not printing message
            ;
}