user770119
user770119

Reputation: 442

spring integration: Custom header enricher that records header values to database

I would like to have a custom header-enricher that takes the header values to be added and adds them to the header and also, records them in the database. I was trying to create a custom spring tag say: db-recording-header-enricher and use that instead of header-enricher tag wherever I am interested in recording the headers to the database.

And here's what I have so far:

I have custom spring XML name-space with custom element db-recorder-header-enricher correctly configured. I have a test spring integration xml that I am using to test whether the parser is functioning correctly. The test is loading the test XML correctly, except I want to use my custom parser below instead of the HeaderEnricher which it picks up by default as the transformer.

The processor for db-recording-header-enricher looks like:

DbRecorderHeaderEnricherParser implements BeanDefinitionParser {
   @Override
   public BeanDefinition parse(Element element, ParserContext parserContext) {
       BeanDefinition beanDefinition = new StandardHeaderEnricherParser().parse(element, parserContext);
       // Set the header Enricher processor to be my custom processor
       // beanDefinition.setHeaderEnricherProcessor(dbRecordingHeaderEnricher);
       return beanDefinition;
    }
}

The problem I am facing is this:

Based on the parser definition above if I use StandardHeaderEnricherParser to parse my xml, I cannot find a way to associate DbRecordingHeaderEnricher as the transformer for the parsing of the header-enricher. Even if I extend StandardHeaderEnricherParser the method below is final, so again I cannot seem to give it my custom parser for transforming purposes.

@Override
protected final String getTransformerClassName() {
    return HeaderEnricher.class.getName();
}

All I want to do in my custom parser is associate my custom header enricher (which extends HeaderEnricher class) for the parsing of the headers and creating records into the database for the headers added. If it's not possible the way I am thinking about it, what are some of the other alternatives? Can I use AOP/advice on a transformer?

Upvotes: 1

Views: 872

Answers (1)

Gary Russell
Gary Russell

Reputation: 174584

This is fairly advanced. You will need a schema, a namespace handler that associates the parser with the namespace element and the parser itself.

It might be simpler to use a <transformer/> and simply reference your bean that adds the headers (and stores them).

If you want to learn how to write your own namespace; a good place to get started is the STS project templates which will create all of the boiler plate for you.

EDIT:

In response to your updates...

Since it's still a bean definition, and not yet a bean, you can simply change the beanClassName property...

BeanDefinition beanDefinition = new StandardHeaderEnricherParser().parse(element, parserContext);
beanDefinition.setBeanClassName(Foo.class.getName());

Upvotes: 1

Related Questions