0

I've found so many different ways of converting my wire-tap loggers into a Java config but none of them seem to work.

Here is my XML version :

<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} " />

The channel is indeed poolable but I don't understand why it is not necessary to define a pooler for the XML syntax ?

Here is my attempt at converting it to Java (my SpEL is not working either) :

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

==========

Update :

I've tried your solution, @Gary, but I've got some weird effects. Here is what I got in my console :

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.

It seems to log only once every x2 the fixedRate on my InboundChannelAdapter.

Here is my logger and 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;
}
redAce
  • 1,558
  • 4
  • 14
  • 24

1 Answers1

0

You can use the .wiretap() method, or simply use a .log() method (which creates a wiretap internally).

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

}

Result:

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}]

You can't use the #{...} construct for SpEL here.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thank you Gary. I've updated my post with the weird results I got. I have also 2 questions : how can I avoid using `.handle(System.out::println)` it's polluting the console but when I delete it on Pollable channels I get a `No Subscriber error` ... and is it mandatory to define a poller for my pollable channels loggers using `IntegrationFlow` ? – redAce Aug 21 '18 at 11:38
  • You have to have something consume from the channel; the System.out was just a sample handler - you need to show what consumes from `tasksRequestChannel` in your XML version, then I can update my example to match. Also, you can't put Spring Integration components in `@RefreshScope` - that only works with passive components, not components that are actively part of an integration flow. – Gary Russell Aug 21 '18 at 14:29