Java Code Examples for com.xxl.job.admin.core.model.XxlJobInfo#setExecutorParam()

The following examples show how to use com.xxl.job.admin.core.model.XxlJobInfo#setExecutorParam() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XxlJobTrigger.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
/**
 * trigger job
 *
 * @param jobId
 * @param triggerType
 * @param failRetryCount
 * 			>=0: use this param
 * 			<0: use param from job info config
 * @param executorShardingParam
 * @param executorParam
 *          null: use job param
 *          not null: cover job param
 */
public static void trigger(int jobId, TriggerTypeEnum triggerType, int failRetryCount, String executorShardingParam, String executorParam) {
    // load data
    XxlJobInfo jobInfo = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(jobId);
    if (jobInfo == null) {
        logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
        return;
    }
    if (executorParam != null) {
        jobInfo.setExecutorParam(executorParam);
    }
    int finalFailRetryCount = failRetryCount>=0?failRetryCount:jobInfo.getExecutorFailRetryCount();
    XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(jobInfo.getJobGroup());

    // sharding param
    int[] shardingParam = null;
    if (executorShardingParam!=null){
        String[] shardingArr = executorShardingParam.split("/");
        if (shardingArr.length==2 && StringUtils.isNumeric(shardingArr[0]) && StringUtils.isNumeric(shardingArr[1])) {
            shardingParam = new int[2];
            shardingParam[0] = Integer.valueOf(shardingArr[0]);
            shardingParam[1] = Integer.valueOf(shardingArr[1]);
        }
    }
    if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST==ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null)
            && group.getRegistryList()!=null && !group.getRegistryList().isEmpty()
            && shardingParam==null) {
        for (int i = 0; i < group.getRegistryList().size(); i++) {
            processTrigger(group, jobInfo, finalFailRetryCount, triggerType, i, group.getRegistryList().size());
        }
    } else {
        if (shardingParam == null) {
            shardingParam = new int[]{0, 1};
        }
        processTrigger(group, jobInfo, finalFailRetryCount, triggerType, shardingParam[0], shardingParam[1]);
    }

}
 
