Search in sources :

Example 16 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class AbstractSaturnJob method executeJob.

@Override
protected final void executeJob(final JobExecutionMultipleShardingContext shardingContext) {
    if (!(shardingContext instanceof SaturnExecutionContext)) {
        LogUtils.error(log, jobName, "!!! The context must be instance of SaturnJobExecutionContext !!!");
        return;
    }
    long start = System.currentTimeMillis();
    SaturnExecutionContext saturnContext = (SaturnExecutionContext) shardingContext;
    saturnContext.setSaturnJob(true);
    Map<Integer, SaturnJobReturn> retMap = new HashMap<Integer, SaturnJobReturn>();
    // shardingItemParameters为参数表解析出来的Key/Value值
    Map<Integer, String> shardingItemParameters = saturnContext.getShardingItemParameters();
    // items为需要处理的作业分片
    List<Integer> items = saturnContext.getShardingItems();
    LogUtils.info(log, jobName, "Job {} handle items: {}", jobName, items);
    for (Integer item : items) {
        // 兼容配置错误,如配置3个分片, 参数表配置为0=*, 2=*, 则1分片不会执行
        if (!shardingItemParameters.containsKey(item)) {
            LogUtils.error(log, jobName, "The {} item's parameter is not valid, will not execute the business code, please check shardingItemParameters", items);
            SaturnJobReturn errRet = new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, "Config of parameter is not valid, check shardingItemParameters", SaturnSystemErrorGroup.FAIL);
            retMap.put(item, errRet);
        }
    }
    Map<Integer, SaturnJobReturn> handleJobMap = handleJob(saturnContext);
    if (handleJobMap != null) {
        retMap.putAll(handleJobMap);
    }
    // 汇总修改
    for (Integer item : items) {
        if (item == null) {
            continue;
        }
        SaturnJobReturn saturnJobReturn = retMap.get(item);
        if (saturnJobReturn == null) {
            saturnJobReturn = new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, "Can not find the corresponding SaturnJobReturn", SaturnSystemErrorGroup.FAIL);
            retMap.put(item, saturnJobReturn);
        }
        updateExecuteResult(saturnJobReturn, saturnContext, item);
    }
    long end = System.currentTimeMillis();
    LogUtils.info(log, jobName, "{} finished, totalCost={}ms, return={}", jobName, (end - start), retMap);
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn)

Example 17 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class JavaShardingItemCallable method call.

/**
 * 真正执行作业分片逻辑
 *
 * @return 执行结果
 */
public SaturnJobReturn call() {
    reset();
    SaturnSystemOutputStream.initLogger();
    currentThread = Thread.currentThread();
    SaturnJobReturn temp = null;
    try {
        beforeExecution();
        temp = saturnJob.doExecution(jobName, item, itemValue, shardingContext, this);
        // 在此之后,不能再强制停止本线程
        breakForceStop = true;
    } catch (Throwable t) {
        // 在此之后,不能再强制停止本线程
        breakForceStop = true;
        // 不是超时,不是强制停止。 打印错误日志,设置SaturnJobReturn。
        if (status.get() != TIMEOUT && status.get() != FORCE_STOP) {
            log.error(String.format(SaturnConstant.LOG_FORMAT_FOR_STRING, jobName, t.getMessage()), t);
            temp = new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, t.getMessage(), SaturnSystemErrorGroup.FAIL);
        }
    } finally {
        if (status.compareAndSet(INIT, SUCCESS)) {
            saturnJobReturn = temp;
        }
        String jobLog = SaturnSystemOutputStream.clearAndGetLog();
        if (saturnJob != null && saturnJob.getConfigService().showNormalLog()) {
            this.shardingContext.putJobLog(this.item, jobLog);
        }
    }
    return saturnJobReturn;
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn)

Example 18 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class ShardingItemFutureTask method call.

