Skip to content

Instantly share code, notes, and snippets.

@bbalakriz
Created November 4, 2022 09:22
Show Gist options
  • Save bbalakriz/b103b506917c982c8d303256e06290dd to your computer and use it in GitHub Desktop.
Save bbalakriz/b103b506917c982c8d303256e06290dd to your computer and use it in GitHub Desktop.
package com.bala.housekeeper;
import java.util.Hashtable;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.jbpm.process.core.timer.TimerServiceRegistry;
public class TimerCleaner{
static final String ETS_JNDI_NAME = "java:module/EJBTimerScheduler";
static final String JOB_HANDLE_CLASS_NAME = "org.drools.core.time.JobHandle";
public TimerCleaner() {
}
public static void removeOrphanedTimers(ArrayList<String> uuids){
try {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
Object ejbTimerScheduler = context.lookup(ETS_JNDI_NAME);
Method getTimerByNameMethod = null;
Object globalJpaTimerJobInstance = null;
Method getJobHandleMethod = null;
Object jobHandle = null;
Method removeJobMethod = null;
final Class jobHandleClass = Class.forName(JOB_HANDLE_CLASS_NAME);
if (null != ejbTimerScheduler){
getTimerByNameMethod = ejbTimerScheduler.getClass().getMethod("getTimerByName", String.class);
for(String uuid: uuids){
globalJpaTimerJobInstance = getTimerByNameMethod.invoke(ejbTimerScheduler, uuid);
System.out.println("Job info for " + uuid + " is: " + globalJpaTimerJobInstance);
if(null != globalJpaTimerJobInstance){
getJobHandleMethod = globalJpaTimerJobInstance.getClass().getMethod("getJobHandle");
jobHandle = getJobHandleMethod.invoke(globalJpaTimerJobInstance);
removeJobMethod = ejbTimerScheduler.getClass().getMethod("removeJob", jobHandleClass);
System.out.println("EJB timer removal status for " + uuid + ": " + removeJobMethod.invoke(ejbTimerScheduler, jobHandle));
}
}
}
else{
System.out.println("EjbTimerScheduler instance not found: " + ejbTimerScheduler);
}
} catch (NamingException | NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment