@Override protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) { throw new MessageHandlingException(message, "Missing header '" + headerName + "' for method parameter type [" + parameter.getParameterType() + "]"); }
@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); }
@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")); } }
/** * 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."); }
@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() + "]"); }
@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."); } }
/** * 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."); }
/** * 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; }
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(); } } }
assertThat(e.getMessage(), containsString("already exists")); assertThat(e.getMessage(), containsString("already exists"));
new MessageHandlingException(message, "Unexpected handler method invocation error", ex); processHandlerMethodException(handlerMethod, handlingException, message);
@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"); }
assertThat(e.getMessage(), Matchers.containsString("Unable to find outbound socket"));
private MessageHandlingException wrapToMessageHandlingExceptionIfNecessary(Message<?> message, String description, Throwable cause) { if (cause instanceof MessageHandlingException) { throw (MessageHandlingException) cause; } else { throw new MessageHandlingException(message, description, cause); } }
@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(); }
assertThat(e.getMessage(), containsString("Failed to clone payload object")); return;
@Override protected void handleMissingValue(String headerName, MethodParameter parameter, Message<?> message) { throw new MessageHandlingException(message, "Missing header '" + headerName + "' for method parameter type [" + parameter.getParameterType() + "]"); }
@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")); } }
@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() + "]"); }
@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"); }