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;
}
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;
}
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());
}
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());
}
Aggregations