Search in sources :

Example 36 with MessageDeliveryException

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

the class MethodInvokingRouterTests method doTestMultiChannelListResolutionByMessage.

private void doTestMultiChannelListResolutionByMessage(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 37 with MessageDeliveryException

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

the class MethodInvokingRouterTests method doTestChannelInstanceResolutionByPayload.

private void doTestChannelInstanceResolutionByPayload(MethodInvokingRouter router, TestChannelResolver channelResolver) {
    Message<String> fooMessage = new GenericMessage<String>("foo");
    Message<String> barMessage = new GenericMessage<String>("bar");
    Message<String> badMessage = new GenericMessage<String>("bad");
    QueueChannel fooChannel = new QueueChannel();
    QueueChannel barChannel = new QueueChannel();
    channelResolver.addChannel("foo-channel", fooChannel);
    channelResolver.addChannel("bar-channel", barChannel);
    router.setChannelResolver(channelResolver);
    router.handleMessage(fooMessage);
    Message<?> result1 = fooChannel.receive(0);
    assertNotNull(result1);
    assertEquals("foo", result1.getPayload());
    router.handleMessage(barMessage);
    Message<?> result2 = barChannel.receive(0);
    assertNotNull(result2);
    assertEquals("bar", result2.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 38 with MessageDeliveryException

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

the class ChannelIntegrationTests method testMessageChannelStatistics.

@Test
public void testMessageChannelStatistics() throws Exception {
    requests.send(new GenericMessage<String>("foo"));
    String intermediateChannelName = "" + intermediate;
    assertEquals(1, messageChannelsMonitor.getChannelSendCount(intermediateChannelName));
    double rate = messageChannelsMonitor.getChannelSendRate("" + requests).getMean();
    assertTrue("No statistics for requests channel", rate >= 0);
    rate = messageChannelsMonitor.getChannelSendRate(intermediateChannelName).getMean();
    assertTrue("No statistics for intermediate channel", rate >= 0);
    assertNotNull(intermediate.receive(100L));
    assertEquals(1, messageChannelsMonitor.getChannelReceiveCount(intermediateChannelName));
    requests.send(new GenericMessage<String>("foo"));
    try {
        requests.send(new GenericMessage<String>("foo"));
    } catch (MessageDeliveryException e) {
    }
    assertEquals(3, messageChannelsMonitor.getChannelSendCount(intermediateChannelName));
    assertEquals(1, messageChannelsMonitor.getChannelSendErrorCount(intermediateChannelName));
    assertSame(intermediate, messageChannelsMonitor.getChannelMetrics(intermediateChannelName));
    MessageHandlerMetrics handlerMetrics = messageChannelsMonitor.getHandlerMetrics("bridge");
    assertEquals(3, handlerMetrics.getHandleCount());
    assertEquals(1, handlerMetrics.getErrorCount());
    assertNotNull(this.sourceChannel.receive(10000));
    assertTrue(messageChannelsMonitor.getSourceMessageCount("source") > 0);
    assertTrue(messageChannelsMonitor.getSourceMetrics("source").getMessageCount() > 0);
}
Also used : MessageHandlerMetrics(org.springframework.integration.support.management.MessageHandlerMetrics) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 39 with MessageDeliveryException

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

the class UnicastSendingMessageHandler method handleMessageInternal.

@Override
public void handleMessageInternal(Message<?> message) throws MessageHandlingException, MessageDeliveryException {
    if (this.acknowledge) {
        Assert.state(this.isRunning(), "When 'acknowledge' is enabled, adapter must be running");
        startAckThread();
    }
    CountDownLatch countdownLatch = null;
    String messageId = message.getHeaders().getId().toString();
    try {
        boolean waitForAck = this.waitForAck;
        if (waitForAck) {
            countdownLatch = new CountDownLatch(this.ackCounter);
            this.ackControl.put(messageId, countdownLatch);
        }
        convertAndSend(message);
        if (waitForAck) {
            try {
                if (!countdownLatch.await(this.ackTimeout, TimeUnit.MILLISECONDS)) {
                    throw new MessagingException(message, "Failed to receive UDP Ack in " + this.ackTimeout + " millis");
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    } catch (MessagingException e) {
        throw e;
    } catch (Exception e) {
        closeSocketIfNeeded();
        throw new MessageHandlingException(message, "failed to send UDP packet", e);
    } finally {
        if (countdownLatch != null) {
            this.ackControl.remove(messageId);
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) MessagingException(org.springframework.messaging.MessagingException) SocketException(java.net.SocketException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) IOException(java.io.IOException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 40 with MessageDeliveryException

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

the class StompMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(final Message<?> message) throws Exception {
    try {
        connectIfNecessary();
    } catch (Exception e) {
        throw new MessageDeliveryException(message, "The [" + this + "] could not deliver message.", e);
    }
    StompSession stompSession = this.stompSession;
    StompHeaders stompHeaders = new StompHeaders();
    this.headerMapper.fromHeaders(message.getHeaders(), stompHeaders);
    if (stompHeaders.getDestination() == null) {
        Assert.state(this.destinationExpression != null, "One of 'destination' or 'destinationExpression' must be" + " provided, if message header doesn't supply 'destination' STOMP header.");
        String destination = this.destinationExpression.getValue(this.evaluationContext, message, String.class);
        stompHeaders.setDestination(destination);
    }
    final StompSession.Receiptable receiptable = stompSession.send(stompHeaders, message.getPayload());
    if (receiptable.getReceiptId() != null) {
        final String destination = stompHeaders.getDestination();
        final ApplicationEventPublisher applicationEventPublisher = this.applicationEventPublisher;
        if (applicationEventPublisher != null) {
            receiptable.addReceiptTask(() -> {
                StompReceiptEvent event = new StompReceiptEvent(StompMessageHandler.this, destination, receiptable.getReceiptId(), StompCommand.SEND, false);
                event.setMessage(message);
                applicationEventPublisher.publishEvent(event);
            });
        }
        receiptable.addReceiptLostTask(() -> {
            if (applicationEventPublisher != null) {
                StompReceiptEvent event = new StompReceiptEvent(StompMessageHandler.this, destination, receiptable.getReceiptId(), StompCommand.SEND, true);
                event.setMessage(message);
                applicationEventPublisher.publishEvent(event);
            } else {
                logger.error("The receipt [" + receiptable.getReceiptId() + "] is lost for [" + message + "] on destination [" + destination + "]");
            }
        });
    }
}
Also used : StompReceiptEvent(org.springframework.integration.stomp.event.StompReceiptEvent) StompSession(org.springframework.messaging.simp.stomp.StompSession) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) StompHeaders(org.springframework.messaging.simp.stomp.StompHeaders) MessagingException(org.springframework.messaging.MessagingException) ConnectionLostException(org.springframework.messaging.simp.stomp.ConnectionLostException) 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