Search in sources :

Example 11 with UnicastingDispatcher

use of org.springframework.integration.dispatcher.UnicastingDispatcher in project spring-integration by spring-projects.

the class MixedDispatcherConfigurationScenarioTests method failoverNoLoadBalancingConcurrent.

@Test
public void failoverNoLoadBalancingConcurrent() throws Exception {
    final DirectChannel channel = (DirectChannel) ac.getBean("noLoadBalancerFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    dispatcher.addHandler(handlerC);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch allDone = new CountDownLatch(TOTAL_EXECUTIONS);
    final Message<?> message = this.message;
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        boolean sent = false;
        try {
            sent = channel.send(message);
        } catch (Exception e2) {
            exceptionRegistry.add(e2);
        }
        if (!sent) {
            failed.set(true);
        }
        allDone.countDown();
    };
    for (int i = 0; i < TOTAL_EXECUTIONS; i++) {
        executor.execute(messageSenderTask);
    }
    start.countDown();
    assertTrue(allDone.await(10, TimeUnit.SECONDS));
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertFalse("not all messages were accepted", failed.get());
    verify(handlerA, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerB, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerC, never()).handleMessage(message);
    verify(exceptionRegistry, never()).add(any(Exception.class));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 12 with UnicastingDispatcher

use of org.springframework.integration.dispatcher.UnicastingDispatcher in project spring-integration by spring-projects.

the class MixedDispatcherConfigurationScenarioTests method noFailoverLoadBalancingConcurrent.

@Test
public void noFailoverLoadBalancingConcurrent() throws Exception {
    final DirectChannel channel = (DirectChannel) ac.getBean("loadBalancerNoFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    dispatcher.addHandler(handlerC);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch allDone = new CountDownLatch(TOTAL_EXECUTIONS);
    final Message<?> message = this.message;
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        boolean sent = false;
        try {
            sent = channel.send(message);
        } catch (Exception e2) {
            exceptionRegistry.add(e2);
        }
        if (!sent) {
            failed.set(true);
        }
        allDone.countDown();
    };
    for (int i = 0; i < TOTAL_EXECUTIONS; i++) {
        executor.execute(messageSenderTask);
    }
    start.countDown();
    assertTrue(allDone.await(10, TimeUnit.SECONDS));
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertTrue("not all messages were accepted", failed.get());
    verify(handlerA, times(14)).handleMessage(message);
    verify(handlerB, times(13)).handleMessage(message);
    verify(handlerC, times(13)).handleMessage(message);
    verify(exceptionRegistry, times(14)).add(any(Exception.class));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 13 with UnicastingDispatcher

use of org.springframework.integration.dispatcher.UnicastingDispatcher in project spring-integration by spring-projects.

the class MixedDispatcherConfigurationScenarioTests method noFailoverNoLoadBalancing.

@Test
public void noFailoverNoLoadBalancing() {
    DirectChannel channel = (DirectChannel) ac.getBean("noLoadBalancerNoFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    verify(handlerA, times(2)).handleMessage(message);
    verify(handlerB, times(0)).handleMessage(message);
}
Also used : UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 14 with UnicastingDispatcher

use of org.springframework.integration.dispatcher.UnicastingDispatcher in project spring-integration by spring-projects.

the class SubscribableJmsChannel method configureDispatcher.

private void configureDispatcher(boolean isPubSub) {
    if (isPubSub) {
        BroadcastingDispatcher broadcastingDispatcher = new BroadcastingDispatcher(true);
        broadcastingDispatcher.setBeanFactory(this.getBeanFactory());
        this.dispatcher = broadcastingDispatcher;
    } else {
        UnicastingDispatcher unicastingDispatcher = new UnicastingDispatcher();
        unicastingDispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
        this.dispatcher = unicastingDispatcher;
    }
    if (this.maxSubscribers == null) {
        this.maxSubscribers = this.getIntegrationProperty(isPubSub ? IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS : IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
    }
    this.dispatcher.setMaxSubscribers(this.maxSubscribers);
}
Also used : RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) BroadcastingDispatcher(org.springframework.integration.dispatcher.BroadcastingDispatcher)

Aggregations

UnicastingDispatcher (org.springframework.integration.dispatcher.UnicastingDispatcher)14 Test (org.junit.Test)11 MessageRejectedException (org.springframework.integration.MessageRejectedException)8 RoundRobinLoadBalancingStrategy (org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 InOrder (org.mockito.InOrder)2 Set (java.util.Set)1 Log (org.apache.commons.logging.Log)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 BroadcastingDispatcher (org.springframework.integration.dispatcher.BroadcastingDispatcher)1 BeanFactoryChannelResolver (org.springframework.integration.support.channel.BeanFactoryChannelResolver)1 ErrorHandlingTaskExecutor (org.springframework.integration.util.ErrorHandlingTaskExecutor)1 AbstractXmppConnectionAwareMessageHandler (org.springframework.integration.xmpp.core.AbstractXmppConnectionAwareMessageHandler)1 MessageHandler (org.springframework.messaging.MessageHandler)1 SubscribableChannel (org.springframework.messaging.SubscribableChannel)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1 ErrorHandler (org.springframework.util.ErrorHandler)1