将窃听从XML语法转换为Spring Integration Java批注或DSL

时间:2018-08-20 22:14:05

标签: spring spring-boot spring-integration spring-el spring-integration-dsl

我发现了很多将窃听记录器转换为Java配置的不同方法,但是似乎都没有。

这是我的XML版本:

<int:channel id="tasksRequestChannel">
    <int:queue capacity="${channel.queue.capacity}"/>
    <int:interceptors>
        <int:wire-tap channel="logRequestingTasks" />
    </int:interceptors>
</int:channel>

<int:logging-channel-adapter id="logRequestingTasks" level="INFO"
    expression="'Requesting tasks : ' + headers.#{T(com.application.infrastructure.CommonConstants).KEY_TASK_NAME} + ' of ID : ' + headers.#{T(com.application.infrastructure.CommonConstants).KEY_TASK_ID} " />

该通道确实是可合并的,但是我不明白为什么不必为XML语法定义一个合并器?

这是我尝试将其转换为Java(我的SpEL也不起作用):

@Bean
public IntegrationFlow logRequestingTasks(@Qualifier("defaultPoller") PollerMetadata defaultPoller) {
    LoggingHandler loggingHandler = new LoggingHandler(LoggingHandler.Level.INFO.name());
    loggingHandler.setLogExpressionString("'Requesting tasks : ' + headers.#{T(com.application.infrastructure.CommonConstants).KEY_TASK_NAME} + ' of ID : ' + headers.#{T(com.application.infrastructure.CommonConstants).KEY_TASK_ID} ");
    loggingHandler.setLoggerName("logRequestingTasks");

    return IntegrationFlows.from("tasksRequestChannel")
             .handle(loggingHandler, e -> e.poller(defaultPoller))
             .get();
}

==========

更新:

我已经尝试过您的解决方案@Gary,但是我得到了一些奇怪的效果。这是我在控制台中得到的:

2018-08-21 13:22:50.347  INFO 7060 --- [ask-scheduler-3] T.RestTemplate                           : step=INIT;...
2018-08-21 13:22:50.564  INFO 7060 --- [ask-scheduler-3] T.RestTemplate                           : step=SUCCESS;...
2018-08-21 13:22:50.824  INFO 7060 --- [ask-scheduler-3] c.a.t.d.a.TasksSplitter            : No active task retrieved.
2018-08-21 13:23:20.343  INFO 7060 --- [ask-scheduler-9] logStartDmwProcess                       : Start of Application process.
2018-08-21 13:23:20.346  INFO 7060 --- [ask-scheduler-9] T.RestTemplate                           : step=INIT;...
2018-08-21 13:23:20.540  INFO 7060 --- [ask-scheduler-9] T.RestTemplate                           : step=SUCCESS;...
2018-08-21 13:23:20.555  INFO 7060 --- [ask-scheduler-9] c.a.t.d.a.TasksSplitter            : No active task retrieved.

似乎每隔x2在我的InboundChannelAdapter上记录一次fixedRate。

这是我的记录器和InboundChannelAdapter:

@Bean
public IntegrationFlow logStartProcess() {       
    Expression logExpression = new SpelExpressionParser().parseExpression("'Start of Application process.'");
    return IntegrationFlows.from("initTimestampChannel")
                .log(Level.INFO, "logStartProcess", logExpression)
                .get();
}

@RefreshScope
@Bean
@InboundChannelAdapter(value = "initTimestampChannel", poller = @Poller(fixedRate = "30000"))
public MessageSource<?> buildTasksRequest() {
    MethodInvokingMessageSource source = new MethodInvokingMessageSource();
    source.setObject(tasksService);
    source.setMethodName("requestAllTasks");
    return source;
}

1 个答案:

答案 0 :(得分:0)

您可以使用.wiretap()方法,也可以简单地使用.log()方法(内部创建窃听)。

@SpringBootApplication
public class So51939181Application {

    private static final Expression logExpression = new SpelExpressionParser().parseExpression(
            "'Requesting tasks : ' + headers." + CommonConstants.KEY_TASK_NAME
            + " + ' of ID : ' + headers." + CommonConstants.KEY_TASK_ID);

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

    @Bean
    public IntegrationFlow flow() {
        return IntegrationFlows.from("foo")
                .log(logExpression)
                // .more stuff
                .handle(System.out::println)
                .get();
    }

    public static void main(String[] args) {
        SpringApplication.run(So51939181Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(MessageChannel foo) {
        return args -> foo.send(MessageBuilder.withPayload("foo")
                .setHeader(CommonConstants.KEY_TASK_NAME, "name")
                .setHeader(CommonConstants.KEY_TASK_ID, "id")
                .build());
    }

}

结果:

2018-08-20 20:19:44.777  INFO 4096 --- [           main] o.s.integration.handler.LoggingHandler   : Requesting tasks : aName of ID : anId
GenericMessage [payload=foo, headers={taskName=name, id=b21dde6e-bfa3-f727-52f0-056cb2775ee7, taskId=id, timestamp=1534810784776}]

您不能在此处为SpEL使用#{...}构造。