INT-711 Created LoadBalancingStrategy and improved encapsulation for the unicasting dispatcher (now contains the TaskExecutor LB strategy and a failover boolean flag).

This commit is contained in:
Mark Fisher
2009-07-11 05:49:34 +00:00
parent 2cd12df50e
commit aeb8c7e3a9
13 changed files with 239 additions and 154 deletions

View File

@@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -39,8 +40,8 @@ import org.springframework.integration.config.TestChannelInterceptor;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.core.MessagePriority;
import org.springframework.integration.dispatcher.FailOverDispatcher;
import org.springframework.integration.dispatcher.RoundRobinDispatcher;
import org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy;
import org.springframework.integration.dispatcher.UnicastingDispatcher;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.MessageDeliveryException;
@@ -79,17 +80,22 @@ public class ChannelParserTests {
MessageChannel channel = (MessageChannel) context.getBean("defaultChannel");
assertEquals(DirectChannel.class, channel.getClass());
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
assertThat(accessor.getPropertyValue("dispatcher"), is(RoundRobinDispatcher.class));
Object dispatcher = accessor.getPropertyValue("dispatcher");
assertThat(dispatcher, is(UnicastingDispatcher.class));
assertThat(new DirectFieldAccessor(dispatcher).getPropertyValue("loadBalancingStrategy"),
is(RoundRobinLoadBalancingStrategy.class));
}
@Test
public void channelWithRoundRobinDispatcher() throws Exception {
public void channelWithFailoverDispatcherAttribute() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("channelParserTests.xml", this
.getClass());
MessageChannel channel = (MessageChannel) context.getBean("failOverChannel");
MessageChannel channel = (MessageChannel) context.getBean("channelWithFailoverAttribute");
assertEquals(DirectChannel.class, channel.getClass());
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
assertThat(accessor.getPropertyValue("dispatcher"), is(FailOverDispatcher.class));
Object dispatcher = accessor.getPropertyValue("dispatcher");
assertThat(dispatcher, is(UnicastingDispatcher.class));
assertNull(new DirectFieldAccessor(dispatcher).getPropertyValue("loadBalancingStrategy"));
}
@Test

View File

@@ -12,7 +12,7 @@
<channel id="defaultChannel" />
<channel id="failOverChannel" dispatcher="failover"/>
<channel id="channelWithFailoverAttribute" dispatcher="failover"/>
<channel id="channelWithCustomQueue">
<queue ref="customQueue"/>

View File

