All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jeesuite.scheduler.JobContext Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
/**
 * 
 */
package com.jeesuite.scheduler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.jeesuite.common.util.NodeNameHolder;
import com.jeesuite.scheduler.helper.ConsistencyHash;
import com.jeesuite.scheduler.registry.NullJobRegistry;

/**
 * @description 
* @author vakin * @date 2016年8月19日 */ public class JobContext { private static JobContext context = new JobContext(); private Set activeNodes = new HashSet(); private ConsistencyHash hash = new ConsistencyHash(); private String groupName; private Map allJobs = new HashMap<>(); private ConfigPersistHandler configPersistHandler; private TaskRetryProcessor retryProcessor; private JobLogPersistHandler jobLogPersistHandler; private JobRegistry registry; private ExecutorService syncExecutor = Executors.newFixedThreadPool(1); public void startRetryProcessor(){ if(retryProcessor == null){ synchronized (context) { if(retryProcessor != null)return; retryProcessor = new TaskRetryProcessor(1); } } } public static JobContext getContext() { return context; } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } public String getNodeId() { return NodeNameHolder.getNodeId(); } public ConfigPersistHandler getConfigPersistHandler() { return configPersistHandler; } public void setConfigPersistHandler(ConfigPersistHandler configPersistHandler) { this.configPersistHandler = configPersistHandler; } public JobLogPersistHandler getJobLogPersistHandler() { return jobLogPersistHandler; } public void setJobLogPersistHandler(JobLogPersistHandler jobLogPersistHandler) { this.jobLogPersistHandler = jobLogPersistHandler; } public JobRegistry getRegistry() { if(registry == null){ registry = new NullJobRegistry(); } return registry; } public void setRegistry(JobRegistry registry) { this.registry = registry; } public TaskRetryProcessor getRetryProcessor() { return retryProcessor; } public void refreshNodes(List nodes){ activeNodes.clear(); activeNodes.addAll(nodes); if(activeNodes.isEmpty())return; hash.refresh(nodes); } public void addNode(String node){ activeNodes.add(node); hash.refresh(new ArrayList<>(activeNodes)); } public void removeNode(String node){ activeNodes.remove(node); if(activeNodes.isEmpty())return; hash.refresh(new ArrayList<>(activeNodes)); } public boolean matchCurrentNode(Object shardFactor){ if(activeNodes.size() == 1)return true; String expectNodeId = hash.getAssignedRealNode(shardFactor); return expectNodeId.equals(getNodeId()); } public void addJob(AbstractJob job){ String key = job.group + ":" + job.jobName; allJobs.put(key, job); } public Map getAllJobs() { return allJobs; } public Set getActiveNodes() { return activeNodes; } public ExecutorService getSyncExecutor() { return syncExecutor; } public void submitSyncTask(Runnable task){ syncExecutor.execute(task); } public void close(){ if(retryProcessor != null){ retryProcessor.close(); } syncExecutor.shutdown(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy