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

com.ineunet.knife.util.concurrent.JobExecutors Maven / Gradle / Ivy

There is a newer version: 2.7.12
Show newest version
/*
 * Copyright 2013-2018 iNeunet OpenSource and the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.ineunet.knife.util.concurrent;

import java.util.Collection;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ineunet.knife.util.Asserts;
import com.ineunet.knife.util.ErrorUtils;
import com.ineunet.knife.util.StringUtils;

/**
 * 
 * @author [email protected]
* Created on 2013年12月1日 */ public final class JobExecutors { private static final Logger log = LoggerFactory.getLogger(JobExecutors.class); // daemon of check execution of task private static ScheduledExecutorService checkTaskExecutor = newSingleThreadScheduledExecutor(); // Check once on every 5 seconds to guarantee the task will be executed where now time is after executeTime. static { checkTaskExecutor.scheduleAtFixedRate(new Runnable() { @Override public void run() { for (Map.Entry entry : JobManagerImpl.scheduledTasks.entrySet()) { String taskId = entry.getKey(); Task task = entry.getValue(); if (System.currentTimeMillis() >= task.getExecuteTime().getTime()) { try { submit(task.getRunnable()); unregisterTask(taskId); } catch (Exception e) { log.error("#### start task " + taskId + " error: " + e.getMessage(), e); } } } } }, 50, 2, TimeUnit.SECONDS); } private JobExecutors() {} /** * all single threads add to active threads counter. * @return a singleThreadExecutor */ public static ExecutorService newSingleThreadExecutor() { ExecutorService service = Executors.newSingleThreadExecutor(); JobManagerImpl.incrementActiveThreads(); return service; } public static ExecutorService newFixedThreadPool(int nThreads) { Asserts.not(nThreads < 1, "nThreads cannot be zero or nagated."); ExecutorService service = Executors.newFixedThreadPool(nThreads); for (int i = 0; i < nThreads; i++) { JobManagerImpl.incrementActiveThreads(); } return service; } public static ScheduledExecutorService newSingleThreadScheduledExecutor() { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); JobManagerImpl.incrementActiveThreads(); return service; } // ! cannot stat active thread number in asynchronous mode, because don't know when will it be finished public static Future submit(final java.util.concurrent.Callable task) { Future future = JobManagerImpl.globalThreadPool.submit(task); return future; } // ! cannot stat active thread number in asynchronous mode, because don't know when will it be finished public static Future submit(final Runnable task) { return JobManagerImpl.globalThreadPool.submit(task); } public static JobResult executeSingleThread(Collection dataRows, final TaskInvoker rowTask) { if (dataRows == null) return new JobResult("dataRows is null", true); JobParameter param = new JobParameter<>(1, dataRows.size()); param.setDataRows(dataRows); return execute(param, rowTask); } // default ten threads public static JobResult execute(Collection dataRows, final TaskInvoker rowTask) { if (dataRows == null) return new JobResult("dataRows is null", true); JobParameter param = new JobParameter<>(10, 1000); param.setDataRows(dataRows); return execute(param, rowTask); } // 根据oneThreadRows自动调整threads,即:每个线程处理的数量固定,线程根据任务浮动 public static JobResult executeFixOneThreadRows(Collection dataRows, int oneThreadRows, final TaskInvoker rowTask) { if (dataRows == null) return new JobResult("dataRows is null", true); int size = dataRows.size(); int threads = 1; // use one thread if oneThreadRows less than dataSize if (oneThreadRows < size) { threads = size / oneThreadRows; } // add one thread if mod more than half tasks if (size % oneThreadRows > (oneThreadRows / 2)) { threads += 1; } JobParameter param = new JobParameter<>(threads, oneThreadRows); param.setDataRows(dataRows); return execute(param, rowTask); } public static JobResult execute(Collection dataRows, int printModRows, final TaskInvoker rowTask) { if (dataRows == null) return new JobResult("dataRows is null", true); if (dataRows.isEmpty()) return new JobResult("dataRows is empty", true); JobParameter param = new JobParameter<>(10, 1000); param.setDataRows(dataRows); param.setPrintProcessed(printModRows); return execute(param, rowTask); } public static JobResult execute(final JobParameter param, final TaskInvoker rowTask) { JobManagerImpl job = new JobManagerImpl() { @Override protected String title() { String name = param.getName(); if (StringUtils.isBlank(name)) { return "Default JobExecutor"; } return name; } @Override protected void doInit(JobParameter param) { } @Override protected void doHandleRow(X row, JobParameter param, JobResult result) throws Exception { int v = rowTask.invoke(row); if (v == 0) { result.incrementSuccessed(); } } }; try { return job.execute(param); } finally { job.close(); } } /** * Register ExecutorService for managed by JobManager. * @param service ExecutorService * @param serviceId id of ExecutorService * @return current number of registered ExecutorService */ public static int registerExecutorService(Object serviceId, ExecutorService service) { Asserts.notNull(serviceId, "serviceId cannot be null."); Asserts.notNull(service, "service cannot be null."); JobManagerImpl.executorServices.put(serviceId, service); JobManagerImpl.incrementActiveThreads(); return JobManagerImpl.executorServices.size(); } /** * Unregister ExecutorService. * @param serviceId id of ExecutorService * @return current number of registered ExecutorService */ public static int unregisterExecutorService(Object serviceId) { Asserts.notNull(serviceId, "serviceId cannot be null."); ExecutorService service = JobManagerImpl.executorServices.remove(serviceId); if (service != null) { JobManagerImpl.decrementActiveThreads(); } return JobManagerImpl.executorServices.size(); } /** * Unregister and destroy ExecutorService. * @param serviceId id of ExecutorService * @return current number of registered ExecutorService */ public static int destroyExecutorService(Object serviceId) { Asserts.notNull(serviceId, "serviceId cannot be null."); ExecutorService service = JobManagerImpl.executorServices.remove(serviceId); if (service != null) { JobManagerImpl.decrementActiveThreads(); try { service.shutdownNow(); } catch (Exception e) { log.error(e.getMessage(), e); } finally { service = null; } } return JobManagerImpl.executorServices.size(); } /** * Unregister ExecutorService. * @param serviceId id of ExecutorService * @return current number of registered ExecutorService */ public static ExecutorService getExecutorService(Object serviceId) { Asserts.notNull(serviceId, "serviceId cannot be null."); return JobManagerImpl.executorServices.get(serviceId); } // add to queue and execute auto by daemon of checkTaskExecution when execute time is over. public static void registerTask(Task task) { Asserts.notNull(task, "task cannot be null"); Asserts.notNull(task.getExecuteTime(), "task.executeTime cannot be null"); Asserts.notNull(task.getRunnable(), "task.runnable cannot be null"); String taskId = task.getId(); if (StringUtils.isNotBlank(taskId)) { if (JobManagerImpl.scheduledTasks.containsKey(taskId)) { throw ErrorUtils.newIllegalArgumentException("task exists: " + taskId); } } else { taskId = UUID.randomUUID().toString().replace("-", ""); task.setId(taskId); } JobManagerImpl.scheduledTasks.put(taskId, task); log.debug("registed task: " + taskId); } /** * 如果取消注册时,任务已经执行或正在执行,任务是不能停止的,任务的执行不会受到影响。
* If unregistered before executeTime, task will not be executed. * @param taskId 任务id * @return task */ public static Task unregisterTask(String taskId) { Asserts.notBlank(taskId, "taskId cannot be null"); Task task = JobManagerImpl.scheduledTasks.remove(taskId); log.debug("unregistered task: " + taskId); return task; } public static boolean containsTask(String taskId) { Asserts.notBlank(taskId, "taskId cannot be null"); return JobManagerImpl.scheduledTasks.containsKey(taskId); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy