2

What's Java config equivalent to following header enricher:-

<!-- Business Entity Header Enricher -->
<int:header-enricher 
    id="businessEntityHeaderEnricherComponent"
    should-skip-nulls="false" 
    output-channel="notificationPreferencesInputChannel"
    input-channel="newUserCreatedChannel">

    <!-- Tenant -->
    <int:header name="tenant" 
        <!-- !! HEADER ENRICHMENT ID DONE BY SPRING MANAGED BEAN !! -->
        ref="businessEntityPayloadHeaderEnricher"
        method="extractTenant" />       


</int:header-enricher>

I have a Spring managed @Bean whose method (that return a Map) should take care of enriching the Message header.

I understand that I can also use spring-integration-dsl but as of now I need to stick to Java config.

For example, this is how I am using Java config to define a Service Activator:-

    @Bean
    @ServiceActivator(requiresReply = "false", inputChannel = "lifeCycleRouterChannel")
    public InvoiceDelinquencyServiceActivator serviceActivator() {
        return new InvoiceDelinquencyServiceActivator();
    }

What's the equivalent way to define Header Enricher ? Couldn't find any example/reference.

Thanks.

Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86

2 Answers2

4

HeaderEnricher implements Transformer, so you can do something like this:

@Bean
@Transformer(inputChannel = "enrichChannel", outputChannel = "processChannel")
public HeaderEnricher headerEnricher() {
    HeaderEnricher headerEnricher = new HeaderEnricher (...);
    ....
    return headerEnricher;
}
Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • 1
    How does the Header enriching bean (businessEntityPayloadHeaderEnricher) in the example comes into picture here. How I tell the HeaderEnricher to use method in my bean? – Kumar Sambhav Apr 24 '15 at 14:40
  • `HeaderEnricher` has `setMessageProcessor` so you can wrap your bean invocation from that interface implementation, e.g. `MethodInvokingMessageProcessor` – Artem Bilan Apr 24 '15 at 15:38
  • That worked. But now I decided to go back to XML configuration as Java config was making my business code coupled with Spring Integration's classes - may be just my opinion.. – Kumar Sambhav Apr 27 '15 at 10:22
  • Maybe that is an issue if your design... You can have several `@Configuration` classes to decouple. For example `ExpressionEvaluatingMessageProcessor` for `HeaderEnricher` `@Bean` allows you do not tie the Integration `@Configuration` with your business components `@Configuration`. – Artem Bilan Apr 27 '15 at 10:56
0

I had a similar need and below Groovy code helped me add header using bean/method invocation.

@Bean
public HeaderEnricher authTokenHeaderEnricher() {
    new HeaderEnricher(["AUTH_TOKEN":
                                new MessageProcessingHeaderValueMessageProcessor(
                                        new BeanNameMessageProcessor<Object>('authTokenProvider', 'fetchAuthToken')
                                )
                ]
    )
}

@Bean
IntegrationFlow readyForDispatchFlow() {
    IntegrationFlows
            .from("inputChannel")
            .transform(authTokenHeaderEnricher())
            .channel("outputChannel")
            .get()
}
suman j
  • 6,710
  • 11
  • 58
  • 109