use of org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy in project spring-integration by spring-projects.
the class SubscriberOrderTests method directChannelAndRoundRobinDispatcher.
@Test
public void directChannelAndRoundRobinDispatcher() {
GenericApplicationContext context = TestUtils.createTestApplicationContext();
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);
context.registerBeanDefinition("testBean", testBeanDefinition);
context.refresh();
TestBean testBean = (TestBean) context.getBean("testBean");
MessageChannel channel = (MessageChannel) context.getBean("input");
channel.send(new GenericMessage<String>("test-1"));
channel.send(new GenericMessage<String>("test-2"));
channel.send(new GenericMessage<String>("test-3"));
channel.send(new GenericMessage<String>("test-4"));
channel.send(new GenericMessage<String>("test-5"));
List<Integer> calls = testBean.calls;
assertEquals(5, calls.size());
assertEquals(1, calls.get(0).intValue());
assertEquals(2, calls.get(1).intValue());
assertEquals(3, calls.get(2).intValue());
assertEquals(4, calls.get(3).intValue());
assertEquals(5, calls.get(4).intValue());
context.close();
}
use of org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy in project spring-integration by spring-projects.
the class PointToPointSubscribableAmqpChannel method createDispatcher.
@Override
protected AbstractDispatcher createDispatcher() {
UnicastingDispatcher unicastingDispatcher = new UnicastingDispatcher();
unicastingDispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
return unicastingDispatcher;
}
use of org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy in project spring-integration by spring-projects.
the class MixedDispatcherConfigurationScenarioTests method noFailoverLoadBalancing.
@Test
public void noFailoverLoadBalancing() {
DirectChannel channel = (DirectChannel) ac.getBean("loadBalancerNoFailover");
doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
UnicastingDispatcher dispatcher = channel.getDispatcher();
dispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
dispatcher.addHandler(handlerA);
dispatcher.addHandler(handlerB);
dispatcher.addHandler(handlerC);
InOrder inOrder = inOrder(handlerA, handlerB, handlerC);
try {
channel.send(message);
} catch (Exception e) {
/* ignore */
}
inOrder.verify(handlerA).handleMessage(message);
try {
channel.send(message);
} catch (Exception e) {
/* ignore */
}
inOrder.verify(handlerB).handleMessage(message);
try {
channel.send(message);
} catch (Exception e) {
/* ignore */
}
inOrder.verify(handlerC).handleMessage(message);
verify(handlerA, times(1)).handleMessage(message);
verify(handlerB, times(1)).handleMessage(message);
verify(handlerC, times(1)).handleMessage(message);
}
use of org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy in project spring-integration by spring-projects.
the class ExecutorChannelTests method verifyFailoverWithLoadBalancing.
@Test
public void verifyFailoverWithLoadBalancing() throws Exception {
int numberOfMessages = 11;
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(new CustomizableThreadFactory("test-"));
ConcurrentTaskExecutor taskExecutor = new ConcurrentTaskExecutor(exec);
ExecutorChannel channel = new ExecutorChannel(taskExecutor, new RoundRobinLoadBalancingStrategy());
CountDownLatch latch = new CountDownLatch(numberOfMessages);
TestHandler handler1 = new TestHandler(latch);
TestHandler handler2 = new TestHandler(latch);
TestHandler handler3 = new TestHandler(latch);
channel.subscribe(handler1);
channel.subscribe(handler2);
channel.subscribe(handler3);
handler2.shouldFail = true;
for (int i = 0; i < numberOfMessages; i++) {
channel.send(new GenericMessage<String>("test-" + i));
}
latch.await(3000, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
assertNotNull(handler1.thread);
assertFalse(Thread.currentThread().equals(handler1.thread));
assertTrue(handler1.thread.getName().startsWith("test-"));
assertNotNull(handler2.thread);
assertFalse(Thread.currentThread().equals(handler2.thread));
assertTrue(handler2.thread.getName().startsWith("test-"));
assertNotNull(handler3.thread);
assertFalse(Thread.currentThread().equals(handler3.thread));
assertTrue(handler3.thread.getName().startsWith("test-"));
assertEquals(0, handler2.count.get());
assertEquals(4, handler1.count.get());
assertEquals(7, handler3.count.get());
exec.shutdownNow();
}
use of org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy in project spring-integration by spring-projects.
the class ExecutorChannelTests method roundRobinLoadBalancing.
@Test
public void roundRobinLoadBalancing() throws Exception {
int numberOfMessages = 11;
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(new CustomizableThreadFactory("test-"));
ConcurrentTaskExecutor taskExecutor = new ConcurrentTaskExecutor(exec);
ExecutorChannel channel = new ExecutorChannel(taskExecutor, new RoundRobinLoadBalancingStrategy());
CountDownLatch latch = new CountDownLatch(numberOfMessages);
TestHandler handler1 = new TestHandler(latch);
TestHandler handler2 = new TestHandler(latch);
TestHandler handler3 = new TestHandler(latch);
channel.subscribe(handler1);
channel.subscribe(handler2);
channel.subscribe(handler3);
for (int i = 0; i < numberOfMessages; i++) {
channel.send(new GenericMessage<String>("test-" + i));
}
latch.await(3000, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
assertNotNull(handler1.thread);
assertFalse(Thread.currentThread().equals(handler1.thread));
assertTrue(handler1.thread.getName().startsWith("test-"));
assertNotNull(handler2.thread);
assertFalse(Thread.currentThread().equals(handler2.thread));
assertTrue(handler2.thread.getName().startsWith("test-"));
assertNotNull(handler3.thread);
assertFalse(Thread.currentThread().equals(handler3.thread));
assertTrue(handler3.thread.getName().startsWith("test-"));
assertEquals(4, handler1.count.get());
assertEquals(4, handler2.count.get());
assertEquals(3, handler3.count.get());
exec.shutdownNow();
}
Aggregations