Example 2
Source File: XxlJobServiceImpl.java    From open-capacity-platform with Apache License 2.0 4 votes vote down vote up
@Override
public ReturnT<String> update(XxlJobInfo jobInfo) {

	// valid
	if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
	}
	if (StringUtils.isBlank(jobInfo.getJobDesc())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_jobdesc")) );
	}
	if (StringUtils.isBlank(jobInfo.getAuthor())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_author")) );
	}
	if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy")+I18nUtil.getString("system_unvalid")) );
	}
	if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy")+I18nUtil.getString("system_unvalid")) );
	}
	if (ExecutorFailStrategyEnum.match(jobInfo.getExecutorFailStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorFailStrategy")+I18nUtil.getString("system_unvalid")));
	}

	// ChildJobId valid
	if (StringUtils.isNotBlank(jobInfo.getChildJobId())) {
		String[] childJobIds = StringUtils.split(jobInfo.getChildJobId(), ",");
		for (String childJobIdItem: childJobIds) {
			if (StringUtils.isNotBlank(childJobIdItem) && StringUtils.isNumeric(childJobIdItem)) {
				XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.valueOf(childJobIdItem));
				if (childJobInfo==null) {
					return new ReturnT<String>(ReturnT.FAIL_CODE,
							MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_not_found")), childJobIdItem));
				}
				// avoid cycle relate
				if (childJobInfo.getId() == jobInfo.getId()) {
					return new ReturnT<String>(ReturnT.FAIL_CODE, MessageFormat.format(I18nUtil.getString("jobinfo_field_childJobId_limit"), childJobIdItem));
				}
			} else {
				return new ReturnT<String>(ReturnT.FAIL_CODE,
						MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_unvalid")), childJobIdItem));
			}
		}
		jobInfo.setChildJobId(StringUtils.join(childJobIds, ","));
	}

	// stage job info
	XxlJobInfo exists_jobInfo = xxlJobInfoDao.loadById(jobInfo.getId());
	if (exists_jobInfo == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id")+I18nUtil.getString("system_not_found")) );
	}
	//String old_cron = exists_jobInfo.getJobCron();

	exists_jobInfo.setJobCron(jobInfo.getJobCron());
	exists_jobInfo.setJobDesc(jobInfo.getJobDesc());
	exists_jobInfo.setAuthor(jobInfo.getAuthor());
	exists_jobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
	exists_jobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
	exists_jobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
	exists_jobInfo.setExecutorParam(jobInfo.getExecutorParam());
	exists_jobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
	exists_jobInfo.setExecutorFailStrategy(jobInfo.getExecutorFailStrategy());
	exists_jobInfo.setChildJobId(jobInfo.getChildJobId());
       xxlJobInfoDao.update(exists_jobInfo);

	// fresh quartz
	String qz_group = String.valueOf(exists_jobInfo.getJobGroup());
	String qz_name = String.valueOf(exists_jobInfo.getId());
       try {
           boolean ret = XxlJobDynamicScheduler.rescheduleJob(qz_group, qz_name, exists_jobInfo.getJobCron());
           return ret?ReturnT.SUCCESS:ReturnT.FAIL;
       } catch (SchedulerException e) {
           logger.error(e.getMessage(), e);
       }

	return ReturnT.FAIL;
}
 
Example 3
Source File: XxlJobServiceImpl.java    From microservices-platform with Apache License 2.0 4 votes vote down vote up
@Override
public ReturnT<String> update(XxlJobInfo jobInfo) {

	// valid
	if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
	}
	if (StringUtils.isBlank(jobInfo.getJobDesc())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_jobdesc")) );
	}
	if (StringUtils.isBlank(jobInfo.getAuthor())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_author")) );
	}
	if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy")+I18nUtil.getString("system_unvalid")) );
	}
	if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy")+I18nUtil.getString("system_unvalid")) );
	}

	// ChildJobId valid
	if (StringUtils.isNotBlank(jobInfo.getChildJobId())) {
		String[] childJobIds = StringUtils.split(jobInfo.getChildJobId(), ",");
		for (String childJobIdItem: childJobIds) {
			if (StringUtils.isNotBlank(childJobIdItem) && StringUtils.isNumeric(childJobIdItem)) {
				XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.valueOf(childJobIdItem));
				if (childJobInfo==null) {
					return new ReturnT<String>(ReturnT.FAIL_CODE,
							MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_not_found")), childJobIdItem));
				}
			} else {
				return new ReturnT<String>(ReturnT.FAIL_CODE,
						MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_unvalid")), childJobIdItem));
			}
		}
		jobInfo.setChildJobId(StringUtils.join(childJobIds, ","));
	}

	// stage job info
	XxlJobInfo exists_jobInfo = xxlJobInfoDao.loadById(jobInfo.getId());
	if (exists_jobInfo == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id")+I18nUtil.getString("system_not_found")) );
	}
	//String old_cron = exists_jobInfo.getJobCron();

	exists_jobInfo.setJobCron(jobInfo.getJobCron());
	exists_jobInfo.setJobDesc(jobInfo.getJobDesc());
	exists_jobInfo.setAuthor(jobInfo.getAuthor());
	exists_jobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
	exists_jobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
	exists_jobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
	exists_jobInfo.setExecutorParam(jobInfo.getExecutorParam());
	exists_jobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
	exists_jobInfo.setExecutorTimeout(jobInfo.getExecutorTimeout());
	exists_jobInfo.setExecutorFailRetryCount(jobInfo.getExecutorFailRetryCount());
	exists_jobInfo.setChildJobId(jobInfo.getChildJobId());
       xxlJobInfoDao.update(exists_jobInfo);


	// update quartz-cron if started
	String qz_group = String.valueOf(exists_jobInfo.getJobGroup());
	String qz_name = String.valueOf(exists_jobInfo.getId());
       try {
           XxlJobDynamicScheduler.updateJobCron(qz_group, qz_name, exists_jobInfo.getJobCron());
       } catch (SchedulerException e) {
           logger.error(e.getMessage(), e);
		return ReturnT.FAIL;
       }

	return ReturnT.SUCCESS;
}
 
