Spring Integration jdbc:inbound-channel-adapter - Set max-rows-per-poll dynamic to throttle

前端 未结 1 1491
轻奢々
轻奢々 2021-01-28 01:39

I have a JDBC:inbound-channel-adapter : To set the \'max-rows-per-poll\' dynamic to throttle the messages getting passed on the channel.

I have a QueueChannel which has

相关标签:
1条回答
  • 2021-01-28 01:57

    Well, let's try to investigate!

    1. max-rows-per-poll is a volatile property of JdbcPollingChannelAdapter with appropriate setter.
    2. As far as JdbcPollingChannelAdapter does the stuff within its receive() method just on the initiative of TaskScheduler.schedule() looks like changing that property at runtime is safe. It is a first point for our task
    3. QueueChannel has property getQueueSize(). As far as a capacity is your configuration option, so you can simply calculate a value for max-rows-per-poll
    4. And now how to get it worked? Actually you are interested in the value for max-rows-per-poll just on each poll. So, we should somehow to wedge into poller or polling task. Well, <poller> has advice-chain sub-element and we can write some Advice, which should change JdbcPollingChannelAdapter#setMaxRowsPerPoll before invoking receive() and the value should be based on QueueChannel#getQueueSize()
    5. Inject your QueueChannel to the bean of your Advice
    6. And now some bad point: how to inject JdbcPollingChannelAdapter bean? We provide a hook to register MessageSources as beans just only since Spring Integration 3.0. From here it's just enough to write this code:

      @Autowired @Qualifier("jdbcAdapter.source") private JdbcPollingChannelAdapter messageSource;

    We are going to release 3.0.GA this week. So, let me do not consider the reflection 'forest' prior to Sring Integration 3.0. However you can do it using DirectFieldAccessor on injected SourcePollingChannelAdapter bean.

    UPDATE

    Your Advice may look like this:

    public class MyAdvice implements MethodInterceptor {
           @Autowired
           QueueChannel queueChannel;
    
           @Autowired
           SourcePollingChannelAdapter jdbcInboundAdapter; 
    
          Object invoke(MethodInvocation invocation) throws Throwable {
                DirectFieldAccessor dfa = new DirectFieldAccessor(jdbcInboundAdapter);
                JdbcPollingChannelAdapter source = (JdbcPollingChannelAdapter) dfa.getPropertyValue("source");
                source.setMaxRowsPerPoll(queueChannel.getRemainingCapacity());
                return invocation.proceed();
          }
    }
    

    The theory is here: http://docs.spring.io/spring/docs/3.2.5.RELEASE/spring-framework-reference/htmlsingle/#aop

    0 讨论(0)
提交回复
热议问题