Search in sources :

Example 11 with MessageDeliveryException

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

the class MethodInvokingRouterTests method doTestMultiChannelNameResolutionByPayload.

private void doTestMultiChannelNameResolutionByPayload(MethodInvokingRouter router, TestChannelResolver channelResolver) {
    QueueChannel fooChannel = new QueueChannel();
    QueueChannel barChannel = new QueueChannel();
    channelResolver.addChannel("foo-channel", fooChannel);
    channelResolver.addChannel("bar-channel", barChannel);
    router.setChannelResolver(channelResolver);
    Message<String> fooMessage = new GenericMessage<String>("foo");
    Message<String> barMessage = new GenericMessage<String>("bar");
    Message<String> badMessage = new GenericMessage<String>("bad");
    router.handleMessage(fooMessage);
    Message<?> result1a = fooChannel.receive(0);
    Message<?> result1b = barChannel.receive(0);
    assertNotNull(result1a);
    assertEquals("foo", result1a.getPayload());
    assertNotNull(result1b);
    assertEquals("foo", result1b.getPayload());
    router.handleMessage(barMessage);
    Message<?> result2a = fooChannel.receive(0);
    Message<?> result2b = barChannel.receive(0);
    assertNotNull(result2a);
    assertEquals("bar", result2a.getPayload());
    assertNotNull(result2b);
    assertEquals("bar", result2b.getPayload());
    try {
        router.handleMessage(badMessage);
        fail();
    } catch (MessageDeliveryException e) {
    /* Success */
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 12 with MessageDeliveryException

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

the class AbstractCorrelatingMessageHandler method forceComplete.

protected void forceComplete(MessageGroup group) {
    Object correlationKey = group.getGroupId();
    // UUIDConverter is no-op if already converted
    Lock lock = this.lockRegistry.obtain(UUIDConverter.getUUID(correlationKey).toString());
    boolean removeGroup = true;
    try {
        lock.lockInterruptibly();
        try {
            ScheduledFuture<?> scheduledFuture = this.expireGroupScheduledFutures.remove(UUIDConverter.getUUID(correlationKey));
            if (scheduledFuture != null) {
                boolean canceled = scheduledFuture.cancel(false);
                if (canceled && this.logger.isDebugEnabled()) {
                    this.logger.debug("Cancel 'forceComplete' scheduling for MessageGroup [ " + group + "].");
                }
            }
            MessageGroup groupNow = group;
            /*
				 * If the group argument is not already complete,
				 * re-fetch it because it might have changed while we were waiting on
				 * its lock. If the last modified timestamp changed, defer the completion
				 * because the selection condition may have changed such that the group
				 * would no longer be eligible. If the timestamp changed, it's a completely new
				 * group and should not be reaped on this cycle.
				 *
				 * If the group argument is already complete, do not re-fetch.
				 * Note: not all message stores provide a direct reference to its internal
				 * group so the initial 'isComplete()` will only return true for those stores if
				 * the group was already complete at the time of its selection as a candidate.
				 *
				 * If the group is marked complete, only consider it
				 * for reaping if it's empty (and both timestamps are unaltered).
				 */
            if (!group.isComplete()) {
                groupNow = this.messageStore.getMessageGroup(correlationKey);
            }
            long lastModifiedNow = groupNow.getLastModified();
            int groupSize = groupNow.size();
            if ((!groupNow.isComplete() || groupSize == 0) && group.getLastModified() == lastModifiedNow && group.getTimestamp() == groupNow.getTimestamp()) {
                if (groupSize > 0) {
                    if (this.releaseStrategy.canRelease(groupNow)) {
                        completeGroup(correlationKey, groupNow);
                    } else {
                        expireGroup(correlationKey, groupNow);
                    }
                    if (!this.expireGroupsUponTimeout) {
                        afterRelease(groupNow, groupNow.getMessages(), true);
                        removeGroup = false;
                    }
                } else {
                    /*
						 * By default empty groups are removed on the same schedule as non-empty
						 * groups. A longer timeout for empty groups can be enabled by
						 * setting minimumTimeoutForEmptyGroups.
						 */
                    removeGroup = lastModifiedNow <= (System.currentTimeMillis() - this.minimumTimeoutForEmptyGroups);
                    if (removeGroup && this.logger.isDebugEnabled()) {
                        this.logger.debug("Removing empty group: " + correlationKey);
                    }
                }
            } else {
                removeGroup = false;
                if (this.logger.isDebugEnabled()) {
                    this.logger.debug("Group expiry candidate (" + correlationKey + ") has changed - it may be reconsidered for a future expiration");
                }
            }
        } catch (MessageDeliveryException e) {
            removeGroup = false;
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Group expiry candidate (" + correlationKey + ") has been affected by MessageDeliveryException - " + "it may be reconsidered for a future expiration one more time");
            }
            throw e;
        } finally {
            try {
                if (removeGroup) {
                    this.remove(group);
                }
            } finally {
                lock.unlock();
            }
        }
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        this.logger.debug("Thread was interrupted while trying to obtain lock");
    }
}
Also used : MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Lock(java.util.concurrent.locks.Lock)

Example 13 with MessageDeliveryException

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

the class UnicastingDispatcherTests method withInboundGatewayAsyncRequestChannelAndExplicitErrorChannel.

@SuppressWarnings("unchecked")
@Test
public void withInboundGatewayAsyncRequestChannelAndExplicitErrorChannel() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("unicasting-with-async.xml", this.getClass());
    SubscribableChannel errorChannel = context.getBean("errorChannel", SubscribableChannel.class);
    MessageHandler errorHandler = message -> {
        MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReplyChannel();
        assertTrue(message.getPayload() instanceof MessageDeliveryException);
        replyChannel.send(new GenericMessage<String>("reply"));
    };
    errorChannel.subscribe(errorHandler);
    RequestReplyExchanger exchanger = context.getBean(RequestReplyExchanger.class);
    Message<String> reply = (Message<String>) exchanger.exchange(new GenericMessage<String>("Hello"));
    assertEquals("reply", reply.getPayload());
    context.close();
}
Also used : MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageHandler(org.springframework.messaging.MessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Assert.assertTrue(org.junit.Assert.assertTrue) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Test(org.junit.Test) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) Assert.assertEquals(org.junit.Assert.assertEquals) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 14 with MessageDeliveryException

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

