Search in sources :

Example 26 with MessageDeliveryException

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

the class DispatcherHasNoSubscribersTests method testPtP.

@SuppressWarnings("unchecked")
@Test
public void testPtP() throws Exception {
    final Channel channel = mock(Channel.class);
    DeclareOk declareOk = mock(DeclareOk.class);
    when(declareOk.getQueue()).thenReturn("noSubscribersChannel");
    when(channel.queueDeclare(anyString(), anyBoolean(), anyBoolean(), anyBoolean(), isNull())).thenReturn(declareOk);
    Connection connection = mock(Connection.class);
    doAnswer(invocation -> channel).when(connection).createChannel(anyBoolean());
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.createConnection()).thenReturn(connection);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    AmqpTemplate amqpTemplate = mock(AmqpTemplate.class);
    PointToPointSubscribableAmqpChannel amqpChannel = new PointToPointSubscribableAmqpChannel("noSubscribersChannel", container, amqpTemplate);
    amqpChannel.setBeanName("noSubscribersChannel");
    amqpChannel.setBeanFactory(mock(BeanFactory.class));
    amqpChannel.afterPropertiesSet();
    MessageListener listener = (MessageListener) container.getMessageListener();
    try {
        listener.onMessage(new Message("Hello world!".getBytes(), null));
        fail("Exception expected");
    } catch (MessageDeliveryException e) {
        assertThat(e.getMessage(), containsString("Dispatcher has no subscribers for amqp-channel 'noSubscribersChannel'."));
    }
}
Also used : DeclareOk(com.rabbitmq.client.AMQP.Queue.DeclareOk) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) Message(org.springframework.amqp.core.Message) Channel(com.rabbitmq.client.Channel) Connection(org.springframework.amqp.rabbit.connection.Connection) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) BeanFactory(org.springframework.beans.factory.BeanFactory) MessageListener(org.springframework.amqp.core.MessageListener) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) AmqpTemplate(org.springframework.amqp.core.AmqpTemplate) Test(org.junit.Test)

Example 27 with MessageDeliveryException

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

the class AbstractCorrelatingMessageHandler method scheduleGroupToForceComplete.

private void scheduleGroupToForceComplete(MessageGroup messageGroup) {
    final Long groupTimeout = obtainGroupTimeout(messageGroup);
    /*
		 * When 'groupTimeout' is evaluated to 'null' we do nothing.
		 * The 'MessageGroupStoreReaper' can be used to 'forceComplete' message groups.
		 */
    if (groupTimeout != null && groupTimeout >= 0) {
        if (groupTimeout > 0) {
            final Object groupId = messageGroup.getGroupId();
            ScheduledFuture<?> scheduledFuture = getTaskScheduler().schedule(() -> {
                try {
                    processForceRelease(groupId);
                } catch (MessageDeliveryException e) {
                    if (AbstractCorrelatingMessageHandler.this.logger.isWarnEnabled()) {
                        AbstractCorrelatingMessageHandler.this.logger.warn("The MessageGroup [" + groupId + "] is rescheduled by the reason of:", e);
                    }
                    scheduleGroupToForceComplete(groupId);
                }
            }, new Date(System.currentTimeMillis() + groupTimeout));
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Schedule MessageGroup [ " + messageGroup + "] to 'forceComplete'.");
            }
            this.expireGroupScheduledFutures.put(UUIDConverter.getUUID(groupId), scheduledFuture);
        } else {
            this.forceReleaseProcessor.processMessageGroup(messageGroup);
        }
    }
}
Also used : MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Date(java.util.Date)

Example 28 with MessageDeliveryException

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

the class AbstractMessageRouter method handleMessageInternal.

