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

x33g5p2x  于2022-01-25 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(1038)

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

MessageHandlingException介绍

[英]Exception that indicates an error occurred during message handling.
[中]异常,指示在消息处理过程中发生错误。

代码示例

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

@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
  throw new MessageHandlingException(message, "Missing header '" + headerName +
      "' for method parameter type [" + parameter.getParameterType() + "]");
}

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

@Test
@DirtiesContext
@SuppressWarnings("unchecked")
public void testWrongPayload() {
  final Message<String> stringMessage = MessageBuilder.withPayload("just text").build();
  try {
    requestChannel.send(stringMessage);
    fail();
  }
  catch (MessageHandlingException e) {
    String message = e.getCause().getMessage();
    assertTrue("Wrong message: " + message, message.contains("The payload must be of type JobLaunchRequest."));
  }
  Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000);
  assertNull("JobExecution message received when no return address set", executionMessage);
}

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

@Test
public void exceptionExpected() {
  try {
    shouldFail.send(new GenericMessage<String>("foo"));
    fail("Exception expected");
  }
  catch (MessageHandlingException e) {
    assertThat(e.getMessage(), startsWith("Unable to find outbound socket"));
  }
}

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

/**
 * Test uses the Fail mode of the File Outbound Gateway. When persisting
 * a payload using the File Outbound Gateway and the mode is set to Fail,
 * then the destination {@link File} will be created and written if it does
 * not yet exist. BUT if the destination {@link File} already exists, a
 * {@link MessageHandlingException} will be thrown.
 *
 */
@Test
public void gatewayWithFailMode() throws Exception {
  final MessagingTemplate messagingTemplate = new MessagingTemplate();
  messagingTemplate.setDefaultDestination(this.gatewayWithFailModeChannel);
  String expectedFileContent = "Initial File Content:";
  File testFile = new File("test/fileToAppend.txt");
  if (testFile.exists()) {
    testFile.delete();
  }
  messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:"));
  final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
  assertEquals(expectedFileContent, actualFileContent);
  try {
    messagingTemplate.sendAndReceive(new GenericMessage<>("String content:"));
  }
  catch (MessageHandlingException e) {
    assertThat(e.getMessage(), startsWith("The destination file already exists at '"));
    return;
  }
  fail("Was expecting a MessageHandlingException to be thrown.");
}

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

@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
  throw new MessageHandlingException(message, "Missing path template variable '" + name +
      "' for method parameter type [" + parameter.getParameterType() + "]");
}

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

@Test
  public void testExceptionRaised() throws Exception {

    final Message<JobLaunchRequest> message = MessageBuilder.withPayload(new JobLaunchRequest(new JobSupport("testJob"),
    new JobParameters())).build();

    final JobLauncher jobLauncher = mock(JobLauncher.class);
    when(jobLauncher.run(any(Job.class), any(JobParameters.class)))
      .thenThrow(new JobParametersInvalidException("This is a JobExecutionException."));

    JobLaunchingGateway jobLaunchingGateway = new JobLaunchingGateway(jobLauncher);

    try {
      jobLaunchingGateway.handleMessage(message);
    }
    catch (MessageHandlingException e) {
      assertEquals("This is a JobExecutionException.", e.getCause().getMessage());
      return;
    }

    fail("Expecting a MessageHandlingException to be thrown.");

  }
}

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

/**
 * Test is exactly the same as {@link #gatewayWithFailMode()}. However, the
 * mode is provided in lower-case ensuring that the mode can be provided
 * in an case-insensitive fashion.
 *
 * Instead a {@link MessageHandlingException} will be thrown.
 *
 */
@Test
public void gatewayWithFailModeLowercase() throws Exception {
  final MessagingTemplate messagingTemplate = new MessagingTemplate();
  messagingTemplate.setDefaultDestination(this.gatewayWithFailModeLowercaseChannel);
  String expectedFileContent = "Initial File Content:";
  File testFile = new File("test/fileToAppend.txt");
  if (testFile.exists()) {
    testFile.delete();
  }
  messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:"));
  final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile));
  assertEquals(expectedFileContent, actualFileContent);
  try {
    messagingTemplate.sendAndReceive(new GenericMessage<>("String content:"));
  }
  catch (MessageHandlingException e) {
    assertThat(e.getMessage(), startsWith("The destination file already exists at '"));
    return;
  }
  fail("Was expecting a MessageHandlingException to be thrown.");
}

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

/**
 * Launches a Batch Job using the provided request {@link Message}. The payload
 * of the {@link Message} <em>must</em> be an instance of {@link JobLaunchRequest}.
 *
 * @param requestMessage must not be null.
 * @return Generally a {@link JobExecution} will always be returned. An
 * exception ({@link MessageHandlingException}) will only be thrown if there
 * is a failure to start the job. The cause of the exception will be a
 * {@link JobExecutionException}.
 *
 * @throws MessageHandlingException when a job cannot be launched
 */
