Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor Spring Integration XML to annotation support?

I'm trying to refactor existing xml of Spring Integration to the new 4.0.0. annotations.

<!-- the service activator is bound to the tcp input gateways error channel -->
<ip:tcp-inbound-gateway error-channel="errorChannel" />
<int:service-activator input-channel="errorChannel" ref="myService" />

But how can I bind the service activator to the error channel as it was in xml?

@Configuration
@EnableIntegration
public class Config {

    @Bean
    public TcpInboundGateway gate() {
        TcpInboundGateway gateway = new TcpInboundGateway();

        //??? how can I bind the service activator class as it was in xml?
        gateway.setErrorChannel(MessageChannel);
        return gateway;
    }
}


@Service
public class MyService {

    @ServiceActivator(inputChannel = "errorChannel")
    public String send(String data) {
        //business logic
    }
}
like image 571
membersound Avatar asked Apr 16 '14 10:04

membersound


1 Answers

Well, since I am an author of those new Java and Annotation config features I can help you.

But you should be ready that it isn't so easy to do. To get rid of xml at all you might go with our another new stuff - Java DSL.

I geass we will have several steps to make it worked.

gateway.setErrorChannel(MessageChannel); and @ServiceActivator(inputChannel = "errorChannel"). You must declare errorChannel as a bean:

@Bean
public MessageChannel errorChannel() {
   return new DirectChannel();
}

And use it from that TCP Gateway:

gateway.setErrorChannel(this.errorChannel());

Or, if you rely on the default errorChannel from Framework, you should @Autowired it to that Config.

Next step. I don't see @ComponentScan. That means that your @ServiceActivator might not be visible for Application context.

You should provide more options for TcpInboundGateway anyway: connectionFactory, requestChannel, replyChannel etc. Everything must be Spring beans.

For a start it is enough. Hope it is clear and makes sense at all for you.

like image 120
Artem Bilan Avatar answered Nov 16 '22 21:11

Artem Bilan