001/** 002 * Copyright 2005-2017 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.ksb.messaging.serviceproxies; 017 018import org.apache.log4j.Logger; 019import org.kuali.rice.core.api.exception.RiceRuntimeException; 020import org.kuali.rice.core.api.util.ClassLoaderUtils; 021import org.kuali.rice.core.api.util.ContextClassLoaderProxy; 022import org.kuali.rice.core.api.util.reflect.BaseInvocationHandler; 023import org.kuali.rice.core.api.util.reflect.TargetedInvocationHandler; 024import org.kuali.rice.ksb.api.bus.Endpoint; 025import org.kuali.rice.ksb.api.bus.ServiceConfiguration; 026import org.kuali.rice.ksb.api.messaging.AsynchronousCall; 027import org.kuali.rice.ksb.messaging.PersistedMessageBO; 028import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJob; 029import org.kuali.rice.ksb.messaging.quartz.MessageServiceExecutorJobListener; 030import org.kuali.rice.ksb.service.KSBServiceLocator; 031import org.quartz.JobDataMap; 032import org.quartz.JobDetail; 033import org.quartz.Scheduler; 034import org.quartz.SchedulerException; 035import org.quartz.SimpleTrigger; 036import org.quartz.Trigger; 037import org.quartz.impl.JobDetailImpl; 038import org.quartz.impl.triggers.SimpleTriggerImpl; 039 040import java.io.Serializable; 041import java.lang.reflect.Method; 042import java.lang.reflect.Proxy; 043import java.sql.Timestamp; 044import java.util.Calendar; 045import java.util.List; 046 047 048/** 049 * A proxy which schedules a service to be executed asynchronously after some delay period. 050 * 051 * @author Kuali Rice Team (rice.collab@kuali.org) 052 */ 053public class DelayedAsynchronousServiceCallProxy extends BaseInvocationHandler implements TargetedInvocationHandler { 054 055 private static final Logger LOG = Logger.getLogger(DelayedAsynchronousServiceCallProxy.class); 056 057 List<Endpoint> endpoints; 058 private Serializable context; 059 private String value1; 060 private String value2; 061 private long delayMilliseconds; 062 063 protected DelayedAsynchronousServiceCallProxy(List<Endpoint> endpoints, Serializable context, 064 String value1, String value2, long delayMilliseconds) { 065 this.endpoints = endpoints; 066 this.context = context; 067 this.value1 = value1; 068 this.value2 = value2; 069 this.delayMilliseconds = delayMilliseconds; 070 } 071 072 public static Object createInstance(List<Endpoint> endpoints, Serializable context, String value1, 073 String value2, long delayMilliseconds) { 074 if (endpoints == null || endpoints.isEmpty()) { 075 throw new RuntimeException("Cannot create service proxy, no service(s) passed in."); 076 } 077 try { 078 return Proxy.newProxyInstance(ClassLoaderUtils.getDefaultClassLoader(), ContextClassLoaderProxy 079 .getInterfacesToProxy(endpoints.get(0).getService()), 080 new DelayedAsynchronousServiceCallProxy(endpoints, context, value1, value2, delayMilliseconds)); 081 } catch (Exception e) { 082 throw new RiceRuntimeException(e); 083 } 084 } 085 086 @Override 087 protected Object invokeInternal(Object proxy, Method method, Object[] arguments) throws Throwable { 088 // there are multiple service calls to make in the case of topics. 089 AsynchronousCall methodCall = null; 090 PersistedMessageBO message = null; 091 synchronized (this) { 092 // consider moving all this topic invocation stuff to the service 093 // invoker for speed reasons 094 for (Endpoint endpoint : this.endpoints) { 095 ServiceConfiguration serviceConfiguration = endpoint.getServiceConfiguration(); 096 methodCall = new AsynchronousCall(method.getParameterTypes(), arguments, serviceConfiguration, 097 method.getName(), null, this.context); 098 message = PersistedMessageBO.buildMessage(serviceConfiguration, methodCall); 099 message.setValue1(this.value1); 100 message.setValue2(this.value2); 101 Calendar now = Calendar.getInstance(); 102 now.add(Calendar.MILLISECOND, (int) delayMilliseconds); 103 message.setQueueDate(new Timestamp(now.getTimeInMillis())); 104 scheduleMessage(message); 105 // only do one iteration if this is a queue. The load balancing 106 // will be handled when the service is 107 // fetched by the MessageServiceInvoker through the GRL (and 108 // then through the RemoteResourceServiceLocatorImpl) 109 if (serviceConfiguration.isQueue()) { 110 break; 111 } 112 } 113 } 114 return null; 115 } 116 117 protected void scheduleMessage(PersistedMessageBO message) throws SchedulerException { 118 LOG.debug("Scheduling execution of a delayed asynchronous message."); 119 Scheduler scheduler = KSBServiceLocator.getScheduler(); 120 JobDataMap jobData = new JobDataMap(); 121 jobData.put(MessageServiceExecutorJob.MESSAGE_KEY, message); 122 123 JobDetailImpl jobDetail = new JobDetailImpl("Delayed_Asynchronous_Call-" + Math.random(), "Delayed_Asynchronous_Call", 124 MessageServiceExecutorJob.class); 125 jobDetail.setJobDataMap(jobData); 126 127 scheduler.getListenerManager().addJobListener( new MessageServiceExecutorJobListener()); 128 129 SimpleTriggerImpl trigger = new SimpleTriggerImpl("Delayed_Asynchronous_Call_Trigger-" + Math.random(), 130 "Delayed_Asynchronous_Call", message.getQueueDate()); 131 132 trigger.setJobDataMap(jobData);// 1.6 bug required or derby will choke 133 scheduler.scheduleJob(jobDetail, trigger); 134 } 135 136 /** 137 * Returns the List<RemotedServiceHolder> of asynchronous services which will be invoked by calls to this proxy. 138 * This is a List because, in the case of Topics, there can be more than one service invoked. 139 */ 140 public Object getTarget() { 141 return this.endpoints; 142 } 143 144}