Example 4
Source File: XxlJobInfoDaoTest.java    From microservices-platform with Apache License 2.0 4 votes vote down vote up
@Test
public void save_load(){
	XxlJobInfo info = new XxlJobInfo();
	info.setJobGroup(1);
	info.setJobCron("jobCron");
	info.setJobDesc("desc");
	info.setAuthor("setAuthor");
	info.setAlarmEmail("setAlarmEmail");
	info.setExecutorRouteStrategy("setExecutorRouteStrategy");
	info.setExecutorHandler("setExecutorHandler");
	info.setExecutorParam("setExecutorParam");
	info.setExecutorBlockStrategy("setExecutorBlockStrategy");
	info.setGlueType("setGlueType");
	info.setGlueSource("setGlueSource");
	info.setGlueRemark("setGlueRemark");
	info.setChildJobId("1");

	int count = xxlJobInfoDao.save(info);

	XxlJobInfo info2 = xxlJobInfoDao.loadById(info.getId());
	info2.setJobCron("jobCron2");
	info2.setJobDesc("desc2");
	info2.setAuthor("setAuthor2");
	info2.setAlarmEmail("setAlarmEmail2");
	info2.setExecutorRouteStrategy("setExecutorRouteStrategy2");
	info2.setExecutorHandler("setExecutorHandler2");
	info2.setExecutorParam("setExecutorParam2");
	info2.setExecutorBlockStrategy("setExecutorBlockStrategy2");
	info2.setGlueType("setGlueType2");
	info2.setGlueSource("setGlueSource2");
	info2.setGlueRemark("setGlueRemark2");
	info2.setGlueUpdatetime(new Date());
	info2.setChildJobId("1");

	int item2 = xxlJobInfoDao.update(info2);

	xxlJobInfoDao.delete(info2.getId());

	List<XxlJobInfo> list2 = xxlJobInfoDao.getJobsByGroup(1);

	int ret3 = xxlJobInfoDao.findAllCount();

}
 
Example 5
Source File: XxlJobServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 4 votes vote down vote up
@Override
public ReturnT<String> update(XxlJobInfo jobInfo) {

    // valid
    if (JobTypeEnum.CRON.eq(jobInfo.getType())) {
        if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
            return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
        }
    } else {
        //表单校验
        String msg = validate(jobInfo.getIntervalSeconds(), jobInfo.getRepeatCount(),
                jobInfo.getStartExecuteTime(), jobInfo.getEndExecuteTime());
        if (!StringUtils.isEmpty(msg)) {
            return new ReturnT<String>(ReturnT.FAIL_CODE, msg);
        }
    }
    if (StringUtils.isBlank(jobInfo.getJobDesc())) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));
    }
    if (StringUtils.isBlank(jobInfo.getAuthor())) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_author")));
    }
    if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy") + I18nUtil.getString("system_unvalid")));
    }
    if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy") + I18nUtil.getString("system_unvalid")));
    }

    // ChildJobId valid
    if (StringUtils.isNotBlank(jobInfo.getChildJobId())) {
        String[] childJobIds = StringUtils.split(jobInfo.getChildJobId(), ",");
        for (String childJobIdItem : childJobIds) {
            if (StringUtils.isNotBlank(childJobIdItem) && StringUtils.isNumeric(childJobIdItem)) {
                XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.valueOf(childJobIdItem));
                if (childJobInfo == null) {
                    return new ReturnT<String>(ReturnT.FAIL_CODE,
                            MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId") + "({0})" + I18nUtil.getString("system_not_found")), childJobIdItem));
                }
            } else {
                return new ReturnT<String>(ReturnT.FAIL_CODE,
                        MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId") + "({0})" + I18nUtil.getString("system_unvalid")), childJobIdItem));
            }
        }
        jobInfo.setChildJobId(StringUtils.join(childJobIds, ","));
    }

    // stage job info
    XxlJobInfo existsJobInfo = xxlJobInfoDao.loadById(jobInfo.getId());
    if (existsJobInfo == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id") + I18nUtil.getString("system_not_found")));
    }
    //String old_cron = exists_jobInfo.getJobCron();

    existsJobInfo.setJobCron(jobInfo.getJobCron());
    existsJobInfo.setJobDesc(jobInfo.getJobDesc());
    existsJobInfo.setAuthor(jobInfo.getAuthor());
    existsJobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
    existsJobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
    existsJobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
    existsJobInfo.setExecutorParam(jobInfo.getExecutorParam());
    existsJobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
    existsJobInfo.setExecutorTimeout(jobInfo.getExecutorTimeout());
    existsJobInfo.setExecutorFailRetryCount(jobInfo.getExecutorFailRetryCount());
    existsJobInfo.setChildJobId(jobInfo.getChildJobId());
    existsJobInfo.setRepeatCount(jobInfo.getRepeatCount());
    existsJobInfo.setStartExecuteTime(jobInfo.getStartExecuteTime());
    existsJobInfo.setEndExecuteTime(jobInfo.getEndExecuteTime());
    existsJobInfo.setIntervalSeconds(jobInfo.getIntervalSeconds());
    xxlJobInfoDao.update(existsJobInfo);

    if (JobTypeEnum.TIMES.eq(jobInfo.getType())) {
        return ReturnT.SUCCESS;
    }

    // update quartz-cron if started
    String qzGroup = String.valueOf(existsJobInfo.getJobGroup());
    String qzName = String.valueOf(existsJobInfo.getId());
    try {
        XxlJobDynamicScheduler.updateJobCron(qzGroup, qzName, existsJobInfo.getJobCron());
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
        return ReturnT.FAIL;
    }

    return ReturnT.SUCCESS;
}
 
