下面列出了org.quartz.JobKey#jobKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 立即执行job
*/
public void runAJobNow(QuartzJob quartzJob) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getId());
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
// 如果不存在则创建一个定时任务
if (trigger == null)
addJob(quartzJob);
JobDataMap dataMap = new JobDataMap();
dataMap.put(QuartzJob.JOB_KEY, quartzJob);
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.triggerJob(jobKey, dataMap);
} catch (Exception e) {
log.error("定时任务执行失败", e);
throw new CustomException("定时任务执行失败");
}
}
@Override
public boolean deployServerCollection(long hostId, String ip) {
Assert.hasText(ip);
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put(ConstUtils.HOST_KEY, ip);
JobKey jobKey = JobKey.jobKey(ConstUtils.SERVER_JOB_NAME, ConstUtils.SERVER_JOB_GROUP);
TriggerKey triggerKey = TriggerKey.triggerKey(ip, ConstUtils.SERVER_TRIGGER_GROUP + ip);
boolean result = schedulerCenter.deployJobByCron(jobKey, triggerKey, dataMap, ScheduleUtil.getFiveMinuteCronByHostId(hostId), false);
return result;
}
/**
* 使用指定的分组删除任务
*
* @param triggerKey 任务key
*/
public void removeJob(String triggerKey, String groupName) {
try {
JobKey jobKey = JobKey.jobKey(triggerKey, groupName);
scheduler.deleteJob(jobKey);
} catch (Exception e) {
logger.error("删除任务:{},遇到异常信息如下.", triggerKey);
e.printStackTrace();
}
}
/**
* 使用指定分组暂停任务
*
* @param triggerKey 任务key
* @param groupName 任务分组
*/
public void pauseJob(String triggerKey, String groupName) {
try {
JobKey jobKey = JobKey.jobKey(triggerKey, groupName);
scheduler.pauseJob(jobKey);
} catch (Exception e) {
logger.error("暂停任务:{},遇到异常信息如下.", triggerKey);
e.printStackTrace();
}
}
@Test
public void assertPutJobWithChangedCron() throws JobExecutionException {
transientProducerRepository.put(jobKey, jobName);
JobKey newJobKey = JobKey.jobKey("0/15 * * * * ?");
transientProducerRepository.put(newJobKey, jobName);
assertTrue(transientProducerRepository.get(jobKey).isEmpty());
assertThat(transientProducerRepository.get(newJobKey).get(0), is(jobName));
transientProducerRepository.remove(jobName);
}
/**
* 立即执行一个QuartzJob(只会运行一次)
* ----------------------------------------------------------------------------
* Quartz是通过临时生成一个Trigger(Trigger的key是随机生成的)的方式实现的
* 该临时Trigger将在本次任务运行完成之后自动删除
* ----------------------------------------------------------------------------
*/
void triggerJob(long taskId) {
Optional<ScheduleTask> taskOptional = scheduleTaskRepository.findById(taskId);
if(!taskOptional.isPresent()){
throw new RuntimeException("不存在的任务:taskId=[" + taskId + "]");
}
ScheduleTask task = taskOptional.get();
JobKey jobKey = JobKey.jobKey(task.getJobname());
try{
scheduler.triggerJob(jobKey);
}catch(SchedulerException e){
throw new SeedException(CodeEnum.SYSTEM_ERROR.getCode(), "立即执行QuartzJob失败:jobname=["+task.getJobname()+"]", e);
}
}
/**
* 暂停一个job
* @param quartzJob /
*/
public void pauseJob(QuartzJob quartzJob){
try {
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.pauseJob(jobKey);
} catch (Exception e){
log.error("定时任务暂停失败", e);
throw new BadRequestException("定时任务暂停失败");
}
}
public boolean contains(String name, String group) throws SchedulerException {
JobKey key = null;
if(StringUtils.isEmpty(group))
key = JobKey.jobKey(name, DEFAULT_GROUP);
else
key = JobKey.jobKey(name, group);
return scheduler.checkExists(key);
}
/**
* 删除一个job
*/
public void deleteJob(QuartzJob quartzJob) {
try {
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.pauseJob(jobKey);
scheduler.deleteJob(jobKey);
} catch (Exception e) {
log.error("删除定时任务失败", e);
throw new CustomException("删除定时任务失败");
}
}
@Override
public boolean deployRedisCollection(long appId, String host, int port) {
Assert.isTrue(appId > 0);
Assert.hasText(host);
Assert.isTrue(port > 0);
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put(ConstUtils.HOST_KEY, host);
dataMap.put(ConstUtils.PORT_KEY, port);
dataMap.put(ConstUtils.APP_KEY, appId);
JobKey jobKey = JobKey.jobKey(ConstUtils.REDIS_JOB_NAME, ConstUtils.REDIS_JOB_GROUP);
TriggerKey triggerKey = TriggerKey
.triggerKey(ObjectConvert.linkIpAndPort(host, port), ConstUtils.REDIS_TRIGGER_GROUP + appId);
return schedulerCenter
.deployJobByCron(jobKey, triggerKey, dataMap, ScheduleUtil.getMinuteCronByAppId(appId), false);
}
@Test
public void assertPutMoreJobWithChangedCron() throws JobExecutionException {
String jobName2 = "other_test_job";
transientProducerRepository.put(jobKey, jobName);
transientProducerRepository.put(jobKey, jobName2);
JobKey newJobKey = JobKey.jobKey("0/15 * * * * ?");
transientProducerRepository.put(newJobKey, jobName);
assertThat(transientProducerRepository.get(jobKey).get(0), is(jobName2));
assertThat(transientProducerRepository.get(newJobKey).get(0), is(jobName));
transientProducerRepository.remove(jobName);
transientProducerRepository.remove(jobName2);
}
/**
* 获取jobKey
*/
public static JobKey getJobKey(Long taskId) {
return JobKey.jobKey(QuartzConst.JOB_KEY_PREFIX + taskId);
}
/**
* 构建任务键对象
*/
public static JobKey getJobKey(Long jobId, String jobGroup)
{
return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId, jobGroup);
}
/**
* 获取jobKey
*/
public static JobKey getJobKey(Long jobId)
{
return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId);
}
public void runJobOnce(ScheduleJob scheduleJob) throws SchedulerException{
checkNotNull(scheduleJob);
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
scheduler.triggerJob(jobKey);
}
protected static JobKey getJobKey(final long snifferId, final long logSourceId) {
return JobKey.jobKey(snifferId + ":" + logSourceId, "SNIFFER:" + snifferId);
}
public void resumeJob(ScheduleJob scheduleJob) throws SchedulerException{
checkNotNull(scheduleJob);
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
scheduler.resumeJob(jobKey);
}
/**
* 获取jobKey
*/
public static JobKey getJobKey(Long jobId)
{
return JobKey.jobKey(ScheduleConstants.TASK_CLASS_NAME + jobId);
}
/**
* 恢复任务
*
* @param scheduler scheduler
* @param jobName jobName
* @param jobGroup jobGroup
*/
public static void resumeJob(Scheduler scheduler, String jobName, String jobGroup) throws SchedulerException {
log.info("恢复定时任务 jobName = {}, jobGroup = {}", jobName, jobGroup);
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
scheduler.resumeJob(jobKey);
}
/**
* 获取jobKey
*
* @param jobName the job name
* @param jobGroup the job group
* @return the job key
*/
private static JobKey getJobKey(String jobName, String jobGroup) {
return JobKey.jobKey(jobName, jobGroup);
}