001
014
015 package com.liferay.portal.messaging.async;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.messaging.MessageBusUtil;
020 import com.liferay.portal.kernel.messaging.async.Async;
021 import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
022
023 import java.lang.annotation.Annotation;
024 import java.lang.reflect.Method;
025
026 import java.util.Map;
027
028 import org.aopalliance.intercept.MethodInvocation;
029
030
034 public class AsyncAdvice extends AnnotationChainableMethodAdvice<Async> {
035
036 @Override
037 public Object before(final MethodInvocation methodInvocation)
038 throws Throwable {
039
040 if (AsyncInvokeThreadLocal.isEnabled()) {
041 return null;
042 }
043
044 Async async = findAnnotation(methodInvocation);
045
046 if (async == _nullAsync) {
047 return null;
048 }
049
050 Method method = methodInvocation.getMethod();
051
052 if (method.getReturnType() != void.class) {
053 if (_log.isWarnEnabled()) {
054 _log.warn(
055 "Async annotation on method " + method.getName() +
056 " does not return void");
057 }
058
059 return null;
060 }
061
062 String destinationName = null;
063
064 if ((_destinationNames != null) && !_destinationNames.isEmpty()) {
065 Object thisObject = methodInvocation.getThis();
066
067 destinationName = _destinationNames.get(thisObject.getClass());
068 }
069
070 if (destinationName == null) {
071 destinationName = _defaultDestinationName;
072 }
073
074 MessageBusUtil.sendMessage(
075 destinationName, new AsyncProcessCallable(methodInvocation));
076
077 return nullResult;
078 }
079
080 public String getDefaultDestinationName() {
081 return _defaultDestinationName;
082 }
083
084 @Override
085 public Async getNullAnnotation() {
086 return _nullAsync;
087 }
088
089 public void setDefaultDestinationName(String defaultDestinationName) {
090 _defaultDestinationName = defaultDestinationName;
091 }
092
093 public void setDestinationNames(Map<Class<?>, String> destinationNames) {
094 _destinationNames = destinationNames;
095 }
096
097 private static Log _log = LogFactoryUtil.getLog(AsyncAdvice.class);
098
099 private static Async _nullAsync =
100 new Async() {
101
102 @Override
103 public Class<? extends Annotation> annotationType() {
104 return Async.class;
105 }
106
107 };
108
109 private String _defaultDestinationName;
110 private Map<Class<?>, String> _destinationNames;
111
112 }