@Override
public SaturnJobReturn call() throws Exception {
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            if (e instanceof IllegalMonitorStateException || e instanceof ThreadDeath) {
                log.warn(String.format(SaturnConstant.LOG_FORMAT_FOR_STRING, callable.getJobName(), "business thread pool maybe crashed"), e);
                if (callFuture != null) {
                    callFuture.cancel(false);
                }
                log.warn(SaturnConstant.LOG_FORMAT, callable.getJobName(), "close the old business thread pool, and re-create new one");
                callable.getSaturnJob().getJobScheduler().reCreateExecutorService();
            }
        }
    });
    try {
        SaturnJobReturn ret = callable.call();
        return ret;
    } finally {
        done();
        log.debug("job:[{}] item:[{}] finish execution, which takes {}ms", callable.getJobName(), callable.getItem(), callable.getExecutionTime());
    }
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 19 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class SaturnJavaJob method handleJavaJob.

public SaturnJobReturn handleJavaJob(final String jobName, final Integer key, final String value, SaturnExecutionContext shardingContext, final JavaShardingItemCallable callable) throws Throwable {
    String jobClass = shardingContext.getJobConfiguration().getJobClass();
    log.info("[{}] msg=Running SaturnJavaJob,  jobClass [{}], item [{}]", jobName, jobClass, key);
    try {
        Object ret = new JobBusinessClassMethodCaller() {

            @Override
            protected Object internalCall(ClassLoader jobClassLoader, Class<?> saturnJobExecutionContextClazz) throws Exception {
                return jobBusinessInstance.getClass().getMethod("handleJavaJob", String.class, Integer.class, String.class, saturnJobExecutionContextClazz).invoke(jobBusinessInstance, jobName, key, value, callable.getContextForJob(jobClassLoader));
            }
        }.call(jobBusinessInstance, saturnExecutorService);
        SaturnJobReturn saturnJobReturn = (SaturnJobReturn) JavaShardingItemCallable.cloneObject(ret, saturnExecutorService.getExecutorClassLoader());
        if (saturnJobReturn != null) {
            callable.setBusinessReturned(true);
        }
        return saturnJobReturn;
    } catch (Exception e) {
        if (e.getCause() instanceof ThreadDeath) {
            throw e.getCause();
        }
        String message = logBusinessExceptionIfNecessary(jobName, e);
        return new SaturnJobReturn(SaturnSystemReturnCode.USER_FAIL, message, SaturnSystemErrorGroup.FAIL);
    }
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) InvocationTargetException(java.lang.reflect.InvocationTargetException) SchedulerException(org.quartz.SchedulerException)

Example 20 with SaturnJobReturn

use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.

the class DemoJob3 method handleJavaJob.

@Override
public SaturnJobReturn handleJavaJob(String jobName, Integer shardItem, String shardParam, SaturnJobExecutionContext shardingContext) {
    demoService.execute();
    System.out.println(new Date() + ";我会出现在运行日志里.running handleJavaJob:" + jobName + "; " + shardItem + ";" + shardParam);
    return new SaturnJobReturn("我是分片" + shardItem + "的处理结果");
}
Also used : SaturnJobReturn(com.vip.saturn.job.SaturnJobReturn) Date(java.util.Date)

Aggregations

SaturnJobReturn (com.vip.saturn.job.SaturnJobReturn)35 LogbackListAppender (com.vip.saturn.job.executor.utils.LogbackListAppender)9 HashMap (java.util.HashMap)9 Test (org.junit.Test)9 Date (java.util.Date)4 JobInitAlarmException (com.vip.saturn.job.exception.JobInitAlarmException)2 UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)2 ExecutorService (java.util.concurrent.ExecutorService)2 DemoService (com.vip.saturn.demo.service.DemoService)1 SaturnExecutionContext (com.vip.saturn.job.basic.SaturnExecutionContext)1 ShardingItemCallable (com.vip.saturn.job.basic.ShardingItemCallable)1 ExecutionInfo (com.vip.saturn.job.internal.control.ExecutionInfo)1 RegException (com.vip.saturn.job.reg.exception.RegException)1 ScriptJobRunner (com.vip.saturn.job.shell.ScriptJobRunner)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CommandLine (org.apache.commons.exec.CommandLine)1