use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.
the class TimeoutSchedulerExecutor method createScheduler.
public static synchronized ScheduledThreadPoolExecutor createScheduler(String executorName) {
if (!scheduledThreadPoolExecutorMap.containsKey(executorName)) {
ScheduledThreadPoolExecutor timeoutExecutor = new ScheduledThreadPoolExecutor(Math.max(2, Runtime.getRuntime().availableProcessors() / 2), new SaturnThreadFactory(executorName + "-timeout-watchdog", false));
timeoutExecutor.setRemoveOnCancelPolicy(true);
scheduledThreadPoolExecutorMap.put(executorName, timeoutExecutor);
return timeoutExecutor;
}
return scheduledThreadPoolExecutorMap.get(executorName);
}
use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.
the class ThreadA method main.
public static void main(String[] args) throws InterruptedException, ExecutionException {
ScheduledThreadPoolExecutor timeoutExecutor = new ScheduledThreadPoolExecutor(Math.max(2, Runtime.getRuntime().availableProcessors() / 2), new SaturnThreadFactory("-timeout-watchdog"));
timeoutExecutor.setRemoveOnCancelPolicy(true);
ExecutorService executorService = Executors.newSingleThreadExecutor(new SaturnThreadFactory("test"));
ThreadA a = new ThreadA();
Future<String> future = executorService.submit(a);
timeoutExecutor.schedule(new TimeoutHandleTask(a), 2, TimeUnit.SECONDS);
String res = future.get();
System.out.println("res:" + res);
a = new ThreadA();
future = executorService.submit(a);
res = future.get();
System.out.println("res:" + res);
}
use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.
the class ShardingListenerManager method start.
@Override
public void start() {
if (necessaryWatcher != null) {
executorService = Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-" + jobName + "-registerNecessaryWatcher", false));
shardingService.registerNecessaryWatcher(necessaryWatcher);
connectionStateListener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.CONNECTED) || (newState == ConnectionState.RECONNECTED)) {
// maybe node data have changed, so doBusiness whatever, it's okay for MSG job
LogUtils.info(log, jobName, "state change to {}, trigger doBusiness and register necessary watcher.", newState);
doBusiness();
registerNecessaryWatcher();
}
}
};
addConnectionStateListener(connectionStateListener);
}
}
use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.
the class RestartAndDumpService method initDump.
private void initDump() throws Exception {
dumpES = Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-dump-watcher-thread", false));
final String nodePath = SaturnExecutorsNode.EXECUTORS_ROOT + "/" + executorName + "/dump";
coordinatorRegistryCenter.remove(nodePath);
dumpNC = new NodeCache(curatorFramework, nodePath);
dumpNC.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
// Watch create, update event
if (dumpNC.getCurrentData() != null) {
LogUtils.info(log, LogEvents.ExecutorEvent.DUMP, "The executor {} dump event is received", executorName);
dumpES.execute(new Runnable() {
@Override
public void run() {
executeRestartOrDumpCmd("dump", LogEvents.ExecutorEvent.DUMP);
coordinatorRegistryCenter.remove(nodePath);
}
});
}
}
});
dumpNC.start(false);
}
use of com.vip.saturn.job.threads.SaturnThreadFactory in project Saturn by vipshop.
the class SaturnExecutorService method registerCallbackAndStartExistingJob.
/**
* Register NewJobCallback, and async start existing jobs. The TreeCache will publish the create events for already
* existing jobs.
*/
public void registerCallbackAndStartExistingJob(final ScheduleNewJobCallback callback) throws Exception {
jobsTreeCache = TreeCache.newBuilder((CuratorFramework) coordinatorRegistryCenter.getRawClient(), JobNodePath.ROOT).setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-$Jobs-watcher", false)), true)).setMaxDepth(1).build();
jobsTreeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event == null) {
return;
}
ChildData data = event.getData();
if (data == null) {
return;
}
String path = data.getPath();
if (path == null || path.equals(JobNodePath.ROOT)) {
return;
}
Type type = event.getType();
if (type == null || !type.equals(Type.NODE_ADDED)) {
return;
}
String jobName = StringUtils.substringAfterLast(path, "/");
String jobClassPath = JobNodePath.getNodeFullPath(jobName, ConfigurationNode.JOB_CLASS);
// wait 5 seconds at most until jobClass created.
for (int i = 0; i < WAIT_JOBCLASS_ADDED_COUNT; i++) {
if (client.checkExists().forPath(jobClassPath) == null) {
Thread.sleep(200);
continue;
}
log.info("new job: {} 's jobClass created event received", jobName);
if (!jobNames.contains(jobName)) {
if (callback.call(jobName)) {
jobNames.add(jobName);
log.info("the job {} initialize successfully", jobName);
} else {
log.warn("the job {} initialize fail", jobName);
}
} else {
log.warn("the job {} is unnecessary to initialize, because it's already existing", jobName);
}
break;
}
}
});
jobsTreeCache.start();
}
Aggregations