Search in sources :

Example 76 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-cloud-stream by spring-cloud.

the class BinderAwareChannelResolverTests method resolveChannel.

@Test
public void resolveChannel() {
    Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(0, bindable.getOutputs().size());
    }
    MessageChannel registered = resolver.resolveDestination("foo");
    assertEquals(2, ((AbstractMessageChannel) registered).getChannelInterceptors().size());
    assertTrue(((AbstractMessageChannel) registered).getChannelInterceptors().get(1) instanceof ImmutableMessageChannelInterceptor);
    bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(1, bindable.getOutputs().size());
    }
    DirectChannel testChannel = new DirectChannel();
    testChannel.setComponentName("INPUT");
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Message<?>> received = new ArrayList<>();
    testChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            received.add(message);
            latch.countDown();
        }
    });
    this.binder.bindConsumer("foo", null, testChannel, new ConsumerProperties());
    assertThat(received).hasSize(0);
    registered.send(MessageBuilder.withPayload("hello").build());
    try {
        assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("Latch timed out");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("interrupted while awaiting latch");
    }
    assertThat(received).hasSize(1);
    assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
    this.context.close();
    for (Bindable bindable : bindables.values()) {
        assertEquals(0, bindable.getInputs().size());
        // Must not be bound"
        assertEquals(0, bindable.getOutputs().size());
    }
}
Also used : AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) Message(org.springframework.messaging.Message) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) ImmutableMessageChannelInterceptor(org.springframework.messaging.support.ImmutableMessageChannelInterceptor) Bindable(org.springframework.cloud.stream.binding.Bindable) DynamicDestinationsBindable(org.springframework.cloud.stream.binding.DynamicDestinationsBindable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 77 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-cloud-stream by spring-cloud.

the class ExtendedPropertiesBinderAwareChannelResolverTests method resolveChannel.

@Test
@Override
public void resolveChannel() {
    Map<String, Bindable> bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(0, bindable.getOutputs().size());
    }
    MessageChannel registered = resolver.resolveDestination("foo");
    bindables = context.getBeansOfType(Bindable.class);
    assertThat(bindables).hasSize(1);
    for (Bindable bindable : bindables.values()) {
        // producer
        assertEquals(0, bindable.getInputs().size());
        // consumer
        assertEquals(1, bindable.getOutputs().size());
    }
    DirectChannel testChannel = new DirectChannel();
    final CountDownLatch latch = new CountDownLatch(1);
    final List<Message<?>> received = new ArrayList<>();
    testChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            received.add(message);
            latch.countDown();
        }
    });
    binder.bindConsumer("foo", null, testChannel, new ExtendedConsumerProperties<ConsumerProperties>(new ConsumerProperties()));
    assertThat(received).hasSize(0);
    registered.send(MessageBuilder.withPayload("hello").build());
    try {
        assertThat(latch.await(1, TimeUnit.SECONDS)).describedAs("latch timed out");
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        fail("interrupted while awaiting latch");
    }
    assertThat(received).hasSize(1);
    assertThat(new String((byte[]) received.get(0).getPayload())).isEqualTo("hello");
    context.close();
    for (Bindable bindable : bindables.values()) {
        assertEquals(0, bindable.getInputs().size());
        // Must not be bound"
        assertEquals(0, bindable.getOutputs().size());
    }
}
Also used : Message(org.springframework.messaging.Message) MessageHandler(org.springframework.messaging.MessageHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) Bindable(org.springframework.cloud.stream.binding.Bindable) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 78 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-framework by spring-projects.

the class SubProtocolWebSocketHandler method handleMessage.

/**
 * Handle an outbound Spring Message to a WebSocket client.
 */