Example 6
Source File: XxlJobTrigger.java    From zuihou-admin-boot with Apache License 2.0 4 votes vote down vote up
/**
 * trigger job
 *
 * @param jobId
 * @param triggerType
 * @param failRetryCount        >=0: use this param
 *                              <0: use param from job info config
 * @param executorShardingParam
 * @param executorParam         null: use job param
 *                              not null: cover job param
 */
public static void trigger(int jobId, TriggerTypeEnum triggerType, int failRetryCount, String executorShardingParam, String executorParam) {
    // load data
    XxlJobInfo jobInfo = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(jobId);
    if (jobInfo == null) {
        logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
        return;
    }
    //这里是为了区分 定时 和 cron 类型
    if (JobTypeEnum.TIMES.eq(jobInfo.getType())) {
        triggerType = TriggerTypeEnum.TIMING;
    }

    //设置执行参数
    if (executorParam != null) {
        jobInfo.setExecutorParam(executorParam);
    }
    //失败重试次数
    int finalFailRetryCount = failRetryCount >= 0 ? failRetryCount : jobInfo.getExecutorFailRetryCount();
    //任务组
    XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(jobInfo.getJobGroup());

    // sharding param 分片参数
    int[] shardingParam = null;
    if (executorShardingParam != null) {
        String[] shardingArr = executorShardingParam.split("/");
        if (shardingArr.length == 2 && StringUtils.isNumeric(shardingArr[0]) && StringUtils.isNumeric(shardingArr[1])) {
            shardingParam = new int[2];
            shardingParam[0] = Integer.valueOf(shardingArr[0]);
            shardingParam[1] = Integer.valueOf(shardingArr[1]);
        }
    }
    //分片广播
    if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null)
            && CollectionUtils.isNotEmpty(group.getRegistryList()) && shardingParam == null) {
        for (int i = 0; i < group.getRegistryList().size(); i++) {
            processTrigger(group, jobInfo, finalFailRetryCount, triggerType, i, group.getRegistryList().size());
        }
    } else {
        if (shardingParam == null) {
            shardingParam = new int[]{0, 1};
        }
        processTrigger(group, jobInfo, finalFailRetryCount, triggerType, shardingParam[0], shardingParam[1]);
    }

}
 
