org.springframework.messaging.MessagingException类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(13.7k)|赞(0)|评价(0)|浏览(1735)

本文整理了Java中org.springframework.messaging.MessagingException类的一些代码示例,展示了MessagingException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MessagingException类的具体详情如下:
包路径:org.springframework.messaging.MessagingException
类名称:MessagingException

MessagingException介绍

[英]The base exception for any failures related to messaging.
[中]与消息传递相关的任何故障的基本异常。

代码示例

代码示例来源:origin: spring-projects/spring-framework

protected MessagingException convertJmsException(JmsException ex) {
  if (ex instanceof org.springframework.jms.support.destination.DestinationResolutionException ||
      ex instanceof InvalidDestinationException) {
    return new DestinationResolutionException(ex.getMessage(), ex);
  }
  if (ex instanceof org.springframework.jms.support.converter.MessageConversionException) {
    return new MessageConversionException(ex.getMessage(), ex);
  }
  // Fallback
  return new MessagingException(ex.getMessage(), ex);
}

代码示例来源:origin: spring-projects/spring-integration-samples

@Test
  public void testTrueHeader() {
    String payload = "XXXABCXXX";
    String fileName = "abc.txt";
    gateway.process(payload, fileName);
    Message<?> errorMessage = testChannel.receive(0);
    assertNotNull("Expected an error message", errorMessage);
    assertEquals(payload, ((MessagingException) errorMessage.getPayload()).getFailedMessage().getPayload());
    Throwable cause = ((MessagingException) errorMessage.getPayload()).getCause();
    assertTrue("Expected exception, got:" + cause, cause instanceof MessageHandlingException);
  }
}

代码示例来源:origin: spring-projects/spring-integration-samples

