Search in sources :

Example 6 with UnicastingDispatcher

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

the class ExecutorChannel method onInit.

@Override
public final void onInit() throws Exception {
    Assert.state(getDispatcher().getHandlerCount() == 0, "You cannot subscribe() until the channel " + "bean is fully initialized by the framework. Do not subscribe in a @Bean definition");
    super.onInit();
    if (!(this.executor instanceof ErrorHandlingTaskExecutor)) {
        ErrorHandler errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(this.getBeanFactory()));
        this.executor = new ErrorHandlingTaskExecutor(this.executor, errorHandler);
    }
    UnicastingDispatcher unicastingDispatcher = new UnicastingDispatcher(this.executor);
    unicastingDispatcher.setFailover(this.failover);
    if (this.maxSubscribers == null) {
        this.maxSubscribers = getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_UNICAST_SUBSCRIBERS, Integer.class);
    }
    unicastingDispatcher.setMaxSubscribers(this.maxSubscribers);
    if (this.loadBalancingStrategy != null) {
        unicastingDispatcher.setLoadBalancingStrategy(this.loadBalancingStrategy);
    }
    unicastingDispatcher.setMessageHandlingTaskDecorator(task -> {
        if (ExecutorChannel.this.executorInterceptorsSize > 0) {
            return new MessageHandlingTask(task);
        } else {
            return task;
        }
    });
    this.dispatcher = unicastingDispatcher;
}
Also used : ErrorHandler(org.springframework.util.ErrorHandler) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor)

Example 7 with UnicastingDispatcher

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

the class DirectChannelTests method testSend.

@Test
public void testSend() {
    DirectChannel channel = new DirectChannel();
    Log logger = spy(TestUtils.getPropertyValue(channel, "logger", Log.class));
    when(logger.isDebugEnabled()).thenReturn(true);
    new DirectFieldAccessor(channel).setPropertyValue("logger", logger);
    ThreadNameExtractingTestTarget target = new ThreadNameExtractingTestTarget();
    channel.subscribe(target);
    GenericMessage<String> message = new GenericMessage<String>("test");
    assertTrue(channel.send(message));
    assertEquals(Thread.currentThread().getName(), target.threadName);
    DirectFieldAccessor channelAccessor = new DirectFieldAccessor(channel);
    UnicastingDispatcher dispatcher = (UnicastingDispatcher) channelAccessor.getPropertyValue("dispatcher");
    DirectFieldAccessor dispatcherAccessor = new DirectFieldAccessor(dispatcher);
    Object loadBalancingStrategy = dispatcherAccessor.getPropertyValue("loadBalancingStrategy");
    assertTrue(loadBalancingStrategy instanceof RoundRobinLoadBalancingStrategy);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger, times(2)).debug(captor.capture());
    List<String> logs = captor.getAllValues();
    assertEquals(2, logs.size());
    assertThat(logs.get(0), startsWith("preSend"));
    assertThat(logs.get(1), startsWith("postSend"));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) Test(org.junit.Test)

Example 8 with UnicastingDispatcher

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

the class MixedDispatcherConfigurationScenarioTests method noFailoverNoLoadBalancingConcurrent.

@Test
public void noFailoverNoLoadBalancingConcurrent() throws Exception {
    final 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);
    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(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerB, times(0)).handleMessage(message);
    verify(exceptionRegistry, times(TOTAL_EXECUTIONS)).add(any(Exception.class));
}
Also used : UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 9 with UnicastingDispatcher

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

the class MixedDispatcherConfigurationScenarioTests method noFailoverLoadBalancingWithExecutorConcurrent.

@Test
public void noFailoverLoadBalancingWithExecutorConcurrent() throws Exception {
    final ExecutorChannel channel = (ExecutorChannel) ac.getBean("loadBalancerNoFailoverExecutor");
    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);
    doAnswer(invocation -> {
        failed.set(true);
        RuntimeException e = new RuntimeException();
        exceptionRegistry.add(e);
        allDone.countDown();
        throw e;
    }).when(handlerA).handleMessage(message);
    doAnswer(invocation -> {
        allDone.countDown();
        return null;
    }).when(handlerB).handleMessage(message);
    doAnswer(invocation -> {
        allDone.countDown();
        return null;
    }).when(handlerC).handleMessage(message);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        channel.send(message);
    };
    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) Test(org.junit.Test)

Example 10 with UnicastingDispatcher

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

the class MixedDispatcherConfigurationScenarioTests method failoverNoLoadBalancingWithExecutorConcurrent.

@Test
public void failoverNoLoadBalancingWithExecutorConcurrent() throws Exception {
    final ExecutorChannel channel = (ExecutorChannel) ac.getBean("noLoadBalancerFailoverExecutor");
    final UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    dispatcher.addHandler(handlerC);
    doAnswer(invocation -> {
        RuntimeException e = new RuntimeException();
        failed.set(true);
        throw e;
    }).when(handlerA).handleMessage(message);
    doAnswer(invocation -> {
        allDone.countDown();
        return null;
    }).when(handlerB).handleMessage(message);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        channel.send(message);
    };
    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);
    verify(handlerA, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerB, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerC, never()).handleMessage(message);
}
Also used : UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) Test(org.junit.Test)

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