/** * 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); } }
private String createMessagingErrorMessage(String description) { InvocableHandlerMethod handlerMethod = getHandlerMethod(); StringBuilder sb = new StringBuilder(description).append("\n") .append("Endpoint handler details:\n") .append("Method [").append(handlerMethod.getMethod()).append("]\n") .append("Bean [").append(handlerMethod.getBean()).append("]\n"); return sb.toString(); }
InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod); if (this.handlerMethodLogger != null) { invocable.setLogger(this.handlerMethodLogger); invocable.setMessageMethodArgumentResolvers(this.argumentResolvers); try { Object returnValue = invocable.invoke(message); MethodParameter returnType = handlerMethod.getReturnType(); if (void.class == returnType.getParameterType()) {
@Override public InvocableHandlerMethod createInvocableHandlerMethod(Object bean, Method method) { InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(bean, method); handlerMethod.setMessageMethodArgumentResolvers(this.argumentResolvers); return handlerMethod; }
ReflectionUtils.makeAccessible(getBridgedMethod()); try { return getBridgedMethod().invoke(getBean(), args); assertTargetBean(getBridgedMethod(), getBean(), args); String text = (ex.getMessage() != null ? ex.getMessage() : "Illegal argument"); throw new IllegalStateException(formatInvokeError(text, args), ex); throw new IllegalStateException(formatInvokeError("Invocation failure", args), targetException);
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); } }
@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"); }
HandlerMethod(InvocableHandlerMethod invocableHandlerMethod, boolean canProcessMessageList) { this.invocableHandlerMethod = invocableHandlerMethod; this.canProcessMessageList = canProcessMessageList; this.expressionString = generateExpression(this.invocableHandlerMethod.getMethod()); }
@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); }
protected InvocableHandlerMethod findHandlerForPayload(Class<? extends Object> payloadClass) { InvocableHandlerMethod result = null; for (InvocableHandlerMethod handler : this.handlers) { if (matchHandlerMethod(payloadClass, handler)) { if (result != null) { boolean resultIsDefault = result.equals(this.defaultHandler); if (!handler.equals(this.defaultHandler) && !resultIsDefault) { throw new KafkaException("Ambiguous methods for payload type: " + payloadClass + ": " + result.getMethod().getName() + " and " + handler.getMethod().getName()); } if (!resultIsDefault) { continue; // otherwise replace the result with the actual match } } result = handler; } } return result != null ? result : this.defaultHandler; }
@Override protected Object handleRequestMessage(Message<?> requestMessage) { try { return this.invocableHandlerMethod.invoke(requestMessage); } catch (Exception e) { if (e instanceof MessagingException) { throw (MessagingException) e; } else { throw new MessagingException(requestMessage, "Exception thrown while invoking " + this.invocableHandlerMethod.getShortLogMessage(), e); } } } }
public Object getBean() { if (this.invokerHandlerMethod != null) { return this.invokerHandlerMethod.getBean(); } else { return this.delegatingHandler.getBean(); } }
return new InvocableHandlerMethod(handlerMethod.getBean(), method); method = resolver.resolveMethod(exception); if (method != null) { return new InvocableHandlerMethod(advice.resolveBean(), method);
@Override protected Object preProcessResponse(Object result) { MethodParameter returnType = getHandlerMethod().getReturnType(); if (result instanceof Message) { return MessageBuilder.fromMessage((Message<?>) result) .setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType).build(); } return MessageBuilder.withPayload(result).setHeader( AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType).build(); }
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); } }
@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); }
ReflectionUtils.makeAccessible(getBridgedMethod()); try { return getBridgedMethod().invoke(getBean(), args); assertTargetBean(getBridgedMethod(), getBean(), args); String text = (ex.getMessage() != null ? ex.getMessage() : "Illegal argument"); throw new IllegalStateException(formatInvokeError(text, args), ex); throw new IllegalStateException(formatInvokeError("Invocation failure", args), targetException);
/** * Return a string representation of the method that will be invoked for this payload. * @param payload the payload. * @return the method name. */ public String getMethodNameFor(Object payload) { InvocableHandlerMethod handlerForPayload = getHandlerForPayload(payload.getClass()); return handlerForPayload == null ? "no match" : handlerForPayload.getMethod().toGenericString(); //NOSONAR }
@Override public InvocableHandlerMethod createInvocableHandlerMethod(Object bean, Method method) { InvocableHandlerMethod handlerMethod = new InvocableHandlerMethod(bean, method); handlerMethod.setMessageMethodArgumentResolvers(this.argumentResolvers); return handlerMethod; }
protected InvocableHandlerMethod findHandlerForPayload(Class<? extends Object> payloadClass) { InvocableHandlerMethod result = null; for (InvocableHandlerMethod handler : this.handlers) { if (matchHandlerMethod(payloadClass, handler)) { if (result != null) { boolean resultIsDefault = result.equals(this.defaultHandler); if (!handler.equals(this.defaultHandler) && !resultIsDefault) { throw new AmqpException("Ambiguous methods for payload type: " + payloadClass + ": " + result.getMethod().getName() + " and " + handler.getMethod().getName()); } if (!resultIsDefault) { continue; // otherwise replace the result with the actual match } } result = handler; } } return result != null ? result : this.defaultHandler; }