@Override
protected void handleMessageInternal(Message<?> message) {
    boolean sent = false;
    Collection<MessageChannel> results = this.determineTargetChannels(message);
    if (results != null) {
        int sequenceSize = results.size();
        int sequenceNumber = 1;
        for (MessageChannel channel : results) {
            final Message<?> messageToSend = !this.applySequence ? message : (this.getMessageBuilderFactory().fromMessage(message).pushSequenceDetails(message.getHeaders().getId(), sequenceNumber++, sequenceSize).build());
            if (channel != null) {
                try {
                    this.messagingTemplate.send(channel, messageToSend);
                    sent = true;
                } catch (MessagingException e) {
                    if (!this.ignoreSendFailures) {
                        throw e;
                    } else if (this.logger.isDebugEnabled()) {
                        this.logger.debug(e);
                    }
                }
            }
        }
    }
    if (!sent) {
        getDefaultOutputChannel();
        if (this.defaultOutputChannel != null) {
            this.messagingTemplate.send(this.defaultOutputChannel, message);
        } else {
            throw new MessageDeliveryException(message, "No channel resolved by router '" + this.getComponentName() + "' and no 'defaultOutputChannel' defined.");
        }
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 29 with MessageDeliveryException

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

the class MessageSelectingInterceptorTests method testMultipleSelectorsReject.

@Test
public void testMultipleSelectorsReject() {
    boolean exceptionThrown = false;
    final AtomicInteger counter = new AtomicInteger();
    MessageSelector selector1 = new TestMessageSelector(true, counter);
    MessageSelector selector2 = new TestMessageSelector(false, counter);
    MessageSelector selector3 = new TestMessageSelector(false, counter);
    MessageSelector selector4 = new TestMessageSelector(true, counter);
    MessageSelectingInterceptor interceptor = new MessageSelectingInterceptor(selector1, selector2, selector3, selector4);
    QueueChannel channel = new QueueChannel();
    channel.addInterceptor(interceptor);
    try {
        channel.send(new GenericMessage<String>("test1"));
    } catch (MessageDeliveryException e) {
        exceptionThrown = true;
    }
    assertTrue(exceptionThrown);
    assertEquals(2, counter.get());
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageSelector(org.springframework.integration.core.MessageSelector) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 30 with MessageDeliveryException

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

the class RemoteFileTemplate method send.

private String send(final Message<?> message, final String subDirectory, final FileExistsMode mode) {
    Assert.notNull(this.directoryExpressionProcessor, "'remoteDirectoryExpression' is required");
    Assert.isTrue(!FileExistsMode.APPEND.equals(mode) || !this.useTemporaryFileName, "Cannot append when using a temporary file name");
    Assert.isTrue(!FileExistsMode.REPLACE_IF_MODIFIED.equals(mode), "FilExistsMode.REPLACE_IF_MODIFIED can only be used for local files");
    final StreamHolder inputStreamHolder = this.payloadToInputStream(message);
    if (inputStreamHolder != null) {
        try {
            return this.execute(session -> {
                String fileName = inputStreamHolder.getName();
                try {
                    String remoteDirectory = RemoteFileTemplate.this.directoryExpressionProcessor.processMessage(message);
                    remoteDirectory = RemoteFileTemplate.this.normalizeDirectoryPath(remoteDirectory);
                    if (StringUtils.hasText(subDirectory)) {
                        if (subDirectory.startsWith(RemoteFileTemplate.this.remoteFileSeparator)) {
                            remoteDirectory += subDirectory.substring(1);
                        } else {
                            remoteDirectory += RemoteFileTemplate.this.normalizeDirectoryPath(subDirectory);
                        }
                    }
                    String temporaryRemoteDirectory = remoteDirectory;
                    if (RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor != null) {
                        temporaryRemoteDirectory = RemoteFileTemplate.this.temporaryDirectoryExpressionProcessor.processMessage(message);
                    }
                    fileName = RemoteFileTemplate.this.fileNameGenerator.generateFileName(message);
                    RemoteFileTemplate.this.sendFileToRemoteDirectory(inputStreamHolder.getStream(), temporaryRemoteDirectory, remoteDirectory, fileName, session, mode);
                    return remoteDirectory + fileName;
                } catch (FileNotFoundException e) {
                    throw new MessageDeliveryException(message, "File [" + inputStreamHolder.getName() + "] not found in local working directory; it was moved or deleted unexpectedly.", e);
                } catch (IOException e) {
                    throw new MessageDeliveryException(message, "Failed to transfer file [" + inputStreamHolder.getName() + " -> " + fileName + "] from local directory to remote directory.", e);
                } catch (Exception e) {
                    throw new MessageDeliveryException(message, "Error handling message for file [" + inputStreamHolder.getName() + " -> " + fileName + "]", e);
                }
            });
        } finally {
            try {
                inputStreamHolder.getStream().close();
            } catch (IOException e) {
            }
        }
    } else {
        // A null holder means a File payload that does not exist.
        if (this.logger.isWarnEnabled()) {
            this.logger.warn("File " + message.getPayload() + " does not exist");
        }
        return null;
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) IOException(java.io.IOException) MessagingException(org.springframework.messaging.MessagingException) IOException(java.io.IOException) BeansException(org.springframework.beans.BeansException) FileNotFoundException(java.io.FileNotFoundException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

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