Search in sources :

Example 1 with MessageDispatchingException

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

the class BroadcastingDispatcher method dispatch.

@Override
public boolean dispatch(Message<?> message) {
    int dispatched = 0;
    int sequenceNumber = 1;
    Collection<MessageHandler> handlers = this.getHandlers();
    if (this.requireSubscribers && handlers.size() == 0) {
        throw new MessageDispatchingException(message, "Dispatcher has no subscribers");
    }
    int sequenceSize = handlers.size();
    Message<?> messageToSend = message;
    UUID sequenceId = null;
    if (this.applySequence) {
        sequenceId = message.getHeaders().getId();
    }
    for (MessageHandler handler : handlers) {
        if (this.applySequence) {
            messageToSend = getMessageBuilderFactory().fromMessage(message).pushSequenceDetails(sequenceId, sequenceNumber++, sequenceSize).build();
            if (message instanceof MessageDecorator) {
                messageToSend = ((MessageDecorator) message).decorateMessage(messageToSend);
            }
        }
        if (this.executor != null) {
            Runnable task = createMessageHandlingTask(handler, messageToSend);
            this.executor.execute(task);
            dispatched++;
        } else {
            if (this.invokeHandler(handler, messageToSend)) {
                dispatched++;
            }
        }
    }
    if (dispatched == 0 && this.minSubscribers == 0 && logger.isDebugEnabled()) {
        if (sequenceSize > 0) {
            logger.debug("No subscribers received message, default behavior is ignore");
        } else {
            logger.debug("No subscribers, default behavior is ignore");
        }
    }
    return dispatched >= this.minSubscribers;
}
Also used : MessageDecorator(org.springframework.integration.support.MessageDecorator) MessageHandler(org.springframework.messaging.MessageHandler) MessageHandlingRunnable(org.springframework.messaging.support.MessageHandlingRunnable) UUID(java.util.UUID) MessageDispatchingException(org.springframework.integration.MessageDispatchingException)

Example 2 with MessageDispatchingException

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

the class UnicastingDispatcher method doDispatch.

private boolean doDispatch(Message<?> message) {
    if (tryOptimizedDispatch(message)) {
        return true;
    }
    boolean success = false;
    Iterator<MessageHandler> handlerIterator = this.getHandlerIterator(message);
    if (!handlerIterator.hasNext()) {
        throw new MessageDispatchingException(message, "Dispatcher has no subscribers");
    }
    List<RuntimeException> exceptions = new ArrayList<RuntimeException>();
    while (!success && handlerIterator.hasNext()) {
        MessageHandler handler = handlerIterator.next();
        try {
            handler.handleMessage(message);
            // we have a winner.
            success = true;
        } catch (Exception e) {
            @SuppressWarnings("deprecation") RuntimeException runtimeException = wrapExceptionIfNecessary(message, e);
            exceptions.add(runtimeException);
            this.handleExceptions(exceptions, message, !handlerIterator.hasNext());
        }
    }
    return success;
}
Also used : MessageHandler(org.springframework.messaging.MessageHandler) ArrayList(java.util.ArrayList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageDispatchingException(org.springframework.integration.MessageDispatchingException) MessageDispatchingException(org.springframework.integration.MessageDispatchingException)

Example 3 with MessageDispatchingException

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

the class ChannelAdapterParserTests method targetOnly.

@Test
public void targetOnly() {
    String beanName = "outboundWithImplicitChannel";
    Object channel = this.applicationContext.getBean(beanName);
    assertTrue(channel instanceof DirectChannel);
    BeanFactoryChannelResolver channelResolver = new BeanFactoryChannelResolver(this.applicationContext);
    assertNotNull(channelResolver.resolveDestination(beanName));
    Object adapter = this.applicationContext.getBean(beanName + ".adapter");
    assertNotNull(adapter);
    assertTrue(adapter instanceof EventDrivenConsumer);
    assertFalse(((EventDrivenConsumer) adapter).isAutoStartup());
    assertEquals(-1, ((EventDrivenConsumer) adapter).getPhase());
    TestConsumer consumer = (TestConsumer) this.applicationContext.getBean("consumer");
    assertNull(consumer.getLastMessage());
    Message<?> message = new GenericMessage<String>("test");
    try {
        ((MessageChannel) channel).send(message);
        fail("MessageDispatchingException is expected.");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), Matchers.instanceOf(MessageDispatchingException.class));
    }
    ((EventDrivenConsumer) adapter).start();
    ((MessageChannel) channel).send(message);
    assertNotNull(consumer.getLastMessage());
    assertEquals(message, consumer.getLastMessage());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) MessageDispatchingException(org.springframework.integration.MessageDispatchingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Test(org.junit.Test)

Example 4 with MessageDispatchingException

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

the class IntegrationFlowTests method testDirectFlow.

@Test
public void testDirectFlow() {
    assertTrue(this.beanFactory.containsBean("filter"));
    assertTrue(this.beanFactory.containsBean("filter.handler"));
    assertTrue(this.beanFactory.containsBean("expressionFilter"));
    assertTrue(this.beanFactory.containsBean("expressionFilter.handler"));
    QueueChannel replyChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("100").setReplyChannel(replyChannel).build();
    try {
        this.inputChannel.send(message);
        fail("Expected MessageDispatchingException");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(MessageDispatchingException.class));
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers"));
    }
    this.controlBus.send("@payloadSerializingTransformer.start()");
    final AtomicBoolean used = new AtomicBoolean();
    this.foo.subscribe(m -> used.set(true));
    this.inputChannel.send(message);
    Message<?> reply = replyChannel.receive(5000);
    assertNotNull(reply);
    assertEquals(200, reply.getPayload());
    Message<?> successMessage = this.successChannel.receive(5000);
    assertNotNull(successMessage);
    assertEquals(100, successMessage.getPayload());
    assertTrue(used.get());
    this.inputChannel.send(new GenericMessage<Object>(1000));
    Message<?> discarded = this.discardChannel.receive(5000);
    assertNotNull(discarded);
    assertEquals("Discarded: 1000", discarded.getPayload());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) QueueChannel(org.springframework.integration.channel.QueueChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) MessagingException(org.springframework.messaging.MessagingException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) MessageDispatchingException(org.springframework.integration.MessageDispatchingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Aggregations

MessageDispatchingException (org.springframework.integration.MessageDispatchingException)4 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)3 Test (org.junit.Test)2 MessageHandler (org.springframework.messaging.MessageHandler)2 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 BeanCreationException (org.springframework.beans.factory.BeanCreationException)1 BeanDefinitionParsingException (org.springframework.beans.factory.parsing.BeanDefinitionParsingException)1 MessageRejectedException (org.springframework.integration.MessageRejectedException)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)1 MessageDecorator (org.springframework.integration.support.MessageDecorator)1 BeanFactoryChannelResolver (org.springframework.integration.support.channel.BeanFactoryChannelResolver)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessagingException (org.springframework.messaging.MessagingException)1 DestinationResolutionException (org.springframework.messaging.core.DestinationResolutionException)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1