@Test
public void testTimeoutReturn() {
  try {
    gw.send("TIMEOUT_TEST_RETURN");
    fail("expected exception");
  }
  catch (MessagingException e) {
    assertThat(e.getMessage(), containsString("No response received for TIMEOUT_TEST"));
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public boolean send(Message<?> message, long timeout) {
  try {
    this.handler.handleMessage(message);
    return true;
  }
  catch (RuntimeException e) {
    if (e instanceof MessagingException &&
        ((MessagingException) e).getFailedMessage() == null) {
      throw new MessagingException(message, "Failed to handle Message", e);
    }
    else {
      throw e;
    }
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void handlerThrowsExceptionPublishSubscribeWithExecutor() {
  StaticApplicationContext context = new StaticApplicationContext();
  context.registerSingleton(
      IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
  context.refresh();
  DirectChannel defaultErrorChannel = (DirectChannel) context.getBean(
      IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
  TaskExecutor executor = new SimpleAsyncTaskExecutor();
  PublishSubscribeChannel channel = new PublishSubscribeChannel(executor);
  channel.setBeanFactory(context);
  channel.afterPropertiesSet();
  ResultHandler resultHandler = new ResultHandler();
  defaultErrorChannel.subscribe(resultHandler);
  channel.subscribe(message -> {
    throw new MessagingException(message,
        new UnsupportedOperationException("intentional test failure"));
  });
  Message<?> message = MessageBuilder.withPayload("test").build();
  channel.send(message);
  this.waitForLatch(10000);
  Message<?> errorMessage = resultHandler.lastMessage;
  assertEquals(MessagingException.class, errorMessage.getPayload().getClass());
  MessagingException exceptionPayload = (MessagingException) errorMessage.getPayload();
  assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
  assertSame(message, exceptionPayload.getFailedMessage());
  assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}

代码示例来源:origin: spring-projects/spring-integration

@Test
@MongoDbAvailable
public void testInt3076ErrorMessage() throws Exception {
  MessageStore store = this.getMessageStore();
  Person p = new Person();
  p.setFname("John");
  p.setLname("Doe");
  Message<Person> failedMessage = MessageBuilder.withPayload(p).build();
  MessagingException messagingException;
  try {
    throw new RuntimeException("intentional");
  }
  catch (Exception e) {
    messagingException = new MessagingException(failedMessage, "intentional MessagingException", e);
  }
  Message<?> messageToStore = new ErrorMessage(messagingException);
  store.addMessage(messageToStore);
  Message<?> retrievedMessage = store.getMessage(messageToStore.getHeaders().getId());
  assertNotNull(retrievedMessage);
  assertTrue(retrievedMessage instanceof ErrorMessage);
  assertThat(retrievedMessage.getPayload(), Matchers.instanceOf(MessagingException.class));
  assertThat(((MessagingException) retrievedMessage.getPayload()).getMessage(),
      containsString("intentional MessagingException"));
  assertEquals(failedMessage, ((MessagingException) retrievedMessage.getPayload()).getFailedMessage());
  assertEquals(messageToStore.getHeaders(), retrievedMessage.getHeaders());
}

代码示例来源:origin: spring-cloud/spring-cloud-sleuth

private Message<?> getMessage(Message<?> message) {
  Object payload = message.getPayload();
  if (payload instanceof MessagingException) {
    MessagingException e = (MessagingException) payload;
    return e.getFailedMessage();
  }
  return message;
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testRetryWithinOnMessageAdapter() throws Exception {
  ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
  AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
  AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
  adapter.setOutputChannel(new DirectChannel());
  adapter.setRetryTemplate(new RetryTemplate());
  QueueChannel errors = new QueueChannel();
  ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
  recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
  adapter.setRecoveryCallback(recoveryCallback);
  adapter.afterPropertiesSet();
  ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
  listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes())
      .andProperties(new MessageProperties()).build(), null);
  Message<?> errorMessage = errors.receive(0);
  assertNotNull(errorMessage);
  assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
  MessagingException payload = (MessagingException) errorMessage.getPayload();
  assertThat(payload.getMessage(), containsString("Dispatcher has no"));
  assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
  org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders()
    .get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
  assertThat(amqpMessage, notNullValue());
  assertNull(errors.receive(0));
}

代码示例来源:origin: spring-projects/spring-integration-samples

assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust1"));
assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust1"));
assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust2"));
assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
assertTrue(e.getCause().getCause().getCause().getMessage().startsWith("host.for.cust3"));
assertThat(e.getCause().getCause().getCause(), instanceOf(UnknownHostException.class));
assertEquals("host.for.cust1", e.getCause().getCause().getCause().getMessage());

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

@Override
public String toString() {
  return super.toString() + " [rocketmqMsg=" + this.rocketmqMsg + "]";
}

代码示例来源:origin: spring-projects/spring-integration

private boolean invokeHandler(MessageHandler handler, Message<?> message) {
  try {
    handler.handleMessage(message);
    return true;
  }
  catch (RuntimeException e) {
    if (!this.ignoreFailures) {
      if (e instanceof MessagingException && ((MessagingException) e).getFailedMessage() == null) {
        throw new MessagingException(message, "Failed to handle Message", e);
      }
      throw e;
    }
    else if (this.logger.isWarnEnabled()) {
      logger.warn("Suppressing Exception since 'ignoreFailures' is set to TRUE.", e);
    }
    return false;
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void handlerThrowsExceptionExecutorChannel() {
  StaticApplicationContext context = new StaticApplicationContext();
  context.registerSingleton(
      IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, DirectChannel.class);
  context.refresh();
  DirectChannel defaultErrorChannel = (DirectChannel) context.getBean(
      IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME);
  TaskExecutor executor = new SimpleAsyncTaskExecutor();
  ExecutorChannel channel = new ExecutorChannel(executor);
  channel.setBeanFactory(context);
  channel.afterPropertiesSet();
  ResultHandler resultHandler = new ResultHandler();
  defaultErrorChannel.subscribe(resultHandler);
  channel.subscribe(message -> {
    throw new MessagingException(message,
        new UnsupportedOperationException("intentional test failure"));
  });
  Message<?> message = MessageBuilder.withPayload("test").build();
  channel.send(message);
  this.waitForLatch(10000);
  Message<?> errorMessage = resultHandler.lastMessage;
  assertEquals(MessagingException.class, errorMessage.getPayload().getClass());
  MessagingException exceptionPayload = (MessagingException) errorMessage.getPayload();
  assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
  assertSame(message, exceptionPayload.getFailedMessage());
  assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}

代码示例来源:origin: spring-projects/spring-integration-samples

@Transformer
public Message<?> transform(ErrorMessage errorMessage) {
  return ((MessagingException) errorMessage.getPayload()).getFailedMessage();
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void testRetryWithinOnMessageGateway() throws Exception {
  ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
  AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
  AmqpInboundGateway adapter = new AmqpInboundGateway(container);
  adapter.setRequestChannel(new DirectChannel());
  adapter.setRetryTemplate(new RetryTemplate());
  QueueChannel errors = new QueueChannel();
  ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
  recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
  adapter.setRecoveryCallback(recoveryCallback);
  adapter.afterPropertiesSet();
  ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
  listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes())
      .andProperties(new MessageProperties()).build(), null);
  Message<?> errorMessage = errors.receive(0);
  assertNotNull(errorMessage);
  assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
  MessagingException payload = (MessagingException) errorMessage.getPayload();
  assertThat(payload.getMessage(), containsString("Dispatcher has no"));
  assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
  org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders()
    .get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
  assertThat(amqpMessage, notNullValue());
  assertNull(errors.receive(0));
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void returnAddressFallbackButNotAvailable() {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
      "returnAddressTests.xml", this.getClass());
  MessageChannel channel3 = (MessageChannel) context.getBean("channel3");
  context.start();
  GenericMessage<String> message = new GenericMessage<String>("*");
  try {
    channel3.send(message);
  }
  catch (MessagingException e) {
    assertTrue(e.getCause() instanceof DestinationResolutionException);
  }
  context.close();
}

代码示例来源:origin: spring-projects/spring-integration

@Override
public String toString() {
  return super.toString() + " [amqpMessage=" + this.amqpMessage + ", replyCode=" + this.replyCode
      + ", replyText=" + this.replyText + ", exchange=" + this.exchange + ", routingKey=" + this.routingKey
      + "]";
}

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

logger.error(
    "RocketMQ Message hasn't been sent. Caused by " + e.getMessage());
throw new MessagingException(e.getMessage(), e);

代码示例来源:origin: spring-projects/spring-integration

@Override
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message)
    throws Exception {
  RetryState retryState = null;
  retryState = this.retryStateGenerator.determineRetryState(message);
  messageHolder.set(message);
  try {
    return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
  }
  catch (MessagingException e) {
    if (e.getFailedMessage() == null) {
      throw new MessagingException(message, "Failed to invoke handler", e);
    }
    throw e;
  }
  catch (Exception e) {
    throw new MessagingException(message, "Failed to invoke handler", unwrapExceptionIfNecessary(e));
  }
  finally {
    messageHolder.remove();
  }
}

代码示例来源:origin: spring-projects/spring-integration

@Test
public void failedMessage() {
  GenericMessage<String> message = new GenericMessage<>("fail");
  try {
    gateway.handleMessage(message);
    fail("Exception expected");
  }
  catch (MessagingException e) {
    assertSame(message, e.getFailedMessage());
    assertEquals("bar", ((MessagingException) e.getCause()).getFailedMessage().getPayload());
  }
}

代码示例来源:origin: spring-projects/spring-integration

/**
 * Publish an error message for the supplied exception.
 * @param exception the exception.
 */
public void publish(MessagingException exception) {
  publish(null, exception.getFailedMessage(), exception);
}

相关文章