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.proxy.TargetedInvocationHandler; |
21 | |
import org.kuali.rice.core.impl.proxy.BaseInvocationHandler; |
22 | |
import org.kuali.rice.core.impl.resourceloader.ContextClassLoaderProxy; |
23 | |
import org.kuali.rice.core.impl.resourceloader.ContextClassLoaderProxy; |
24 | |
import org.kuali.rice.core.util.ClassLoaderUtils; |
25 | |
import org.kuali.rice.ksb.messaging.AsynchronousCall; |
26 | |
import org.kuali.rice.ksb.messaging.PersistedMessageBO; |
27 | |
import org.kuali.rice.ksb.messaging.RemotedServiceHolder; |
28 | |
import org.kuali.rice.ksb.messaging.ServiceInfo; |
29 | |
import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJob; |
30 | |
import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJobListener; |
31 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
32 | |
import org.quartz.*; |
33 | |
|
34 | |
import java.io.Serializable; |
35 | |
import java.lang.reflect.Method; |
36 | |
import java.lang.reflect.Proxy; |
37 | |
import java.sql.Timestamp; |
38 | |
import java.util.Calendar; |
39 | |
import java.util.List; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public class DelayedAsynchronousServiceCallProxy extends BaseInvocationHandler implements TargetedInvocationHandler { |
48 | |
|
49 | 0 | private static final Logger LOG = Logger.getLogger(DelayedAsynchronousServiceCallProxy.class); |
50 | |
|
51 | |
List<RemotedServiceHolder> serviceDefs; |
52 | |
private Serializable context; |
53 | |
private String value1; |
54 | |
private String value2; |
55 | |
private long delayMilliseconds; |
56 | |
|
57 | |
protected DelayedAsynchronousServiceCallProxy(List<RemotedServiceHolder> serviceDefs, Serializable context, |
58 | 0 | String value1, String value2, long delayMilliseconds) { |
59 | 0 | this.serviceDefs = serviceDefs; |
60 | 0 | this.context = context; |
61 | 0 | this.value1 = value1; |
62 | 0 | this.value2 = value2; |
63 | 0 | this.delayMilliseconds = delayMilliseconds; |
64 | 0 | } |
65 | |
|
66 | |
public static Object createInstance(List<RemotedServiceHolder> serviceDefs, Serializable context, String value1, |
67 | |
String value2, long delayMilliseconds) { |
68 | 0 | if (serviceDefs == null || serviceDefs.isEmpty()) { |
69 | 0 | throw new RuntimeException("Cannot create service proxy, no service(s) passed in."); |
70 | |
} |
71 | |
try { |
72 | 0 | return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy |
73 | |
.getInterfacesToProxy(serviceDefs.get(0).getService()), |
74 | |
new DelayedAsynchronousServiceCallProxy(serviceDefs, context, value1, value2, delayMilliseconds)); |
75 | 0 | } catch (Exception e) { |
76 | 0 | throw new RiceRuntimeException(e); |
77 | |
} |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
protected Object invokeInternal(Object proxy, Method method, Object[] arguments) throws Throwable { |
82 | |
|
83 | 0 | AsynchronousCall methodCall = null; |
84 | 0 | PersistedMessageBO message = null; |
85 | 0 | synchronized (this) { |
86 | |
|
87 | |
|
88 | 0 | for (RemotedServiceHolder remotedServiceHolder : this.serviceDefs) { |
89 | 0 | ServiceInfo serviceInfo = remotedServiceHolder.getServiceInfo(); |
90 | 0 | methodCall = new AsynchronousCall(method.getParameterTypes(), arguments, serviceInfo, method.getName(), |
91 | |
null, this.context); |
92 | 0 | message = KSBServiceLocator.getRouteQueueService().getMessage(serviceInfo, methodCall); |
93 | 0 | message.setValue1(this.value1); |
94 | 0 | message.setValue2(this.value2); |
95 | 0 | Calendar now = Calendar.getInstance(); |
96 | 0 | now.add(Calendar.MILLISECOND, (int) delayMilliseconds); |
97 | 0 | message.setQueueDate(new Timestamp(now.getTimeInMillis())); |
98 | 0 | scheduleMessage(message); |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | 0 | if (serviceInfo.getServiceDefinition().getQueue()) { |
104 | 0 | break; |
105 | |
} |
106 | 0 | } |
107 | 0 | } |
108 | 0 | return null; |
109 | |
} |
110 | |
|
111 | |
protected void scheduleMessage(PersistedMessageBO message) throws SchedulerException { |
112 | 0 | LOG.debug("Scheduling execution of a delayed asynchronous message."); |
113 | 0 | Scheduler scheduler = KSBServiceLocator.getScheduler(); |
114 | 0 | JobDataMap jobData = new JobDataMap(); |
115 | 0 | jobData.put(MessageServiceExecutorJob.MESSAGE_KEY, message); |
116 | 0 | JobDetail jobDetail = new JobDetail("Delayed_Asynchronous_Call-" + Math.random(), "Delayed_Asynchronous_Call", |
117 | |
MessageServiceExecutorJob.class); |
118 | 0 | jobDetail.setJobDataMap(jobData); |
119 | 0 | jobDetail.addJobListener(MessageServiceExecutorJobListener.NAME); |
120 | 0 | Trigger trigger = new SimpleTrigger("Delayed_Asynchronous_Call_Trigger-" + Math.random(), |
121 | |
"Delayed_Asynchronous_Call", message.getQueueDate()); |
122 | 0 | trigger.setJobDataMap(jobData); |
123 | 0 | scheduler.scheduleJob(jobDetail, trigger); |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public Object getTarget() { |
131 | 0 | return this.serviceDefs; |
132 | |
} |
133 | |
|
134 | |
public List<RemotedServiceHolder> getServiceDefs() { |
135 | 0 | return this.serviceDefs; |
136 | |
} |
137 | |
|
138 | |
public void setServiceDefs(List<RemotedServiceHolder> serviceDefs) { |
139 | 0 | this.serviceDefs = serviceDefs; |
140 | 0 | } |
141 | |
|
142 | |
} |