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.

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

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?
HeaderEnricher has setMessageProcessor so you can wrap your bean invocation from that interface implementation, e.g. MethodInvokingMessageProcessor
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..
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.
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()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.