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 Async async = findAnnotation(methodInvocation);
041
042 if (async == _nullAsync) {
043 return null;
044 }
045
046 Method method = methodInvocation.getMethod();
047
048 if (method.getReturnType() != void.class) {
049 if (_log.isWarnEnabled()) {
050 _log.warn(
051 "Async annotation on method " + method.getName() +
052 " does not return void");
053 }
054
055 return null;
056 }
057
058 String destinationName = null;
059
060 if ((_destinationNames != null) && !_destinationNames.isEmpty()) {
061 Object thisObject = methodInvocation.getThis();
062
063 destinationName = _destinationNames.get(thisObject.getClass());
064 }
065
066 if (destinationName == null) {
067 destinationName = _defaultDestinationName;
068 }
069
070 MessageBusUtil.sendMessage(
071 destinationName,
072 new Runnable() {
073
074 public void run() {
075 try {
076 nextMethodInterceptor.invoke(methodInvocation);
077 }
078 catch (Throwable t) {
079 throw new RuntimeException(t);
080 }
081 }
082
083 @Override
084 public String toString() {
085 return methodInvocation.toString();
086 }
087
088 });
089
090 return nullResult;
091 }
092
093 public String getDefaultDestinationName() {
094 return _defaultDestinationName;
095 }
096
097 @Override
098 public Async getNullAnnotation() {
099 return _nullAsync;
100 }
101
102 public void setDefaultDestinationName(String defaultDestinationName) {
103 _defaultDestinationName = defaultDestinationName;
104 }
105
106 public void setDestinationNames(Map<Class<?>, String> destinationNames) {
107 _destinationNames = destinationNames;
108 }
109
110 private static Log _log = LogFactoryUtil.getLog(AsyncAdvice.class);
111
112 private static Async _nullAsync =
113 new Async() {
114
115 public Class<? extends Annotation> annotationType() {
116 return Async.class;
117 }
118
119 };
120
121 private String _defaultDestinationName;
122 private Map<Class<?>, String> _destinationNames;
123
124 }