001
014
015 package com.liferay.portal.messaging.async;
016
017 import com.liferay.portal.bean.IdentifiableBeanInvokerUtil;
018 import com.liferay.portal.kernel.util.MethodHandler;
019
020 import java.io.Externalizable;
021 import java.io.IOException;
022 import java.io.ObjectInput;
023 import java.io.ObjectOutput;
024
025 import org.aopalliance.intercept.MethodInvocation;
026
027
030 public class AsyncRunnable implements Externalizable, Runnable {
031
032 public AsyncRunnable() {
033 }
034
035 public AsyncRunnable(MethodInvocation methodInvocation) {
036 _methodInvocation = methodInvocation;
037 }
038
039 @Override
040 public void readExternal(ObjectInput objectInput)
041 throws ClassNotFoundException, IOException {
042
043 _methodHandler = (MethodHandler)objectInput.readObject();
044 }
045
046 @Override
047 public void run() {
048 try {
049 if (_methodInvocation != null) {
050 _methodInvocation.proceed();
051 }
052 else {
053 AsyncInvokeThreadLocal.setEnabled(true);
054
055 try {
056 _methodHandler.invoke(null);
057 }
058 finally {
059 AsyncInvokeThreadLocal.setEnabled(false);
060 }
061 }
062 }
063 catch (Throwable t) {
064 throw new RuntimeException(t);
065 }
066 }
067
068 @Override
069 public void writeExternal(ObjectOutput objectOutput) throws IOException {
070 MethodHandler methodHandler = _methodHandler;
071
072 if (methodHandler == null) {
073 methodHandler = IdentifiableBeanInvokerUtil.createMethodHandler(
074 _methodInvocation);
075 }
076
077 objectOutput.writeObject(methodHandler);
078 }
079
080 private MethodHandler _methodHandler;
081 private MethodInvocation _methodInvocation;
082
083 }