the class ScatterGatherHandler method doInit.

@Override
protected void doInit() {
    if (this.gatherChannel == null) {
        this.gatherChannel = new FixedSubscriberChannel(this.gatherer);
    } else {
        if (this.gatherChannel instanceof SubscribableChannel) {
            this.gatherEndpoint = new EventDrivenConsumer((SubscribableChannel) this.gatherChannel, this.gatherer);
        } else if (this.gatherChannel instanceof PollableChannel) {
            this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer);
            ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
        } else {
            throw new MessagingException("Unsupported 'replyChannel' type [" + this.gatherChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
        }
        this.gatherEndpoint.setBeanFactory(this.getBeanFactory());
        this.gatherEndpoint.afterPropertiesSet();
    }
    ((MessageProducer) this.gatherer).setOutputChannel(new FixedSubscriberChannel(message -> {
        MessageHeaders headers = message.getHeaders();
        if (headers.containsKey(GATHER_RESULT_CHANNEL)) {
            Object gatherResultChannel = headers.get(GATHER_RESULT_CHANNEL);
            if (gatherResultChannel instanceof MessageChannel) {
                messagingTemplate.send((MessageChannel) gatherResultChannel, message);
            } else if (gatherResultChannel instanceof String) {
                messagingTemplate.send((String) gatherResultChannel, message);
            }
        } else {
            throw new MessageDeliveryException(message, "The 'gatherResultChannel' header is required to delivery gather result.");
        }
    }));
    this.replyChannelRegistry = getBeanFactory().getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, HeaderChannelRegistry.class);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) ClassUtils(org.springframework.util.ClassUtils) AopUtils(org.springframework.aop.support.AopUtils) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) IntegrationContextUtils(org.springframework.integration.context.IntegrationContextUtils) Lifecycle(org.springframework.context.Lifecycle) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageHandler(org.springframework.messaging.MessageHandler) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Assert(org.springframework.util.Assert) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) MessageHeaders(org.springframework.messaging.MessageHeaders) SubscribableChannel(org.springframework.messaging.SubscribableChannel) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 15 with MessageDeliveryException

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

the class DatatypeChannelTests method multipleTypes.

@Test
public void multipleTypes() {
    MessageChannel channel = createChannel(String.class, Integer.class);
    assertTrue(channel.send(new GenericMessage<String>("test1")));
    assertTrue(channel.send(new GenericMessage<Integer>(2)));
    Exception exception = null;
    try {
        channel.send(new GenericMessage<Date>(new Date()));
    } catch (MessageDeliveryException e) {
        exception = e;
    }
    assertNotNull(exception);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)47 Test (org.junit.Test)19 GenericMessage (org.springframework.messaging.support.GenericMessage)17 QueueChannel (org.springframework.integration.channel.QueueChannel)13 MessagingException (org.springframework.messaging.MessagingException)10 MessageChannel (org.springframework.messaging.MessageChannel)8 Message (org.springframework.messaging.Message)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 MessageHandler (org.springframework.messaging.MessageHandler)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 PollableChannel (org.springframework.messaging.PollableChannel)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)3 FileNotFoundException (java.io.FileNotFoundException)2 Date (java.util.Date)2 List (java.util.List)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2