Search in sources :

Example 1 with CustomizableThreadFactory

use of org.springframework.scheduling.concurrent.CustomizableThreadFactory 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 2 with CustomizableThreadFactory

use of org.springframework.scheduling.concurrent.CustomizableThreadFactory 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)

Example 3 with CustomizableThreadFactory

use of org.springframework.scheduling.concurrent.CustomizableThreadFactory in project spring-integration by spring-projects.

the class UnicastSendingMessageHandler method doStart.

@Override
public void doStart() {
    if (this.acknowledge) {
        if (this.taskExecutor == null) {
            CustomizableThreadFactory threadFactory = new CustomizableThreadFactory("UDP-Ack-Handler-");
            threadFactory.setDaemon(true);
            this.taskExecutor = Executors.newSingleThreadExecutor(threadFactory);
        }
        startAckThread();
    }
}
Also used : CustomizableThreadFactory(org.springframework.scheduling.concurrent.CustomizableThreadFactory)

Example 4 with CustomizableThreadFactory

use of org.springframework.scheduling.concurrent.CustomizableThreadFactory in project spring-integration by spring-projects.

the class ExecutorChannelTests method verifyFailoverWithoutLoadBalancing.

@Test
public void verifyFailoverWithoutLoadBalancing() throws Exception {
    int numberOfMessages = 11;
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(new CustomizableThreadFactory("test-"));
    ConcurrentTaskExecutor taskExecutor = new ConcurrentTaskExecutor(exec);
    ExecutorChannel channel = new ExecutorChannel(taskExecutor, null);
    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);
    handler1.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-"));
    assertNull(handler3.thread);
    assertEquals(0, handler1.count.get());
    assertEquals(0, handler3.count.get());
    assertEquals(numberOfMessages, handler2.count.get());
    exec.shutdownNow();
}
Also used : CustomizableThreadFactory(org.springframework.scheduling.concurrent.CustomizableThreadFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConcurrentTaskExecutor(org.springframework.scheduling.concurrent.ConcurrentTaskExecutor) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

CustomizableThreadFactory (org.springframework.scheduling.concurrent.CustomizableThreadFactory)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Test (org.junit.Test)3 ConcurrentTaskExecutor (org.springframework.scheduling.concurrent.ConcurrentTaskExecutor)3 RoundRobinLoadBalancingStrategy (org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy)2