@@ -31,7 +31,7 @@ import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.dispatcher.FailOverDispatcher;
import org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy;
import org.springframework.integration.message.MessageRejectedException;
import org.springframework.integration.message.StringMessage;
@@ -45,7 +45,6 @@ public class SubscriberOrderTests {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("postProcessor", new RootBeanDefinition(MessagingAnnotationPostProcessor.class));
RootBeanDefinition channelDefinition = new RootBeanDefinition(DirectChannel.class);
channelDefinition.getConstructorArgumentValues().addGenericArgumentValue(new FailOverDispatcher());
context.registerBeanDefinition("input", channelDefinition);
RootBeanDefinition testBeanDefinition = new RootBeanDefinition(TestBean.class);
testBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(1);
@@ -72,7 +71,6 @@ public class SubscriberOrderTests {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("postProcessor", new RootBeanDefinition(MessagingAnnotationPostProcessor.class));
RootBeanDefinition channelDefinition = new RootBeanDefinition(DirectChannel.class);
channelDefinition.getConstructorArgumentValues().addGenericArgumentValue(new FailOverDispatcher());
context.registerBeanDefinition("input", channelDefinition);
RootBeanDefinition testBeanDefinition = new RootBeanDefinition(TestBean.class);
testBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(2);
@@ -112,6 +110,7 @@ public class SubscriberOrderTests {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("postProcessor", new RootBeanDefinition(MessagingAnnotationPostProcessor.class));
RootBeanDefinition channelDefinition = new RootBeanDefinition(DirectChannel.class);
channelDefinition.getConstructorArgumentValues().addGenericArgumentValue(new RoundRobinLoadBalancingStrategy());
context.registerBeanDefinition("input", channelDefinition);
RootBeanDefinition testBeanDefinition = new RootBeanDefinition(TestBean.class);
testBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(1000);

View File

@@ -41,7 +41,7 @@ public class FailOverDispatcherTests {
@Test
public void singleMessage() throws InterruptedException {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final CountDownLatch latch = new CountDownLatch(1);
dispatcher.addHandler(createConsumer(TestHandlers.countDownHandler(latch)));
dispatcher.dispatch(new StringMessage("test"));
@@ -51,7 +51,7 @@ public class FailOverDispatcherTests {
@Test
public void pointToPoint() throws InterruptedException {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
@@ -65,7 +65,7 @@ public class FailOverDispatcherTests {
@Test
public void noDuplicateSubscriptions() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target = new CountingTestEndpoint(counter, false);
dispatcher.addHandler(target);
@@ -81,7 +81,7 @@ public class FailOverDispatcherTests {
@Test
public void removeConsumerBeforeSend() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target1 = new CountingTestEndpoint(counter, false);
MessageHandler target2 = new CountingTestEndpoint(counter, false);
@@ -101,7 +101,7 @@ public class FailOverDispatcherTests {
@Test
public void removeConsumerBetweenSends() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target1 = new CountingTestEndpoint(counter, false);
MessageHandler target2 = new CountingTestEndpoint(counter, false);
@@ -136,7 +136,7 @@ public class FailOverDispatcherTests {
@Test(expected = MessageDeliveryException.class)
public void removeConsumerLastTargetCausesDeliveryException() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target = new CountingTestEndpoint(counter, false);
dispatcher.addHandler(target);
@@ -153,7 +153,7 @@ public class FailOverDispatcherTests {
@Test
public void firstHandlerReturnsTrue() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target1 = new CountingTestEndpoint(counter, true);
MessageHandler target2 = new CountingTestEndpoint(counter, false);
@@ -167,7 +167,7 @@ public class FailOverDispatcherTests {
@Test
public void middleHandlerReturnsTrue() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target1 = new CountingTestEndpoint(counter, false);
MessageHandler target2 = new CountingTestEndpoint(counter, true);
@@ -181,7 +181,7 @@ public class FailOverDispatcherTests {
@Test
public void allHandlersReturnFalse() {
FailOverDispatcher dispatcher = new FailOverDispatcher();
UnicastingDispatcher dispatcher = new UnicastingDispatcher();
final AtomicInteger counter = new AtomicInteger();
MessageHandler target1 = new CountingTestEndpoint(counter, false);
MessageHandler target2 = new CountingTestEndpoint(counter, false);

View File

@@ -46,7 +46,7 @@ public class RoundRobinDispatcherConcurrentTests {
private static final int TOTAL_EXECUTIONS = 40;
private RoundRobinDispatcher dispatcher = new RoundRobinDispatcher();
private UnicastingDispatcher dispatcher = new UnicastingDispatcher();
private ThreadPoolTaskExecutor scheduler = new ThreadPoolTaskExecutor();
@@ -67,6 +67,7 @@ public class RoundRobinDispatcherConcurrentTests {
@Before
public void initialize() throws Exception {
dispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
scheduler.setCorePoolSize(10);
scheduler.setMaxPoolSize(10);
scheduler.initialize();

View File

@@ -15,8 +15,10 @@
package org.springframework.integration.dispatcher;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -32,7 +34,7 @@ import org.springframework.integration.message.MessageHandler;
@RunWith(MockitoJUnit44Runner.class)
public class RoundRobinDispatcherTests {
private AbstractUnicastDispatcher dispatcher = new RoundRobinDispatcher();
private UnicastingDispatcher dispatcher = new UnicastingDispatcher();
@Mock
private MessageHandler handler;
@@ -43,6 +45,12 @@ public class RoundRobinDispatcherTests {
@Mock
private MessageHandler differentHandler;
@Before
public void setupDispatcher() {
this.dispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
}
@Test
public void dispatchMessageWithSingleHandler() throws Exception {
dispatcher.addHandler(handler);