2
votes

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.

2

2 Answers

4
votes

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;
}
0
votes

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