Youri
Youri

Reputation: 91

How to handle input data with IntegrationFlowExtension

I want to create create a custom spring integration DSL to avoid boilerplate code in our product. More concrete, we are creating a lot of flows where we receive data from an AMQP0.9 queue, transform it, and publish it to an Exchange.

From the spring integration docs (https://docs.spring.io/spring-integration/reference/dsl/java-extensions.html) I see it is possible by extending IntegrationFlowExtension.

So my intention is to create an integration flow that looks something like this:

@Bean
AmqpIntegrationFlow amqpIntegrationFlow() {
  return new AmqpIntegrationFlow("someQueue")
          .transform(...)
          .publishTo("someExchange") // This is the custom method from the extension
          .get();
}

The idea is that the constructor contains all the boilerplate code like IntegrationFlow.from(Amqp.inboundAdapter(...)).

However, there is no way to call from() from IntegrationFlowExtension. How do I handle input ?

Upvotes: 1

Views: 82

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121552

The question makes sense from some point of view, but it is a big hard to make those IntegrationFlow.from() static factories to be used in the extension class. So, the recommendation (for now) is to use that IntegrationFlow.from() from some custom factory method to encapsulate your Amqp.inboundAdapter() and compose it with your extension flow. The IntegrationFlowExtension starts from the channel, which has a name like beanName + ".input", where beanName is for this your IntegrationFlowExtension bean definition. So, you can finish the first flow with channel(), or use flows composition: https://docs.spring.io/spring-integration/reference/dsl/integration-flows-composition.html.

Another argument, that usually we have some eternal dependencies, like ConnecitonFactory, to be used in those from(). So, having an extension aware of them from its ctor, would complicate the target code a little bit. Relying on flow composition just based on channels in between looks as much cleaner solution, then trying to bring everything to a single extension class.

UPDATE

I think this is one of the possible solution:

public MyIntegrationFlowExtension(IntegrationFlow fromIntegration) { 
     IntegrationFlow composition =  IntegrationFlow.from(fromIntegration)
                                             .channel(getCurrentMessageChannel());
     addComponent(composition);
}

We definitely have to make that composition flow as a bean to trigger all the expected bean registrations and so on.

Upvotes: 0

Related Questions