| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.ksb.messaging.serviceproxies; |
| 17 | |
|
| 18 | |
import org.apache.log4j.Logger; |
| 19 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
| 20 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
| 21 | |
import org.kuali.rice.core.api.util.reflect.BaseInvocationHandler; |
| 22 | |
import org.kuali.rice.core.api.util.reflect.TargetedInvocationHandler; |
| 23 | |
import org.kuali.rice.core.impl.resourceloader.ContextClassLoaderProxy; |
| 24 | |
import org.kuali.rice.ksb.api.bus.Endpoint; |
| 25 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
| 26 | |
import org.kuali.rice.ksb.api.messaging.AsynchronousCall; |
| 27 | |
import org.kuali.rice.ksb.api.messaging.AsynchronousCallback; |
| 28 | |
import org.kuali.rice.ksb.messaging.PersistedMessageBO; |
| 29 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
| 30 | |
import org.kuali.rice.ksb.util.KSBConstants; |
| 31 | |
|
| 32 | |
import java.io.Serializable; |
| 33 | |
import java.lang.reflect.Method; |
| 34 | |
import java.lang.reflect.Proxy; |
| 35 | |
import java.util.List; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public class AsynchronousServiceCallProxy extends BaseInvocationHandler implements TargetedInvocationHandler { |
| 46 | |
|
| 47 | 0 | private static final Logger LOG = Logger.getLogger(AsynchronousServiceCallProxy.class); |
| 48 | |
|
| 49 | |
private AsynchronousCallback callback; |
| 50 | |
|
| 51 | |
private List<Endpoint> endpoints; |
| 52 | |
|
| 53 | |
private Serializable context; |
| 54 | |
|
| 55 | |
private String value1; |
| 56 | |
|
| 57 | |
private String value2; |
| 58 | |
|
| 59 | |
protected AsynchronousServiceCallProxy(List<Endpoint> endpoints, AsynchronousCallback callback, |
| 60 | 0 | Serializable context, String value1, String value2) { |
| 61 | 0 | this.endpoints = endpoints; |
| 62 | 0 | this.callback = callback; |
| 63 | 0 | this.context = context; |
| 64 | 0 | this.value1 = value1; |
| 65 | 0 | this.value2 = value2; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public static Object createInstance(List<Endpoint> endpoints, AsynchronousCallback callback, |
| 69 | |
Serializable context, String value1, String value2) { |
| 70 | 0 | if (endpoints == null || endpoints.isEmpty()) { |
| 71 | 0 | throw new RuntimeException("Cannot create service proxy, no service(s) passed in."); |
| 72 | |
} |
| 73 | |
try { |
| 74 | 0 | return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy |
| 75 | |
.getInterfacesToProxy(endpoints.get(0).getService()), new AsynchronousServiceCallProxy( |
| 76 | |
endpoints, callback, context, value1, value2)); |
| 77 | 0 | } catch (Exception e) { |
| 78 | 0 | throw new RiceRuntimeException(e); |
| 79 | |
} |
| 80 | |
} |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
protected Object invokeInternal(Object proxy, Method method, Object[] arguments) throws Throwable { |
| 84 | |
|
| 85 | 0 | if (LOG.isDebugEnabled()) { |
| 86 | 0 | LOG.debug("creating messages for method invocation: " + method.getName()); |
| 87 | |
} |
| 88 | |
|
| 89 | 0 | AsynchronousCall methodCall = null; |
| 90 | 0 | PersistedMessageBO message = null; |
| 91 | 0 | synchronized (this) { |
| 92 | |
|
| 93 | |
|
| 94 | 0 | for (Endpoint endpoint : this.endpoints) { |
| 95 | 0 | ServiceConfiguration serviceConfiguration = endpoint.getServiceConfiguration(); |
| 96 | 0 | methodCall = new AsynchronousCall(method.getParameterTypes(), arguments, serviceConfiguration, method.getName(), |
| 97 | |
this.callback, this.context); |
| 98 | 0 | message = KSBServiceLocator.getMessageQueueService().getMessage(serviceConfiguration, methodCall); |
| 99 | 0 | message.setValue1(this.value1); |
| 100 | 0 | message.setValue2(this.value2); |
| 101 | 0 | saveMessage(message); |
| 102 | 0 | executeMessage(message); |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | 0 | if (serviceConfiguration.isQueue()) { |
| 108 | 0 | break; |
| 109 | |
} |
| 110 | 0 | } |
| 111 | 0 | } |
| 112 | 0 | if (LOG.isDebugEnabled()) { |
| 113 | 0 | LOG.debug("finished creating messages for method invocation: " + method.getName()); |
| 114 | |
} |
| 115 | 0 | return null; |
| 116 | |
} |
| 117 | |
|
| 118 | |
protected void saveMessage(PersistedMessageBO message) { |
| 119 | 0 | message.setQueueStatus(KSBConstants.ROUTE_QUEUE_ROUTING); |
| 120 | 0 | KSBServiceLocator.getMessageQueueService().save(message); |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
protected void executeMessage(PersistedMessageBO message) throws Exception { |
| 124 | 0 | MessageSender.sendMessage(message); |
| 125 | 0 | } |
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
public Object getTarget() { |
| 132 | 0 | return this.endpoints; |
| 133 | |
} |
| 134 | |
|
| 135 | |
public AsynchronousCallback getCallback() { |
| 136 | 0 | return this.callback; |
| 137 | |
} |
| 138 | |
|
| 139 | |
public void setCallback(AsynchronousCallback callback) { |
| 140 | 0 | this.callback = callback; |
| 141 | 0 | } |
| 142 | |
|
| 143 | |
} |