Search in sources :

Example 1 with RoundRobinLoadBalancingStrategy

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();
}
Also used : RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 2 with RoundRobinLoadBalancingStrategy

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;
}
Also used : RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher)

Example 3 with RoundRobinLoadBalancingStrategy

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);
}
Also used : RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) InOrder(org.mockito.InOrder) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 4 with RoundRobinLoadBalancingStrategy

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();
}
Also used : CustomizableThreadFactory(org.springframework.scheduling.concurrent.CustomizableThreadFactory) RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConcurrentTaskExecutor(org.springframework.scheduling.concurrent.ConcurrentTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with RoundRobinLoadBalancingStrategy

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();
}
Also used : CustomizableThreadFactory(org.springframework.scheduling.concurrent.CustomizableThreadFactory) RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConcurrentTaskExecutor(org.springframework.scheduling.concurrent.ConcurrentTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

RoundRobinLoadBalancingStrategy (org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy)8 Test (org.junit.Test)6 UnicastingDispatcher (org.springframework.integration.dispatcher.UnicastingDispatcher)4 CountDownLatch (java.util.concurrent.CountDownLatch)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 ConcurrentTaskExecutor (org.springframework.scheduling.concurrent.ConcurrentTaskExecutor)2 CustomizableThreadFactory (org.springframework.scheduling.concurrent.CustomizableThreadFactory)2 Log (org.apache.commons.logging.Log)1 InOrder (org.mockito.InOrder)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 MessageRejectedException (org.springframework.integration.MessageRejectedException)1 BroadcastingDispatcher (org.springframework.integration.dispatcher.BroadcastingDispatcher)1 MessageChannel (org.springframework.messaging.MessageChannel)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1