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.kernel.util.MethodTargetClassKey;
022 import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
023
024 import java.lang.annotation.Annotation;
025 import java.lang.reflect.Method;
026
027 import java.util.Map;
028
029 import org.aopalliance.intercept.MethodInvocation;
030
031
035 public class AsyncAdvice extends AnnotationChainableMethodAdvice<Async> {
036
037 public Object before(final MethodInvocation methodInvocation)
038 throws Throwable {
039
040 MethodTargetClassKey methodTargetClassKey = buildMethodTargetClassKey(
041 methodInvocation);
042
043 Async async = findAnnotation(methodTargetClassKey);
044
045 if (async == _nullAsync) {
046 return null;
047 }
048
049 Method method = methodTargetClassKey.getMethod();
050
051 if (method.getReturnType() != void.class) {
052 if (_log.isWarnEnabled()) {
053 _log.warn(
054 "Async annotation on method " + method.getName() +
055 " does not return void");
056 }
057
058 return null;
059 }
060
061 String destinationName = null;
062
063 if ((_destinationNames != null) && !_destinationNames.isEmpty()) {
064 destinationName = _destinationNames.get(
065 methodTargetClassKey.getTargetClass());
066 }
067
068 if (destinationName == null) {
069 destinationName = _defaultDestinationName;
070 }
071
072 MessageBusUtil.sendMessage(
073 destinationName,
074 new Runnable() {
075
076 public void run() {
077 try {
078 nextMethodInterceptor.invoke(methodInvocation);
079 }
080 catch (Throwable t) {
081 throw new RuntimeException(t);
082 }
083 }
084
085 public String toString() {
086 return methodInvocation.toString();
087 }
088
089 });
090
091 return nullResult;
092 }
093
094 public String getDefaultDestinationName() {
095 return _defaultDestinationName;
096 }
097
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 Async _nullAsync =
111 new Async() {
112
113 public Class<? extends Annotation> annotationType() {
114 return Async.class;
115 }
116
117 };
118
119 private static Log _log = LogFactoryUtil.getLog(AsyncAdvice.class);
120
121 private String _defaultDestinationName;
122 private Map<Class<?>, String> _destinationNames;
123
124 }