use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class AbstractSaturnJob method mayRunDownStream.
@Override
protected boolean mayRunDownStream(JobExecutionMultipleShardingContext shardingContext) {
if (!super.mayRunDownStream(shardingContext)) {
return false;
}
// 只要有一个失败,就不触发下游
if (shardingContext instanceof SaturnExecutionContext) {
SaturnExecutionContext saturnContext = (SaturnExecutionContext) shardingContext;
Map<Integer, SaturnJobReturn> shardingItemResults = saturnContext.getShardingItemResults();
if (shardingItemResults != null && !shardingItemResults.isEmpty()) {
Iterator<Map.Entry<Integer, SaturnJobReturn>> iterator = shardingItemResults.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, SaturnJobReturn> next = iterator.next();
Integer item = next.getKey();
SaturnJobReturn saturnJobReturn = next.getValue();
if (saturnJobReturn.getErrorGroup() != SaturnSystemErrorGroup.SUCCESS) {
LogUtils.warn(log, jobName, "item {} ran unsuccessfully, SaturnJobReturn is {}, wont run downStream", item, saturnJobReturn, item);
return false;
}
}
}
}
return true;
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class LongtimeJavaJob method handleJavaJob.
@Override
public SaturnJobReturn handleJavaJob(String jobName, Integer shardItem, String shardParam, SaturnJobExecutionContext shardingContext) {
String key = jobName + "_" + shardItem;
JobStatus status = statusMap.get(key);
status.running = true;
System.out.println(new Date() + " running:" + jobName + "; " + shardItem + ";" + shardParam + ";finished:" + status.finished);
try {
Thread.sleep(status.sleepSeconds * 1000);
status.runningCount++;
} catch (InterruptedException e) {
status.interrupted = true;
System.out.println("i am terminating..");
} finally {
status.finished = true;
}
return new SaturnJobReturn();
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class SaturnJavaJob method doExecution.
public SaturnJobReturn doExecution(final String jobName, final Integer key, final String value, SaturnExecutionContext shardingContext, final JavaShardingItemCallable callable) throws Throwable {
String jobClass = shardingContext.getJobConfiguration().getJobClass();
LogUtils.info(log, jobName, "Running SaturnJavaJob, jobClass [{}], item [{}]", 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 SaturnJavaJob method handleJob.
@Override
protected Map<Integer, SaturnJobReturn> handleJob(final SaturnExecutionContext shardingContext) {
final Map<Integer, SaturnJobReturn> retMap = new HashMap<Integer, SaturnJobReturn>();
synchronized (futureTaskMap) {
futureTaskMap.clear();
final String jobName = shardingContext.getJobName();
final int timeoutSeconds = getTimeoutSeconds();
ExecutorService executorService = getExecutorService();
// 处理自定义参数
String jobParameter = shardingContext.getJobParameter();
// shardingItemParameters为参数表解析出来的Key/Value值
Map<Integer, String> shardingItemParameters = shardingContext.getShardingItemParameters();
for (final Entry<Integer, String> shardingItem : shardingItemParameters.entrySet()) {
final Integer key = shardingItem.getKey();
try {
String jobValue = shardingItem.getValue();
// 作业分片的对应值
final String itemVal = getRealItemValue(jobParameter, jobValue);
ShardingItemFutureTask shardingItemFutureTask = new ShardingItemFutureTask(createCallable(jobName, key, itemVal, timeoutSeconds, shardingContext, this), null);
Future<?> callFuture = executorService.submit(shardingItemFutureTask);
if (timeoutSeconds > 0) {
TimeoutSchedulerExecutor.scheduleTimeoutJob(shardingContext.getExecutorName(), timeoutSeconds, shardingItemFutureTask);
}
shardingItemFutureTask.setCallFuture(callFuture);
futureTaskMap.put(key, shardingItemFutureTask);
} catch (Throwable t) {
LogUtils.error(log, jobName, t.getMessage(), t);
retMap.put(key, new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, t.getMessage(), SaturnSystemErrorGroup.FAIL));
}
}
}
for (Entry<Integer, ShardingItemFutureTask> entry : futureTaskMap.entrySet()) {
Integer item = entry.getKey();
ShardingItemFutureTask futureTask = entry.getValue();
try {
futureTask.getCallFuture().get();
} catch (Exception e) {
LogUtils.error(log, jobName, e.getMessage(), e);
retMap.put(item, new SaturnJobReturn(SaturnSystemReturnCode.SYSTEM_FAIL, e.getMessage(), SaturnSystemErrorGroup.FAIL));
continue;
}
retMap.put(item, futureTask.getCallable().getSaturnJobReturn());
}
synchronized (futureTaskMap) {
futureTaskMap.clear();
}
return retMap;
}
use of com.vip.saturn.job.SaturnJobReturn in project Saturn by vipshop.
the class SaturnScriptJob method handleJob.
@Override
public Map<Integer, SaturnJobReturn> handleJob(final SaturnExecutionContext shardingContext) {
synchronized (watchDogList) {
watchDogList.clear();
}
shardingItemCallableList.clear();
final Map<Integer, SaturnJobReturn> retMap = new ConcurrentHashMap<Integer, SaturnJobReturn>();
Map<Integer, String> shardingItemParameters = shardingContext.getShardingItemParameters();
final String jobName = shardingContext.getJobName();
ExecutorService executorService = getExecutorService();
// 处理自定义参数
String jobParameter = shardingContext.getJobParameter();
final CountDownLatch latch = new CountDownLatch(shardingItemParameters.size());
for (final Entry<Integer, String> shardingItem : shardingItemParameters.entrySet()) {
final Integer key = shardingItem.getKey();
String jobValue = shardingItem.getValue();
// 作业分片的对应值
final String execParameter = getRealItemValue(jobParameter, jobValue);
LogUtils.debug(log, jobName, "jobname={}, key= {}, jobParameter={}", jobName, key, execParameter);
executorService.submit(new Runnable() {
@Override
public void run() {
SaturnJobReturn jobReturn = null;
try {
jobReturn = innerHandleWithListener(jobName, key, execParameter, shardingContext);
} catch (Throwable e) {
LogUtils.error(log, jobName, e.getMessage(), e);
jobReturn = new SaturnJobReturn(SaturnSystemReturnCode.USER_FAIL, "Error: " + e.getMessage(), SaturnSystemErrorGroup.FAIL);
} finally {
retMap.put(key, jobReturn);
latch.countDown();
}
}
});
}
try {
latch.await();
} catch (final InterruptedException ex) {
LogUtils.error(log, jobName, "SaturnScriptJob: Job {} is interrupted", jobName);
Thread.currentThread().interrupt();
}
return retMap;
}
Aggregations