/** * 异步执行一个任务并且把结果回调给ES * * @param jobData * @param executor * @return */ private JobResult executeAsyncJob(final JobData jobData, final JobExecutor executor) { JobResult result = JobResult.succcessResult(); new Thread( new Runnable() { @Override public void run() { JobResult result = null; try { result = executeJob(jobData, executor); } catch (Exception e) { result = JobResult.errorResult( JobResult.RESULTCODE_OTHER_ERR, "异步任务处理失败:" + e.getMessage()); } finally { try { HttpJobUtils.callBackHttpJob(jobData, result); } catch (IOException e) { logger.error("异步任务回调处理失败", e); } } } }) .start(); return result; }
/** * 同步执行一个任务 * * @param jobData * @param executor * @return */ private JobResult executeJob(JobData jobData, JobExecutor executor) throws Exception { JobResult result = null; int clientRetries = HttpJobUtils.getJobClientRetries(jobData.getData().get(JobData.JOBDATA_DATA_CLIENTRETRIES)); for (int i = 1; i <= clientRetries + 1; i++) { result = executor.execute(jobData); if (result.isSuccess()) { if (i > 1) { result.getData().put(JobResult.JOBRESULT_DATA_CLIENTRETRYCOUNT, String.valueOf(i)); } break; } try { Thread.sleep(5000); } catch (InterruptedException e1) { } if (i > 1 && i == clientRetries + 1) { result.getData().put(JobResult.JOBRESULT_DATA_CLIENTRETRYCOUNT, String.valueOf(i)); } } return result; }
public String checkRepeatAlarm() { jobResult = JobResult.succcessResult(); try { int count = repeatAlarmBO.getRepeatAlarmCount(); int page = count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1; for (int i = 1; i <= page; i++) { RepeatAlarmDO reapeatAlarm = new RepeatAlarmDO(); reapeatAlarm.setToPage(i); reapeatAlarm.setPerPageSize(pageSize); List<RepeatAlarmDO> reapeatAlarms = repeatAlarmBO.findRepeatAlarms(reapeatAlarm); for (RepeatAlarmDO r : reapeatAlarms) { if (r.getStatus() != Constants.REPEAT_ALARM_STATUS_PAUSE) { repeatAlarmBO.checkRepeatAlarm(r.getId()); } } } } catch (Exception e) { jobResult = JobResult.errorResult(JobResult.RESULTCODE_OTHER_ERR, e.getMessage()); } return SUCCESS; }
/** * 执行一个http任务 * * @param request * @param executor * @return */ public JobResult execute(JobData jobData, JobExecutor executor) { JobResult result = HttpJobUtils.checkJobData(jobData); if (!result.isSuccess()) { return result; } try { logger.info( "start|jobId:" + jobData.getJobId() + "|jobGroup:" + jobData.getJobGroup() + "|jobName:" + jobData.getJobName()); if (jobData.isSync()) { result = executeJob(jobData, executor); } else { result = executeAsyncJob(jobData, executor); } if (result.isSuccess()) { logger.info( "end|jobId:" + jobData.getJobId() + "|jobGroup:" + jobData.getJobGroup() + "|jobName:" + jobData.getJobName() + "|success:" + result.isSuccess() + "|resultCode:" + result.getResultCode() + "|resultMsg:" + result.getResultMsg(), new Object[] { jobData.getJobId(), jobData.getJobGroup(), jobData.getJobName(), result.isSuccess(), result.getResultCode(), result.getResultMsg() }); } else { logger.info( "fail|jobId:" + jobData.getJobId() + "|jobGroup:" + jobData.getJobGroup() + "|jobName:" + jobData.getJobName() + "|success:" + result.isSuccess() + "|resultCode:" + result.getResultCode() + "|resultMsg:" + result.getResultMsg(), new Object[] { jobData.getJobId(), jobData.getJobGroup(), jobData.getJobName(), result.isSuccess(), result.getResultCode(), result.getResultMsg() }); } } catch (Exception e) { result = JobResult.errorResult( JobResult.RESULTCODE_OTHER_ERR, "处理任务失败,errorMsg is:" + e.getMessage()); logger.info( "fail|jobId:" + jobData.getJobId() + "|jobGroup:" + jobData.getJobGroup() + "|jobName:" + jobData.getJobName() + "|success:" + result.isSuccess() + "|resultCode:" + result.getResultCode() + "|resultMsg:" + result.getResultMsg()); } result.setJobId(jobData.getJobId()); return result; }