Message channels one or many?

In each child context, you can add a header enricher to set a custom header to the URL from the adapter; with the output channel being the shared channel to the shared service.

In the service, use void foo(Message emailMessage, @Header("myHeader") String url)

I would generally recommend using a single service unless the service needs to do radically different things based on the source.

EDIT:

I modified my answer to your previous question to enhance the original message with the url in a header; each instance has its own header enricher and they all route the enriched message to the common emailChannel.

@Configuration
@EnableIntegration
public class GeneralImapAdapter {

    @Value("${imap.url}")
    String imapUrl;

    @Bean
    public static PropertySourcesPlaceholderConfigurer pspc() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    @InboundChannelAdapter(value = "enrichHeadersChannel", poller = @Poller(fixedDelay = "10000") )
    public MessageSource<javax.mail.Message> mailMessageSource(MailReceiver imapMailReceiver) {
        return new MailReceivingMessageSource(imapMailReceiver);
    }

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

    @Bean
    @Transformer(inputChannel="enrichHeadersChannel", outputChannel="emailChannel")
    public HeaderEnricher enrichHeaders() {
        Map<String, ? extends HeaderValueMessageProcessor<?>> headersToAdd =
                Collections.singletonMap("emailUrl", new StaticHeaderValueMessageProcessor<>(this.imapUrl));
        HeaderEnricher enricher = new HeaderEnricher(headersToAdd);
        return enricher;
    }

    @Bean
    public MailReceiver imapMailReceiver() {
        MailReceiver receiver = mock(MailReceiver.class);
        Message message = mock(Message.class);
        when(message.toString()).thenReturn("Message from " + this.imapUrl);
        Message[] messages = new Message[] {message};
        try {
            when(receiver.receive()).thenReturn(messages);
        }
        catch (MessagingException e) {
            e.printStackTrace();
        }
        return receiver;
    }

}

…and I modified the receiving service so it gets access to the header…

@MessageEndpoint
public class EmailReceiverService {

    @ServiceActivator(inputChannel="emailChannel")
    public void handleMessage(Message message, @Header("emailUrl") String url) {
        System.out.println(message + " header:" + url);
    }

}

…hope that helps.

EDIT 2:

And this one’s a bit more sophisticated; it pulls the from from the payload and puts it in a header; not needed for your use case since you have the full message, but it illustrates the technique…

@Bean
@Transformer(inputChannel="enrichHeadersChannel", outputChannel="emailChannel")
public HeaderEnricher enrichHeaders() {
    Map<String, HeaderValueMessageProcessor<?>> headersToAdd = new HashMap<>();
    headersToAdd.put("emailUrl", new StaticHeaderValueMessageProcessor<String>(this.imapUrl));
    Expression expression = new SpelExpressionParser().parseExpression("payload.from[0].toString()");
    headersToAdd.put("from", new ExpressionEvaluatingHeaderValueMessageProcessor<>(expression, String.class));
    HeaderEnricher enricher = new HeaderEnricher(headersToAdd);
    return enricher;
}

and

@ServiceActivator(inputChannel="emailChannel")
public void handleMessage(Message message, @Header("emailUrl") String url,
                                           @Header("from") String from) {
    System.out.println(message + " header:" + url + " from:" + from);
}

Leave a Comment