package org.joget.workflow.util; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Properties; import java.util.Set; import org.joget.commons.util.DynamicDataSourceManager; import org.joget.commons.util.HostManager; import org.joget.commons.util.LogUtil; import org.joget.workflow.model.service.WorkflowManager; import org.joget.workflow.shark.DeadlineChecker; public class DeadlineThreadManager { public static final long INTERVAL_MINIMUM = 1000; // 1 second private static Map<String, DeadlineChecker> threadMap = Collections.synchronizedMap(new HashMap<String, DeadlineChecker>()); public static void initThreads(WorkflowManager workflowManager) { // stop and clear all threads for (String profile : threadMap.keySet()) { stopThread(profile); } threadMap.clear(); if (HostManager.isVirtualHostEnabled()) { // find all profiles and start threads Properties profiles = DynamicDataSourceManager.getProfileProperties(); Set<String> profileSet = new HashSet(profiles.values()); for (String profile : profileSet) { HostManager.setCurrentProfile(profile); workflowManager.internalUpdateDeadlineChecker(); } HostManager.setCurrentProfile(null); } else { // start current profile workflowManager.internalUpdateDeadlineChecker(); } } public static void startThread(long interval) { String profile = DynamicDataSourceManager.getCurrentProfile(); if (interval >= INTERVAL_MINIMUM) { DeadlineChecker thread = getThread(profile); if (thread == null) { thread = new DeadlineChecker(profile, null, interval, 100, 10, true); threadMap.put(profile, thread); } else { thread.setDelay(interval); if (thread.isStopped()) { LogUtil.info(DeadlineThreadManager.class.getName(), "Starting DeadlineChecker for profile " + profile); thread.startChecker(); } } } else { stopThread(profile); } } protected static DeadlineChecker getThread(String profile) { DeadlineChecker thread = (DeadlineChecker)threadMap.get(profile); return thread; } protected static void stopThread(String profile) { DeadlineChecker thread = getThread(profile); if (thread != null) { LogUtil.info(DeadlineThreadManager.class.getName(), "Stopping DeadlineChecker for profile " + profile); thread.stopChecker(); threadMap.remove(profile); } } }