@Override
public void handleMessage(Message<?> message) throws MessagingException {
    String sessionId = resolveSessionId(message);
    if (sessionId == null) {
        if (logger.isErrorEnabled()) {
            logger.error("Could not find session id in " + message);
        }
        return;
    }
    WebSocketSessionHolder holder = this.sessions.get(sessionId);
    if (holder == null) {
        if (logger.isDebugEnabled()) {
            // The broker may not have removed the session yet
            logger.debug("No session for " + message);
        }
        return;
    }
    WebSocketSession session = holder.getSession();
    try {
        findProtocolHandler(session).handleMessageToClient(session, message);
    } catch (SessionLimitExceededException ex) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("Terminating '" + session + "'", ex);
            } else if (logger.isWarnEnabled()) {
                logger.warn("Terminating '" + session + "': " + ex.getMessage());
            }
            this.stats.incrementLimitExceededCount();
            // clear first, session may be unresponsive
            clearSession(session, ex.getStatus());
            session.close(ex.getStatus());
        } catch (Exception secondException) {
            logger.debug("Failure while closing session " + sessionId + ".", secondException);
        }
    } catch (Exception ex) {
        // Could be part of normal workflow (e.g. browser tab closed)
        if (logger.isDebugEnabled()) {
            logger.debug("Failed to send message to client in " + session + ": " + message, ex);
        }
    }
}
Also used : SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) MessagingException(org.springframework.messaging.MessagingException) SessionLimitExceededException(org.springframework.web.socket.handler.SessionLimitExceededException) WebSocketSession(org.springframework.web.socket.WebSocketSession)

Example 79 with MessagingException

use of org.springframework.messaging.MessagingException in project spring-framework by spring-projects.

the class AbstractMessageChannel method send.

@Override
public final boolean send(Message<?> message, long timeout) {
    Assert.notNull(message, "Message must not be null");
    Message<?> messageToUse = message;
    ChannelInterceptorChain chain = new ChannelInterceptorChain();
    boolean sent = false;
    try {
        messageToUse = chain.applyPreSend(messageToUse, this);
        if (messageToUse == null) {
            return false;
        }
        sent = sendInternal(messageToUse, timeout);
        chain.applyPostSend(messageToUse, this, sent);
        chain.triggerAfterSendCompletion(messageToUse, this, sent, null);
        return sent;
    } catch (Exception ex) {
        chain.triggerAfterSendCompletion(messageToUse, this, sent, ex);
        if (ex instanceof MessagingException) {
            throw (MessagingException) ex;
        }
        throw new MessageDeliveryException(messageToUse, "Failed to send message to " + this, ex);
    } catch (Throwable err) {
        MessageDeliveryException ex2 = new MessageDeliveryException(messageToUse, "Failed to send message to " + this, err);
        chain.triggerAfterSendCompletion(messageToUse, this, sent, ex2);
        throw ex2;
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 80 with MessagingException

use of org.springframework.messaging.MessagingException in project rocketmq-externals by apache.

the class RocketMQTemplate method sendOneWayOrderly.

/**
 * Same to {@link #sendOneWay(String, Message)} with send orderly with hashKey by specified.
 *
 * @param destination formats: `topicName:tags`
 * @param message {@link org.springframework.messaging.Message}
 * @param hashKey use this key to select queue. for example: orderId, productId ...
 */
public void sendOneWayOrderly(String destination, Message<?> message, String hashKey) {
    if (Objects.isNull(message) || Objects.isNull(message.getPayload())) {
        log.info("sendOneWayOrderly failed. destination:{}, message is null ", destination);
        throw new IllegalArgumentException("`message` and `message.payload` cannot be null");
    }
    try {
        org.apache.rocketmq.common.message.Message rocketMsg = convertToRocketMsg(destination, message);
        producer.sendOneway(rocketMsg, messageQueueSelector, hashKey);
    } catch (Exception e) {
        log.info("sendOneWayOrderly failed. destination:{}, message:{}", destination, message);
        throw new MessagingException(e.getMessage(), e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MessagingException(org.springframework.messaging.MessagingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

MessagingException (org.springframework.messaging.MessagingException)143 Test (org.junit.Test)60 IOException (java.io.IOException)25 Message (org.springframework.messaging.Message)23 QueueChannel (org.springframework.integration.channel.QueueChannel)22 ErrorMessage (org.springframework.messaging.support.ErrorMessage)21 CountDownLatch (java.util.concurrent.CountDownLatch)17 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 MessageHandlingException (org.springframework.messaging.MessageHandlingException)15 GenericMessage (org.springframework.messaging.support.GenericMessage)15 MessageHandler (org.springframework.messaging.MessageHandler)14 File (java.io.File)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 DirectChannel (org.springframework.integration.channel.DirectChannel)12 ArrayList (java.util.ArrayList)11 PollableChannel (org.springframework.messaging.PollableChannel)11 Lock (java.util.concurrent.locks.Lock)8 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)8