protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception exception, Message<?> message) { InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, exception); if (invocable == null) { logger.error("Unhandled exception from message handler method", exception); return; } invocable.setMessageMethodArgumentResolvers(this.argumentResolvers); if (logger.isDebugEnabled()) { logger.debug("Invoking " + invocable.getShortLogMessage()); } try { Throwable cause = exception.getCause(); Object returnValue = (cause != null ? invocable.invoke(message, exception, cause, handlerMethod) : invocable.invoke(message, exception, handlerMethod)); MethodParameter returnType = invocable.getReturnType(); if (void.class == returnType.getParameterType()) { return; } this.returnValueHandlers.handleReturnValue(returnValue, returnType, message); } catch (Throwable ex2) { logger.error("Error while processing handler method exception", ex2); } }
/** * Invoke the handler, wrapping any exception to a {@link ListenerExecutionFailedException} * with a dedicated error message. */ @Nullable private Object invokeHandler(javax.jms.Message jmsMessage, @Nullable Session session, Message<?> message) { InvocableHandlerMethod handlerMethod = getHandlerMethod(); try { return handlerMethod.invoke(message, jmsMessage, session); } catch (MessagingException ex) { throw new ListenerExecutionFailedException( createMessagingErrorMessage("Listener method could not be invoked with incoming message"), ex); } catch (Exception ex) { throw new ListenerExecutionFailedException("Listener method '" + handlerMethod.getMethod().toGenericString() + "' threw exception", ex); } }
@Nullable private Object invoke(Object handler, Method method, Object... providedArgs) throws Exception { InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(handler, method); handlerMethod.setMessageMethodArgumentResolvers(this.resolvers); return handlerMethod.invoke(this.message, providedArgs); }
Object returnValue = invocable.invoke(message); MethodParameter returnType = handlerMethod.getReturnType(); if (void.class == returnType.getParameterType()) {
@Test public void customArgumentResolver() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>(); customResolvers.add(new CustomHandlerMethodArgumentResolver()); instance.setCustomArgumentResolvers(customResolvers); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "customArgumentResolver", Locale.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build()); assertMethodInvocation(sample, "customArgumentResolver"); }
@Test public void overrideArgumentResolvers() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>(); customResolvers.add(new CustomHandlerMethodArgumentResolver()); instance.setArgumentResolvers(customResolvers); instance.afterPropertiesSet(); Message<String> message = MessageBuilder.withPayload("sample").build(); // This will work as the local resolver is set InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "customArgumentResolver", Locale.class); invocableHandlerMethod.invoke(message); assertMethodInvocation(sample, "customArgumentResolver"); // This won't work as no resolver is known for the payload InvocableHandlerMethod invocableHandlerMethod2 = createInvocableHandlerMethod(instance, "simpleString", String.class); thrown.expect(MethodArgumentResolutionException.class); thrown.expectMessage("No suitable resolver"); invocableHandlerMethod2.invoke(message); }
@Test public void noValidationByDefault() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "payloadValidation", String.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload("failure").build()); assertMethodInvocation(sample, "payloadValidation"); }
@Test public void customMessageConverterFailure() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); MessageConverter messageConverter = new ByteArrayMessageConverter(); instance.setMessageConverter(messageConverter); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "simpleString", String.class); thrown.expect(MessageConversionException.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build()); }
@Test public void customValidation() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); instance.setValidator(new Validator() { @Override public boolean supports(Class<?> clazz) { return String.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { String value = (String) target; if ("failure".equals(value)) { errors.reject("not a valid value"); } } }); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "payloadValidation", String.class); thrown.expect(MethodArgumentNotValidException.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload("failure").build()); }
@Test public void customConversionServiceFailure() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); GenericConversionService conversionService = new GenericConversionService(); assertFalse("conversion service should fail to convert payload", conversionService.canConvert(Integer.class, String.class)); instance.setConversionService(conversionService); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "simpleString", String.class); thrown.expect(MessageConversionException.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build()); }
@Test public void customConversion() throws Exception { DefaultMessageHandlerMethodFactory instance = createInstance(); GenericConversionService conversionService = new GenericConversionService(); conversionService.addConverter(SampleBean.class, String.class, new Converter<SampleBean, String>() { @Override public String convert(SampleBean source) { return "foo bar"; } }); instance.setConversionService(conversionService); instance.afterPropertiesSet(); InvocableHandlerMethod invocableHandlerMethod = createInvocableHandlerMethod(instance, "simpleString", String.class); invocableHandlerMethod.invoke(MessageBuilder.withPayload(sample).build()); assertMethodInvocation(sample, "simpleString"); }
/** * Invoke the method with the given message. * @param message the message. * @param providedArgs additional arguments. * @return the result of the invocation. * @throws Exception raised if no suitable argument resolver can be found, * or the method raised an exception. */ public Object invoke(Message<?> message, Object... providedArgs) throws Exception { //NOSONAR Class<? extends Object> payloadClass = message.getPayload().getClass(); InvocableHandlerMethod handler = getHandlerForPayload(payloadClass); Object result = handler.invoke(message, providedArgs); Expression replyTo = this.handlerSendTo.get(handler); return new InvocationResult(result, replyTo, this.handlerReturnsMessage.get(handler)); }
public Object invoke(Message<?> message, Object... providedArgs) throws Exception { //NOSONAR if (this.invokerHandlerMethod != null) { return this.invokerHandlerMethod.invoke(message, providedArgs); } else if (this.delegatingHandler.hasDefaultHandler()) { // Needed to avoid returning raw Message which matches Object Object[] args = new Object[providedArgs.length + 1]; args[0] = message.getPayload(); System.arraycopy(providedArgs, 0, args, 1, providedArgs.length); return this.delegatingHandler.invoke(message, args); } else { return this.delegatingHandler.invoke(message, providedArgs); } }
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception exception, Message<?> message) { InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, exception); if (invocable == null) { logger.error("Unhandled exception from message handler method", exception); return; } invocable.setMessageMethodArgumentResolvers(this.argumentResolvers); if (logger.isDebugEnabled()) { logger.debug("Invoking " + invocable.getShortLogMessage()); } try { Throwable cause = exception.getCause(); Object returnValue = (cause != null ? invocable.invoke(message, exception, cause, handlerMethod) : invocable.invoke(message, exception, handlerMethod)); MethodParameter returnType = invocable.getReturnType(); if (void.class == returnType.getParameterType()) { return; } this.returnValueHandlers.handleReturnValue(returnValue, returnType, message); } catch (Throwable ex2) { logger.error("Error while processing handler method exception", ex2); } }
@SuppressWarnings("unchecked") public <T> T invoke(ParametersWrapper parameters) throws Exception { Message<?> message = parameters.getMessage(); if (this.canProcessMessageList) { message = new MutableMessage<>(parameters.getMessages(), parameters.getHeaders()); } return (T) this.invocableHandlerMethod.invoke(message); }
Object returnValue = invocable.invoke(message); MethodParameter returnType = handlerMethod.getReturnType(); if (void.class == returnType.getParameterType()) {
public InvocationResult invoke(Message<?> message, Object... providedArgs) throws Exception { // NOSONAR if (this.invokerHandlerMethod != null) { return new InvocationResult(this.invokerHandlerMethod.invoke(message, providedArgs), null, this.invokerHandlerMethod.getMethod().getGenericReturnType()); } else if (this.delegatingHandler.hasDefaultHandler()) { // Needed to avoid returning raw Message which matches Object Object[] args = new Object[providedArgs.length + 1]; args[0] = message.getPayload(); System.arraycopy(providedArgs, 0, args, 1, providedArgs.length); return this.delegatingHandler.invoke(message, args); } else { return this.delegatingHandler.invoke(message, providedArgs); } }
@Override public void handleMessage(Message<?> message) throws MessagingException { InvocableHandlerMethod handlerMethod = getHandlerMethod(); try { handlerMethod.invoke(message); } catch (MessagingException ex) { throw new ListenerExecutionFailedException( createMessagingErrorMessage("Listener method could not be invoked with incoming message"), ex); } catch (Exception ex) { throw new ListenerExecutionFailedException( "Listener method '" + handlerMethod.getMethod().toGenericString() + "' threw exception", ex); } }
@SuppressWarnings("unchecked") public <T> T invoke(ParametersWrapper parameters) throws Exception { Message<?> message = parameters.getMessage(); if (this.canProcessMessageList) { message = new MutableMessage<>(parameters.getMessages(), parameters.getHeaders()); } return (T) this.invocableHandlerMethod.invoke(message); }
@Override public void handleMessage(Message<?> message) throws MessagingException { InvocableHandlerMethod handlerMethod = getHandlerMethod(); try { handlerMethod.invoke(message); } catch (MessagingException ex) { throw new ListenerExecutionFailedException( createMessagingErrorMessage("Listener method could not be invoked with incoming message"), ex); } catch (Exception ex) { throw new ListenerExecutionFailedException( "Listener method '" + handlerMethod.getMethod().toGenericString() + "' threw exception", ex); } }