user1836542
user1836542

Reputation: 354

Why is ExecutorChannel.onInit() resetting the dispatcher?

I have a simple, working Spring Integration application the moves messages from an inbound RabbitMQ gateway, through a handler chain and into a MongoDB database. When I changed from a direct channel to an executor channel, I started getting subscriber errors. Watching things in the debugger I saw that after I set up the ExecutorChannel bean, the onInit() method gets triggered and resets everything to default values. I cannot figure out why the code would be structured to do this? I looked at DirectChannel.onInit() and it only modifies things if values have not previously been set. Any ideas? I am using Spring Integration 4.1.2.

// from
@Bean
DirectChannel uploadChannel( MessageHandlerChain uploadMessageHandlerChain ) {
    def bean = new DirectChannel()
    bean.subscribe( uploadMessageHandlerChain )
    bean
}

// to
@Bean
ExecutorChannel uploadChannel( MessageHandlerChain uploadMessageHandlerChain ) {
    def bean = new ExecutorChannel( Executors.newCachedThreadPool() )
    bean.subscribe( uploadMessageHandlerChain )
    bean
}

org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers

Upvotes: 1

Views: 119

Answers (1)

Nicolas Labrot
Nicolas Labrot

Reputation: 4116

When you create channels using XML application context, you first define the channel and after you use it in an EIP pattern:

<int:channel id="directChannel"/>
<int:service-activator input-channel="directChannel"/>

This allows in my opinion a better separation of concern. Same pattern must be used with java configuration, first declare your channel then declare MessageHandlerChain and subscribe to the channel

@Bean
ExecutorChannel uploadChannel() {
    def bean = new ExecutorChannel( Executors.newCachedThreadPool() )
    bean
}

@Bean
MessageHandlerChain uploadMessageHandlerChain(){
    def uploadMessageHandlerChain = new MessageHandlerChain()
    uploadChannel().subscribe(uploadMessageHandlerChain)
    uploadMessageHandlerChain
}

Upvotes: 1

Related Questions