Example 7
Source File: XxlJobServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 4 votes vote down vote up
@Override
public ReturnT<String> update(XxlJobInfo jobInfo) {

    // valid
    if (JobTypeEnum.CRON.eq(jobInfo.getType())) {
        if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
            return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
        }
    } else {
        //表单校验
        String msg = validate(jobInfo.getIntervalSeconds(), jobInfo.getRepeatCount(),
                jobInfo.getStartExecuteTime(), jobInfo.getEndExecuteTime());
        if (!StringUtils.isEmpty(msg)) {
            return new ReturnT<String>(ReturnT.FAIL_CODE, msg);
        }
    }
    if (StringUtils.isBlank(jobInfo.getJobDesc())) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_jobdesc")));
    }
    if (StringUtils.isBlank(jobInfo.getAuthor())) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input") + I18nUtil.getString("jobinfo_field_author")));
    }
    if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy") + I18nUtil.getString("system_unvalid")));
    }
    if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy") + I18nUtil.getString("system_unvalid")));
    }

    // ChildJobId valid
    if (StringUtils.isNotBlank(jobInfo.getChildJobId())) {
        String[] childJobIds = StringUtils.split(jobInfo.getChildJobId(), ",");
        for (String childJobIdItem : childJobIds) {
            if (StringUtils.isNotBlank(childJobIdItem) && StringUtils.isNumeric(childJobIdItem)) {
                XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.valueOf(childJobIdItem));
                if (childJobInfo == null) {
                    return new ReturnT<String>(ReturnT.FAIL_CODE,
                            MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId") + "({0})" + I18nUtil.getString("system_not_found")), childJobIdItem));
                }
            } else {
                return new ReturnT<String>(ReturnT.FAIL_CODE,
                        MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId") + "({0})" + I18nUtil.getString("system_unvalid")), childJobIdItem));
            }
        }
        jobInfo.setChildJobId(StringUtils.join(childJobIds, ","));
    }

    // stage job info
    XxlJobInfo existsJobInfo = xxlJobInfoDao.loadById(jobInfo.getId());
    if (existsJobInfo == null) {
        return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id") + I18nUtil.getString("system_not_found")));
    }
    //String old_cron = exists_jobInfo.getJobCron();

    existsJobInfo.setJobCron(jobInfo.getJobCron());
    existsJobInfo.setJobDesc(jobInfo.getJobDesc());
    existsJobInfo.setAuthor(jobInfo.getAuthor());
    existsJobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
    existsJobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
    existsJobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
    existsJobInfo.setExecutorParam(jobInfo.getExecutorParam());
    existsJobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
    existsJobInfo.setExecutorTimeout(jobInfo.getExecutorTimeout());
    existsJobInfo.setExecutorFailRetryCount(jobInfo.getExecutorFailRetryCount());
    existsJobInfo.setChildJobId(jobInfo.getChildJobId());
    existsJobInfo.setRepeatCount(jobInfo.getRepeatCount());
    existsJobInfo.setStartExecuteTime(jobInfo.getStartExecuteTime());
    existsJobInfo.setEndExecuteTime(jobInfo.getEndExecuteTime());
    existsJobInfo.setIntervalSeconds(jobInfo.getIntervalSeconds());
    xxlJobInfoDao.update(existsJobInfo);

    if (JobTypeEnum.TIMES.eq(jobInfo.getType())) {
        return ReturnT.SUCCESS;
    }

    // update quartz-cron if started
    String qzGroup = String.valueOf(existsJobInfo.getJobGroup());
    String qzName = String.valueOf(existsJobInfo.getId());
    try {
        XxlJobDynamicScheduler.updateJobCron(qzGroup, qzName, existsJobInfo.getJobCron());
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
        return ReturnT.FAIL;
    }

    return ReturnT.SUCCESS;
}
 
Example 8
Source File: XxlJobTrigger.java    From zuihou-admin-cloud with Apache License 2.0 4 votes vote down vote up
/**
 * trigger job
 *
 * @param jobId
 * @param triggerType
 * @param failRetryCount        >=0: use this param
 *                              <0: use param from job info config
 * @param executorShardingParam
 * @param executorParam         null: use job param
 *                              not null: cover job param
 */
public static void trigger(int jobId, TriggerTypeEnum triggerType, int failRetryCount, String executorShardingParam, String executorParam) {
    // load data
    XxlJobInfo jobInfo = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(jobId);
    if (jobInfo == null) {
        logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
        return;
    }
    //这里是为了区分 定时 和 cron 类型
    if (JobTypeEnum.TIMES.eq(jobInfo.getType())) {
        triggerType = TriggerTypeEnum.TIMING;
    }

    //设置执行参数
    if (executorParam != null) {
        jobInfo.setExecutorParam(executorParam);
    }
    //失败重试次数
    int finalFailRetryCount = failRetryCount >= 0 ? failRetryCount : jobInfo.getExecutorFailRetryCount();
    //任务组
    XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(jobInfo.getJobGroup());

    // sharding param 分片参数
    int[] shardingParam = null;
    if (executorShardingParam != null) {
        String[] shardingArr = executorShardingParam.split("/");
        if (shardingArr.length == 2 && StringUtils.isNumeric(shardingArr[0]) && StringUtils.isNumeric(shardingArr[1])) {
            shardingParam = new int[2];
            shardingParam[0] = Integer.valueOf(shardingArr[0]);
            shardingParam[1] = Integer.valueOf(shardingArr[1]);
        }
    }
    //分片广播
    if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null)
            && CollectionUtils.isNotEmpty(group.getRegistryList()) && shardingParam == null) {
        for (int i = 0; i < group.getRegistryList().size(); i++) {
            processTrigger(group, jobInfo, finalFailRetryCount, triggerType, i, group.getRegistryList().size());
        }
    } else {
        if (shardingParam == null) {
            shardingParam = new int[]{0, 1};
        }
        processTrigger(group, jobInfo, finalFailRetryCount, triggerType, shardingParam[0], shardingParam[1]);
    }

}
 
