use of org.springframework.integration.router.AbstractMessageRouter in project spring-integration by spring-projects.
the class RouterFactoryBean method createMethodInvokingHandler.
@Override
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
Assert.notNull(targetObject, "target object must not be null");
AbstractMessageRouter router = this.extractTypeIfPossible(targetObject, AbstractMessageRouter.class);
if (router == null) {
if (targetObject instanceof MessageHandler && this.noRouterAttributesProvided() && this.methodIsHandleMessageOrEmpty(targetMethodName)) {
return (MessageHandler) targetObject;
}
router = this.createMethodInvokingRouter(targetObject, targetMethodName);
this.configureRouter(router);
} else {
Assert.isTrue(!StringUtils.hasText(targetMethodName), "target method should not be provided when the target " + "object is an implementation of AbstractMessageRouter");
this.configureRouter(router);
if (targetObject instanceof MessageHandler) {
return (MessageHandler) targetObject;
}
}
return router;
}
use of org.springframework.integration.router.AbstractMessageRouter in project spring-integration by spring-projects.
the class AbstractMethodAnnotationPostProcessor method postProcess.
@Override
public Object postProcess(Object bean, String beanName, Method method, List<Annotation> annotations) {
if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
try {
resolveTargetBeanFromMethodWithBeanAnnotation(method);
} catch (NoSuchBeanDefinitionException e) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Skipping endpoint creation; " + e.getMessage() + "; perhaps due to some '@Conditional' annotation.");
}
return null;
}
}
List<Advice> adviceChain = extractAdviceChain(beanName, annotations);
MessageHandler handler = createHandler(bean, method, annotations);
if (!CollectionUtils.isEmpty(adviceChain) && handler instanceof AbstractReplyProducingMessageHandler) {
((AbstractReplyProducingMessageHandler) handler).setAdviceChain(adviceChain);
}
if (handler instanceof Orderable) {
Order orderAnnotation = AnnotationUtils.findAnnotation(method, Order.class);
if (orderAnnotation != null) {
((Orderable) handler).setOrder(orderAnnotation.value());
}
}
if (handler instanceof AbstractMessageProducingHandler || handler instanceof AbstractMessageRouter) {
String sendTimeout = MessagingAnnotationUtils.resolveAttribute(annotations, "sendTimeout", String.class);
if (sendTimeout != null) {
Long value = Long.valueOf(this.beanFactory.resolveEmbeddedValue(sendTimeout));
if (handler instanceof AbstractMessageProducingHandler) {
((AbstractMessageProducingHandler) handler).setSendTimeout(value);
} else {
((AbstractMessageRouter) handler).setSendTimeout(value);
}
}
}
boolean handlerExists = false;
if (this.beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
Object handlerBean = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
handlerExists = handlerBean != null && handler == handlerBean;
}
if (!handlerExists) {
String handlerBeanName = generateHandlerBeanName(beanName, method);
if (handler instanceof ReplyProducingMessageHandlerWrapper && StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) {
handlerBeanName = handlerBeanName + ".wrapper";
}
this.beanFactory.registerSingleton(handlerBeanName, handler);
handler = (MessageHandler) this.beanFactory.initializeBean(handler, handlerBeanName);
}
if (AnnotatedElementUtils.isAnnotated(method, IdempotentReceiver.class.getName()) && !AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
String[] interceptors = AnnotationUtils.getAnnotation(method, IdempotentReceiver.class).value();
for (String interceptor : interceptors) {
DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor();
advisor.setAdviceBeanName(interceptor);
NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
pointcut.setMappedName("handleMessage");
advisor.setPointcut(pointcut);
advisor.setBeanFactory(this.beanFactory);
if (handler instanceof Advised) {
((Advised) handler).addAdvisor(advisor);
} else {
ProxyFactory proxyFactory = new ProxyFactory(handler);
proxyFactory.addAdvisor(advisor);
handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader());
}
}
}
if (!CollectionUtils.isEmpty(adviceChain)) {
for (Advice advice : adviceChain) {
if (advice instanceof HandleMessageAdvice) {
NameMatchMethodPointcutAdvisor handlerAdvice = new NameMatchMethodPointcutAdvisor(advice);
handlerAdvice.addMethodName("handleMessage");
if (handler instanceof Advised) {
((Advised) handler).addAdvisor(handlerAdvice);
} else {
ProxyFactory proxyFactory = new ProxyFactory(handler);
proxyFactory.addAdvisor(handlerAdvice);
handler = (MessageHandler) proxyFactory.getProxy(this.beanFactory.getBeanClassLoader());
}
}
}
}
AbstractEndpoint endpoint = createEndpoint(handler, method, annotations);
if (endpoint != null) {
return endpoint;
}
return handler;
}
use of org.springframework.integration.router.AbstractMessageRouter in project spring-integration by spring-projects.
the class RouterAnnotationPostProcessor method createHandler.
@Override
protected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) {
AbstractMessageRouter router;
if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
Object target = this.resolveTargetBeanFromMethodWithBeanAnnotation(method);
router = this.extractTypeIfPossible(target, AbstractMessageRouter.class);
if (router == null) {
if (target instanceof MessageHandler) {
Assert.isTrue(this.routerAttributesProvided(annotations), "'defaultOutputChannel', 'applySequence', " + "'ignoreSendFailures', 'resolutionRequired', 'channelMappings', 'prefix' and 'suffix' " + "can be applied to 'AbstractMessageRouter' implementations, but target handler is: " + target.getClass());
return (MessageHandler) target;
} else {
router = new MethodInvokingRouter(target);
}
} else {
checkMessageHandlerAttributes(resolveTargetBeanName(method), annotations);
return router;
}
} else {
router = new MethodInvokingRouter(bean, method);
}
String defaultOutputChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, "defaultOutputChannel", String.class);
if (StringUtils.hasText(defaultOutputChannelName)) {
router.setDefaultOutputChannelName(defaultOutputChannelName);
}
String applySequence = MessagingAnnotationUtils.resolveAttribute(annotations, "applySequence", String.class);
if (StringUtils.hasText(applySequence)) {
router.setApplySequence(Boolean.parseBoolean(this.beanFactory.resolveEmbeddedValue(applySequence)));
}
String ignoreSendFailures = MessagingAnnotationUtils.resolveAttribute(annotations, "ignoreSendFailures", String.class);
if (StringUtils.hasText(ignoreSendFailures)) {
router.setIgnoreSendFailures(Boolean.parseBoolean(this.beanFactory.resolveEmbeddedValue(ignoreSendFailures)));
}
if (this.routerAttributesProvided(annotations)) {
MethodInvokingRouter methodInvokingRouter = (MethodInvokingRouter) router;
String resolutionRequired = MessagingAnnotationUtils.resolveAttribute(annotations, "resolutionRequired", String.class);
if (StringUtils.hasText(resolutionRequired)) {
String resolutionRequiredValue = this.beanFactory.resolveEmbeddedValue(resolutionRequired);
if (StringUtils.hasText(resolutionRequiredValue)) {
methodInvokingRouter.setResolutionRequired(Boolean.parseBoolean(resolutionRequiredValue));
}
}
String prefix = MessagingAnnotationUtils.resolveAttribute(annotations, "prefix", String.class);
if (StringUtils.hasText(prefix)) {
methodInvokingRouter.setPrefix(this.beanFactory.resolveEmbeddedValue(prefix));
}
String suffix = MessagingAnnotationUtils.resolveAttribute(annotations, "suffix", String.class);
if (StringUtils.hasText(suffix)) {
methodInvokingRouter.setSuffix(this.beanFactory.resolveEmbeddedValue(suffix));
}
String[] channelMappings = MessagingAnnotationUtils.resolveAttribute(annotations, "channelMappings", String[].class);
if (!ObjectUtils.isEmpty(channelMappings)) {
StringBuilder mappings = new StringBuilder();
for (String channelMapping : channelMappings) {
mappings.append(channelMapping).append("\n");
}
Properties properties = (Properties) this.conversionService.convert(mappings.toString(), TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Properties.class));
methodInvokingRouter.replaceChannelMappings(properties);
}
}
return router;
}
Aggregations