Spring Integration Java DSL中的WireTap配置

时间:2016-02-26 23:14:21

标签: java spring spring-integration

我已将WireTap配置添加到我的Spring Integration Java DSL中。

我查看了讨论What is the equivalent of logging-channel-adapter in java DSL,现在我的主要Java Config文件看起来像

@Configuration
@Import(LoggerConfiguration.class)
@EnableIntegration
public class ProcessorConfiguration {

    @Autowired
    private WireTap wireTap;

    @Bean
    public QueueChannel inputChannel() {
        return MessageChannels.queue(500)
            .interceptor(wireTap)
            .get();
    }

    @Bean
    public PublishSubscribeChannel outputChannel() {
        return MessageChannels.publishSubscribe()
            .interceptor(wireTap)
            .get();
    }
    ...
}

LoggerConfiguration

@Configuration
public class LoggerConfiguration {

    public static final LoggingHandler.Level INFO = LoggingHandler.Level.INFO;

    @Bean
    public WireTap wireTap() {
        return new WireTap(loggerChannel());
    }

    @Bean
    public IntegrationFlow loggerChain() {
        return IntegrationFlows.from(loggerChannel())
               .handle(loggerHandler())
               .get();
    }

    @Bean
    public MessageChannel loggerChannel() {
        return MessageChannels.direct().get();
    }

    public MessageHandler loggerHandler() {
        LoggingHandler loggingHandler = new LoggingHandler(INFO.name());
        ...  
        return loggingHandler;
   }
}

代码的所有音调(!)只能在这些简单的行中达到我在XML配置中所拥有的内容

<int:channel id="inputChannel">
    <int:queue capacity="500"/>
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:channel>

<int:publish-subscribe-channel id="outputChannel">
    <int:interceptors>
        <int:wire-tap channel="logger"/>
    </int:interceptors>
</int:publish-subscribe-channel>

<int:logging-channel-adapter id="logger" log-full-message="true" level="INFO"/>

如何在Java DSL中配置wireTaps更简洁,更简单?

1 个答案:

答案 0 :(得分:1)

由于你真的没有flow,你最终可以得到:

@ServiceActivator(inputChannel = "loggerChannel") 
public MessageHandler loggerHandler() {
     LoggingHandler loggingHandler = new LoggingHandler(INFO.name());
     ...  
     return loggingHandler;
}

如果只有简单的IntegrationFlow逻辑,则没有理由声明“详细”handle

Java DSL真的可以与原始的Java&amp;注释配置甚至XML配置也可以存在。

WireTap可能不会被声明为@Bean

@Bean
public PublishSubscribeChannel outputChannel() {
    return MessageChannels.publishSubscribe()
        .interceptor(new WireTap(loggerChannel()))
        .get();
}

XML定义通过解析器逻辑为<int:wire-tap>声明bean。

使用Java配置我们没有太多选择,除非我们希望将它作为bean用于@Bean目标类。

从另一方面来说,Java DSL具有.wireTap() EIP方法。因此,您不需要声明所有这些bean。例如。来自我们的测试案例:

@Bean
public IntegrationFlow wireTapFlow5() {
    return f -> f
            .wireTap(sf -> sf
                    .<String, String>transform(String::toUpperCase)
                    .channel(c -> c.queue("wireTapSubflowResult")))
            .channel("nullChannel");
}

如您所见,我们在那里没有任何额外@Bean,甚至我们也不需要WireTap的额外频道。

让我知道您希望看到它更简单!