Example 9
Source File: XxlJobServiceImpl.java    From xxl-job with GNU General Public License v3.0 4 votes vote down vote up
@Override
public ReturnT<String> update(XxlJobInfo jobInfo) {

	// valid
	if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
	}
	if (jobInfo.getJobDesc()==null || jobInfo.getJobDesc().trim().length()==0) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_jobdesc")) );
	}
	if (jobInfo.getAuthor()==null || jobInfo.getAuthor().trim().length()==0) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_author")) );
	}
	if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy")+I18nUtil.getString("system_unvalid")) );
	}
	if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy")+I18nUtil.getString("system_unvalid")) );
	}

	// ChildJobId valid
       if (jobInfo.getChildJobId()!=null && jobInfo.getChildJobId().trim().length()>0) {
		String[] childJobIds = jobInfo.getChildJobId().split(",");
		for (String childJobIdItem: childJobIds) {
			if (childJobIdItem!=null && childJobIdItem.trim().length()>0 && isNumeric(childJobIdItem)) {
				XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.parseInt(childJobIdItem));
				if (childJobInfo==null) {
					return new ReturnT<String>(ReturnT.FAIL_CODE,
							MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_not_found")), childJobIdItem));
				}
			} else {
				return new ReturnT<String>(ReturnT.FAIL_CODE,
						MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_unvalid")), childJobIdItem));
			}
		}

		// join , avoid "xxx,,"
		String temp = "";
		for (String item:childJobIds) {
			temp += item + ",";
		}
		temp = temp.substring(0, temp.length()-1);

		jobInfo.setChildJobId(temp);
	}

	// group valid
	XxlJobGroup jobGroup = xxlJobGroupDao.load(jobInfo.getJobGroup());
	if (jobGroup == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_jobgroup")+I18nUtil.getString("system_unvalid")) );
	}

	// stage job info
	XxlJobInfo exists_jobInfo = xxlJobInfoDao.loadById(jobInfo.getId());
	if (exists_jobInfo == null) {
		return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id")+I18nUtil.getString("system_not_found")) );
	}

	// next trigger time (5s后生效,避开预读周期)
	long nextTriggerTime = exists_jobInfo.getTriggerNextTime();
	if (exists_jobInfo.getTriggerStatus() == 1 && !jobInfo.getJobCron().equals(exists_jobInfo.getJobCron()) ) {
		try {
			Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS));
			if (nextValidTime == null) {
				return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_never_fire"));
			}
			nextTriggerTime = nextValidTime.getTime();
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
			return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid")+" | "+ e.getMessage());
		}
	}

	exists_jobInfo.setJobGroup(jobInfo.getJobGroup());
	exists_jobInfo.setJobCron(jobInfo.getJobCron());
	exists_jobInfo.setJobDesc(jobInfo.getJobDesc());
	exists_jobInfo.setAuthor(jobInfo.getAuthor());
	exists_jobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
	exists_jobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
	exists_jobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
	exists_jobInfo.setExecutorParam(jobInfo.getExecutorParam());
	exists_jobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
	exists_jobInfo.setExecutorTimeout(jobInfo.getExecutorTimeout());
	exists_jobInfo.setExecutorFailRetryCount(jobInfo.getExecutorFailRetryCount());
	exists_jobInfo.setChildJobId(jobInfo.getChildJobId());
	exists_jobInfo.setTriggerNextTime(nextTriggerTime);

	exists_jobInfo.setUpdateTime(new Date());
       xxlJobInfoDao.update(exists_jobInfo);


	return ReturnT.SUCCESS;
}
 
