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