Spring: How to configure Messaging Bridge poller w

2019-09-17 02:09发布

问题:

I want to be able to throttle requests received from my SubscribableChannel. I do not use a PollableChannel. Will i be able to do an equivalent of this:

<bridge input-channel="pollable" output-channel="subscribable">
     <poller max-messages-per-poll="10">
         <interval-trigger interval="5" time-unit="SECONDS"/>
     </poller>
 </bridge>

http://docs.spring.io/spring-integration/docs/2.0.0.M4/spring-integration-reference/html/bridge.html

using annotations?

回答1:

With a bridge handler...

@Bean
@ServiceActivator(inputChannel = "polled", 
        poller = @Poller(fixedRate = "5000", maxMessagesPerPoll = "10"))
public BridgeHandler bridge() {
    BridgeHandler bridge = new BridgeHandler();
    bridge.setOutputChannelName("direct");
    return bridge;
}

...or simply...

@Bean
@BridgeTo(value = "direct", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public PollableChannel polled() {
    return new QueueChannel();
}

@Bean
public SubscribableChannel direct() {
    return new DirectChannel();
}

or

@Bean
public PollableChannel polled() {
    return new QueueChannel();
}

@Bean
@BridgeFrom(value = "polled", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public SubscribableChannel direct() {
    return new DirectChannel();
}


回答2:

A little late but here is a solution I found for my use case :

<int-amqp:inbound-channel-adapter id="inbound-channel" prefetch-count="100"  concurrent-consumers="1" connection-factory="connectionFactory" queue-names="${queue.name}" error-channel="error-channel" />

<int:json-to-object-transformer input-channel="inbound-channel" output-channel="queue-channel" />

<int:channel id="queue-channel">
    <int:queue capacity="100" />
</int:channel>

<int:bridge input-channel="queue-channel" output-channel="outbound-channel">
    <int:poller max-messages-per-poll="100" fixed-delay="1000" />
</int:bridge>

<int:outbound-channel-adapter id="outbound-channel" ref="myServiceBean" method="onMessage" />

Explanation :

amqp inbound channel adapter is subscribable. A do the switch to a pollable channel using the "queue style" channel and use a bridge to perform a polling from the queue-channel to the outbound-channel.

In my case I should configure a message-store or set the acknowledge-mode of my amqp inbound channel adapter to manual and do the ack my self to avoid losing my messages.

Regards