Example 10
Source File: XxlJobTrigger.java    From xxl-job with GNU General Public License v3.0 4 votes vote down vote up
/**
 * trigger job
 *
 * @param jobId
 * @param triggerType
 * @param failRetryCount
 * 			>=0: use this param
 * 			<0: use param from job info config
 * @param executorShardingParam
 * @param executorParam
 *          null: use job param
 *          not null: cover job param
 * @param addressList
 *          null: use executor addressList
 *          not null: cover
 */
public static void trigger(int jobId,
                           TriggerTypeEnum triggerType,
                           int failRetryCount,
                           String executorShardingParam,
                           String executorParam,
                           String addressList) {

    // load data
    XxlJobInfo jobInfo = XxlJobAdminConfig.getAdminConfig().getXxlJobInfoDao().loadById(jobId);
    if (jobInfo == null) {
        logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
        return;
    }
    if (executorParam != null) {
        jobInfo.setExecutorParam(executorParam);
    }
    int finalFailRetryCount = failRetryCount>=0?failRetryCount:jobInfo.getExecutorFailRetryCount();
    XxlJobGroup group = XxlJobAdminConfig.getAdminConfig().getXxlJobGroupDao().load(jobInfo.getJobGroup());

    // cover addressList
    if (addressList!=null && addressList.trim().length()>0) {
        group.setAddressType(1);
        group.setAddressList(addressList.trim());
    }

    // sharding param
    int[] shardingParam = null;
    if (executorShardingParam!=null){
        String[] shardingArr = executorShardingParam.split("/");
        if (shardingArr.length==2 && isNumeric(shardingArr[0]) && isNumeric(shardingArr[1])) {
            shardingParam = new int[2];
            shardingParam[0] = Integer.valueOf(shardingArr[0]);
            shardingParam[1] = Integer.valueOf(shardingArr[1]);
        }
    }
    if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST==ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null)
            && group.getRegistryList()!=null && !group.getRegistryList().isEmpty()
            && shardingParam==null) {
        for (int i = 0; i < group.getRegistryList().size(); i++) {
            processTrigger(group, jobInfo, finalFailRetryCount, triggerType, i, group.getRegistryList().size());
        }
    } else {
        if (shardingParam == null) {
            shardingParam = new int[]{0, 1};
        }
        processTrigger(group, jobInfo, finalFailRetryCount, triggerType, shardingParam[0], shardingParam[1]);
    }

}
 
Example 11
Source File: XxlJobInfoDaoTest.java    From xxl-job with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void save_load(){
	XxlJobInfo info = new XxlJobInfo();
	info.setJobGroup(1);
	info.setJobCron("jobCron");
	info.setJobDesc("desc");
	info.setAuthor("setAuthor");
	info.setAlarmEmail("setAlarmEmail");
	info.setExecutorRouteStrategy("setExecutorRouteStrategy");
	info.setExecutorHandler("setExecutorHandler");
	info.setExecutorParam("setExecutorParam");
	info.setExecutorBlockStrategy("setExecutorBlockStrategy");
	info.setGlueType("setGlueType");
	info.setGlueSource("setGlueSource");
	info.setGlueRemark("setGlueRemark");
	info.setChildJobId("1");

	info.setAddTime(new Date());
	info.setUpdateTime(new Date());
	info.setGlueUpdatetime(new Date());

	int count = xxlJobInfoDao.save(info);

	XxlJobInfo info2 = xxlJobInfoDao.loadById(info.getId());
	info2.setJobCron("jobCron2");
	info2.setJobDesc("desc2");
	info2.setAuthor("setAuthor2");
	info2.setAlarmEmail("setAlarmEmail2");
	info2.setExecutorRouteStrategy("setExecutorRouteStrategy2");
	info2.setExecutorHandler("setExecutorHandler2");
	info2.setExecutorParam("setExecutorParam2");
	info2.setExecutorBlockStrategy("setExecutorBlockStrategy2");
	info2.setGlueType("setGlueType2");
	info2.setGlueSource("setGlueSource2");
	info2.setGlueRemark("setGlueRemark2");
	info2.setGlueUpdatetime(new Date());
	info2.setChildJobId("1");

	info2.setUpdateTime(new Date());
	int item2 = xxlJobInfoDao.update(info2);

	xxlJobInfoDao.delete(info2.getId());

	List<XxlJobInfo> list2 = xxlJobInfoDao.getJobsByGroup(1);

	int ret3 = xxlJobInfoDao.findAllCount();

}