@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
  Assert.notNull(requestMessage, "The provided requestMessage must not be null.");
  final Object payload = requestMessage.getPayload();
  Assert.isInstanceOf(JobLaunchRequest.class, payload, "The payload must be of type JobLaunchRequest.");
  final JobLaunchRequest jobLaunchRequest = (JobLaunchRequest) payload;
  final JobExecution jobExecution;
  try {
    jobExecution = this.jobLaunchingMessageHandler.launch(jobLaunchRequest);
  } catch (JobExecutionException e) {
    throw new MessageHandlingException(requestMessage, e);
  }
  return jobExecution;
}

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

private void handleMessageAsClient(Message<?> message) {
  // we own the connection
  TcpConnection connection = null;
  try {
    connection = doWrite(message);
  }
  catch (MessageHandlingException e) {
    // retry - socket may have closed
    if (e.getCause() instanceof IOException) {
      if (logger.isDebugEnabled()) {
        logger.debug("Fail on first write attempt", e);
      }
      connection = doWrite(message);
    }
    else {
      throw e;
    }
  }
  finally {
    if (connection != null && this.isSingleUse
        && this.clientConnectionFactory.getListener() == null) {
      // if there's no collaborating inbound adapter, close immediately, otherwise
      // it will close after receiving the reply.
      connection.close();
    }
  }
}

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

assertThat(e.getMessage(), containsString("already exists"));
assertThat(e.getMessage(), containsString("already exists"));

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

new MessageHandlingException(message, "Unexpected handler method invocation error", ex);
processHandlerMethodException(handlerMethod, handlingException, message);

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

@Test
public void saveToSubDirWithEmptyStringExpression() throws Exception {
  try {
    this.inputChannelSaveToSubDirEmptyStringExpression.send(message);
  }
  catch (MessageHandlingException e) {
    Assert.assertEquals("Unable to resolve Destination Directory for the provided Expression ''   ''.",
        e.getCause().getMessage());
    return;
  }
  Assert.fail("Was expecting a MessageHandlingException to be thrown");
}

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

assertThat(e.getMessage(), Matchers.containsString("Unable to find outbound socket"));

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

private MessageHandlingException wrapToMessageHandlingExceptionIfNecessary(Message<?> message, String description,
    Throwable cause) {
  if (cause instanceof MessageHandlingException) {
    throw (MessageHandlingException) cause;
  }
  else {
    throw new MessageHandlingException(message, description, cause);
  }
}

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

@Test
public void errorChannel() throws Exception {
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("InboundOneWayErrorTests-context.xml", getClass());
  JmsTemplate jmsTemplate = new JmsTemplate(context.getBean("jmsConnectionFactory", ConnectionFactory.class));
  Destination queue = context.getBean("queueB", Destination.class);
  jmsTemplate.send(queue, (MessageCreator) session -> session.createTextMessage("test-B"));
  PollableChannel errorChannel = context.getBean("testErrorChannel", PollableChannel.class);
  Message<?> errorMessage = errorChannel.receive(3000);
  assertNotNull(errorMessage);
  assertEquals(MessageHandlingException.class, errorMessage.getPayload().getClass());
  MessageHandlingException exception = (MessageHandlingException) errorMessage.getPayload();
  assertNotNull(exception.getCause());
  assertEquals(TestException.class, exception.getCause().getClass());
  assertEquals("failed to process: test-B", exception.getCause().getMessage());
  TestErrorHandler errorHandler = context.getBean("testErrorHandler", TestErrorHandler.class);
  assertNull(errorHandler.lastError);
  context.close();
}

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

assertThat(e.getMessage(), containsString("Failed to clone payload object"));
return;

代码示例来源:origin: org.springframework/spring-messaging

@Override
protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) {
  throw new MessageHandlingException(message, "Missing header '" + headerName +
      "' for method parameter type [" + parameter.getParameterType() + "]");
}

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

@Test //INT-2631
public void testFailOperationOnAbstractBean() {
  try {
    Message<?> message = MessageBuilder.withPayload("abstractService.convert('testString')").build();
    this.input.send(message);
    fail("Expected BeanIsAbstractException");
  }
  catch (MessageHandlingException e) {
    Throwable cause = e.getCause();
    assertTrue("Expected BeanIsAbstractException, got " + cause.getClass() + ":" + cause.getMessage(),
        cause instanceof BeanIsAbstractException);
    assertTrue(cause.getMessage().contains("abstractService"));
  }
}

代码示例来源:origin: org.springframework/spring-messaging

@Override
protected void handleMissingValue(String name, MethodParameter parameter, Message<?> message) {
  throw new MessageHandlingException(message, "Missing path template variable '" + name +
      "' for method parameter type [" + parameter.getParameterType() + "]");
}

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

@Test
public void saveToSubDirWithWrongExpression() throws Exception {
  try {
    this.inputChannelSaveToSubDirWrongExpression.send(message);
  }
  catch (MessageHandlingException e) {
    Assert.assertEquals(
        TestUtils.applySystemFileSeparator("Destination path [target/base-directory/sub-directory/foo.txt] does not point to a directory."),
        e.getCause().getMessage());
    return;
  }
  Assert.fail("Was expecting a MessageHandlingException to be thrown");
}

相关文章