Sasi
Sasi

Reputation: 89

spring integration passing message to channel

I am trying to use spring integration for receiving an email, parse it to invoke a service and then conditionally forward the same email. I am able to read, parse and send the email, but however am not abel to forward the content

@Bean
    @Value("${imap.url}")
    public IntegrationFlow mailListener( String imapUrl) {
        return IntegrationFlows
                .from(s -> s.imap(imapUrl)
                                .searchTermStrategy((f,l) -> fromAndNotSeenTerm())
                                .javaMailProperties(p -> p.put("mail.debug", "false")),
                        e -> e.autoStartup(true)
                                .poller(p -> p.fixedDelay(10000)))
                .handle(Message.class, (payload, header) -> parseEmail(payload))
                .<Boolean, Boolean>route(p -> p,
                    m -> m.channelMapping("true", "sendMailChannel")
                          .channelMapping("false", "nullChannel"))
                .get();
    }


@Bean
    public IntegrationFlow sendMailFlow() {
        return IntegrationFlows.from("sendMailChannel")
                .enrichHeaders(Mail.headers().subjectFunction(m->"Email SUbj").from("[email protected]").toFunction(m -> new String[] { "[email protected]" }))
                .handle(Mail.outboundAdapter("smtp.host.net")
                                .port(25)
.protocol("smtp")
                                .javaMailProperties(p -> p.put("mail.debug", "true")))
                .get();
    }

Using the above code, the problem is to pass the content read from email(payload) to sendEmailChannel. Currently the output of parseEmail(Boolean) is going to the channel. Can you please advise on the way to accomplish it? Still couldn't figure how to solve that

Update1:

@Bean
    @Value("${imap.url}")
    public IntegrationFlow mailListener( String imapUrl) {
        return IntegrationFlows
                .from(s -> s.imap(imapUrl)
                                .searchTermStrategy((f,l) -> fromAndNotSeenTerm())
                                .javaMailProperties(p -> p.put("mail.debug", "false")),
                        e -> e.autoStartup(true)
                                .poller(p -> p.fixedDelay(10000)))
                .enrichHeaders(s -> s.headerExpressions(h -> h
                        .put("saved", "payload")))
                .handle(Message.class, (payload, header) -> parseEmail(payload))
                .<Boolean, Boolean>route(p -> p,
                    m -> m.channelMapping("true", "sendMailChannel")
                          .channelMapping("false", "nullChannel"))
                .get();
    }

I added the payload to header as suggested but couldn't figure out the transform to set the payload back from header. But even if i set it back to payload before routing,will it not break the flow? because the output of parse method(Boolean) is supposed to drive the route decision

Final Update

Thanks to Gary's help, Am able to make my first integration flow work by adding filter and transform

public IntegrationFlow mailListener( String imapUrl) {
    return IntegrationFlows
            .from(s -> s.imap(imapUrl)
                            .searchTermStrategy((f,l) -> fromAndNotSeenTerm())
                            .javaMailProperties(p -> p.put("mail.debug", "false")),
                    e -> e.autoStartup(true)
                            .poller(p -> p.fixedDelay(10000)))
            .enrichHeaders(s -> s.headerExpressions(h -> h
                    .put("saved", "payload")))
            .handle(Message.class, (payload, header) -> parseEmail(payload))
            .filter((Boolean p) -> p == true)
            .transform("headers['saved']")
            .channel(MessageChannels.queue("sendMailChannel"))
            .get();
}

Upvotes: 0

Views: 1465

Answers (1)

Gary Russell
Gary Russell

Reputation: 174749

Use a header enricher to save off the original payload in a header (header name= saved expression = payload) then a transformer after the parser to move it back to the paylaod (expression = headers['saved']).

Upvotes: 1

Related Questions