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);
}
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;
}
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());
}
}
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);
}
}
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 + "的处理结果");
}
Aggregations