com.github.hengboy.job.schedule.http.resource.ScheduleHttpClient Maven / Gradle / Ivy
/*
* Copyright [2019] [恒宇少年 - 于起宇]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.hengboy.job.schedule.http.resource;
import com.github.hengboy.job.core.exception.JobException;
import com.github.hengboy.job.core.http.RestUrlConstants;
import com.github.hengboy.job.core.wrapper.support.CronJobWrapper;
import com.github.hengboy.job.core.wrapper.support.LoopJobWrapper;
import com.github.hengboy.job.core.wrapper.support.OnceJobWrapper;
import com.github.hengboy.job.schedule.quartz.QuartzJobContext;
import com.github.hengboy.job.schedule.resource.MicroJobScheduleResource;
import com.github.hengboy.job.schedule.store.JobStore;
import com.github.hengboy.job.schedule.store.model.JobDetail;
import com.github.hengboy.job.schedule.store.model.JobExecuteQueue;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
/**
* 调度器http rest interface
*
* @author:恒宇少年 - 于起宇
*
* DateTime:2019-02-12 15:44
* Blog:http://blog.yuqiyu.com
* WebSite:http://www.jianshu.com/u/092df3f77bca
* Gitee:https://gitee.com/hengboy
* GitHub:https://github.com/hengyuboy
*/
@Path(RestUrlConstants.SCHEDULE)
public class ScheduleHttpClient {
/**
* 添加cron任务
*
* @param cronJobWrapper cron任务对象
* @throws JobException 任务异常
*/
@POST
@Path("/new-cron-job")
@Consumes(MediaType.APPLICATION_JSON)
public void newCronJob(CronJobWrapper cronJobWrapper) throws JobException {
try {
// 添加任务
String jobQueueId = MicroJobScheduleResource.getJobStore().addJob(cronJobWrapper);
// add cron quartz job
QuartzJobContext.newCronJob(MicroJobScheduleResource.getScheduler(), cronJobWrapper, jobQueueId);
} catch (Exception e) {
throw new JobException("Add new cron job fail.", e);
}
}
/**
* 添加loop任务
*
* @param loopJobWrapper loop任务对象
* @throws JobException 任务异常
*/
@POST
@Path("/new-loop-job")
@Consumes(MediaType.APPLICATION_JSON)
public void newLoopJob(LoopJobWrapper loopJobWrapper) throws JobException {
try {
// 添加任务
String jobQueueId = MicroJobScheduleResource.getJobStore().addJob(loopJobWrapper);
// add loop quartz job
QuartzJobContext.newLoopJob(MicroJobScheduleResource.getScheduler(), loopJobWrapper, jobQueueId);
} catch (Exception e) {
throw new JobException("Add new loop job fail.", e);
}
}
/**
* 添加once任务
*
* @param onceJobWrapper once任务对象
* @throws JobException 任务异常
*/
@POST
@Path("/new-once-job")
@Consumes(MediaType.APPLICATION_JSON)
public void newOnceJob(OnceJobWrapper onceJobWrapper) throws JobException {
try {
// 添加任务
String jobQueueId = MicroJobScheduleResource.getJobStore().addJob(onceJobWrapper);
// add once quartz job
QuartzJobContext.newOnceJob(MicroJobScheduleResource.getScheduler(), onceJobWrapper, jobQueueId);
} catch (Exception e) {
throw new JobException("Add new loop job fail.", e);
}
}
/**
* 根据任务队列key删除任务
*
* @param jobQueueKey 任务队列key
* @throws JobException 任务异常
*/
@DELETE
@Path("/remove-job/{jobQueueKey}")
public void removeJob(@PathParam("jobQueueKey") String jobQueueKey) throws JobException {
try {
JobStore jobStore = MicroJobScheduleResource.getJobStore();
// 根据任务key查询任务队列信息
JobExecuteQueue jobExecuteQueue = jobStore.selectJobQueueByKey(jobQueueKey);
// 删除数据库任务队列信息
jobStore.removeJob(jobExecuteQueue.getJeqId());
// 查询任务详情
JobDetail jobDetail = jobStore.selectJob(jobExecuteQueue.getJeqJobId());
QuartzJobContext.removeJob(MicroJobScheduleResource.getScheduler(), jobExecuteQueue.getJeqId(), jobDetail.getMjdJobKey());
} catch (Exception e) {
throw new JobException("Exceptional information encountered when deleting job", e);
}
}
/**
* 根据任务队列key验证任务是否存在
*
* @param jobQueueKey 任务队列key
* @return true:任务存在,false:不存在
* @throws JobException 任务异常
*/
@GET
@Path("/exists/{jobQueueKey}")
public boolean checkExists(@PathParam("jobQueueKey") String jobQueueKey) throws JobException {
return MicroJobScheduleResource.getJobStore().selectJobQueueByKey(jobQueueKey) != null;
}
}