下面列出了org.quartz.Scheduler#pauseJob ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 创建定时任务
*/
public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException
{
Class<? extends Job> jobClass = getQuartzJobClass(job);
// 构建job信息
Long jobId = job.getJobId();
String jobGroup = job.getJobGroup();
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(getJobKey(jobId, jobGroup)).build();
// 表达式调度构建器
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);
// 按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobId, jobGroup))
.withSchedule(cronScheduleBuilder).build();
// 放入参数,运行时的方法可以获取
jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
// 判断是否存在
if (scheduler.checkExists(getJobKey(jobId, jobGroup)))
{
// 防止创建时存在数据问题 先移除,然后在执行创建操作
scheduler.deleteJob(getJobKey(jobId, jobGroup));
}
scheduler.scheduleJob(jobDetail, trigger);
// 暂停任务
if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue()))
{
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
}
}
/**
* 创建定时任务
*/
public static void createScheduleJob(Scheduler scheduler, SysJob job) throws SchedulerException, TaskException
{
Class<? extends Job> jobClass = getQuartzJobClass(job);
// 构建job信息
Long jobId = job.getJobId();
String jobGroup = job.getJobGroup();
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(getJobKey(jobId, jobGroup)).build();
// 表达式调度构建器
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
cronScheduleBuilder = handleCronScheduleMisfirePolicy(job, cronScheduleBuilder);
// 按新的cronExpression表达式构建一个新的trigger
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(jobId, jobGroup))
.withSchedule(cronScheduleBuilder).build();
// 放入参数,运行时的方法可以获取
jobDetail.getJobDataMap().put(ScheduleConstants.TASK_PROPERTIES, job);
// 判断是否存在
if (scheduler.checkExists(getJobKey(jobId, jobGroup)))
{
// 防止创建时存在数据问题 先移除,然后在执行创建操作
scheduler.deleteJob(getJobKey(jobId, jobGroup));
}
scheduler.scheduleJob(jobDetail, trigger);
// 暂停任务
if (job.getStatus().equals(ScheduleConstants.Status.PAUSE.getValue()))
{
scheduler.pauseJob(ScheduleUtils.getJobKey(jobId, jobGroup));
}
}
/**
* 暂停任务
*/
public static void pauseJob(Scheduler scheduler, Long jobId)
{
try
{
scheduler.pauseJob(getJobKey(jobId));
}
catch (SchedulerException e)
{
log.error("pauseJob 异常:", e);
}
}
/**
* 暂停任务
*/
public static void pauseJob(Scheduler scheduler, Long jobId)
{
try
{
scheduler.pauseJob(getJobKey(jobId));
}
catch (SchedulerException e)
{
log.error("pauseJob 异常:", e);
}
}
private void pauseJob(String group, String name) throws SchedulerException {
if (StringUtils.isEmpty(group) || StringUtils.isEmpty(name)) {
return;
}
Scheduler scheduler = factory.getScheduler();
JobKey jobKey = new JobKey(name, group);
if (!scheduler.checkExists(jobKey)) {
LOGGER.warn("Job({},{}) does not exist.", jobKey.getGroup(), jobKey
.getName());
throw new GriffinException.NotFoundException
(JOB_KEY_DOES_NOT_EXIST);
}
scheduler.pauseJob(jobKey);
}
public void suspendJob(String name, String group) {
if(!getIsEnabled()){
throw new FixFlowScheduleException(ExceptionCode.QUARZTEXCEPTION_ISENABLE);
}
Scheduler scheduler = getScheduler();
try {
scheduler.pauseJob(new JobKey(name,group));
} catch (SchedulerException e) {
throw new FixFlowException(e.getMessage(),e);
}
}
public static void pauseJob(String jobName, String jobGroup, Scheduler scheduler) throws SchedulerException {
scheduler.pauseJob(getJobKey(jobName, jobGroup));
}
@SuppressWarnings("unchecked")
public RepoService(ApplicationContext applicationContext) throws Exception
{
this.applicationContext = applicationContext;
this.publicApiContext = new PublicApiTestContext(applicationContext);
this.authenticationService = (MutableAuthenticationService)applicationContext.getBean("AuthenticationService");
this.siteService = (SiteService)applicationContext.getBean("SiteService");
this.activityService = (ActivityService)applicationContext.getBean("activityService");
this.fileFolderService = (FileFolderService)applicationContext.getBean("FileFolderService");
this.contentService = (ContentService)applicationContext.getBean("ContentService");
this.commentService = (CommentService)applicationContext.getBean("CommentService");
this.nodeService = (NodeService)applicationContext.getBean("NodeService");
this.preferenceService = (PreferenceService)applicationContext.getBean("PreferenceService");
this.taggingService = (TaggingService)applicationContext.getBean("TaggingService");
this.ratingService = (RatingService)applicationContext.getBean("RatingService");
this.tenantService = (TenantService)applicationContext.getBean("tenantService");
this.tenantAdminService = (TenantAdminService)applicationContext.getBean("tenantAdminService");
this.personService = (PersonService)applicationContext.getBean("PersonService");
this.contentStoreCleaner = (ContentStoreCleaner)applicationContext.getBean("contentStoreCleaner");
this.postDAO = (ActivityPostDAO)applicationContext.getBean("postDAO");
this.nodeRatingSchemeRegistry = (NamedObjectRegistry<RatingScheme>)applicationContext.getBean("nodeRatingSchemeRegistry");
this.cociService = (CheckOutCheckInService)applicationContext.getBean("CheckoutCheckinService");
this.favouritesService = (FavouritesService)applicationContext.getBean("FavouritesService");
this.dictionaryService = (DictionaryService)applicationContext.getBean("dictionaryService");
this.invitationService = (InvitationService)applicationContext.getBean("InvitationService");
this.lockService = (LockService)applicationContext.getBean("LockService");
this.cmisConnector = (CMISConnector)applicationContext.getBean("CMISConnector");
this.activities = (Activities)applicationContext.getBean("activities");
this.hiddenAspect = (HiddenAspect)applicationContext.getBean("hiddenAspect");
this.networksService = (NetworksService)applicationContext.getBean("networksService");
this.namespaceService = (NamespaceService)applicationContext.getBean("namespaceService");
this.transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper");
Scheduler scheduler = (Scheduler)applicationContext.getBean("schedulerFactory");
CronTrigger contentStoreCleanerTrigger = (CronTrigger)applicationContext.getBean("contentStoreCleanerTrigger");
scheduler.pauseJob(contentStoreCleanerTrigger.getJobKey());
ChildApplicationContextFactory activitiesFeed = (ChildApplicationContextFactory)applicationContext.getBean("ActivitiesFeed");
ApplicationContext activitiesFeedCtx = activitiesFeed.getApplicationContext();
this.postLookup = (PostLookup)activitiesFeedCtx.getBean("postLookup");
this.feedGenerator = (FeedGenerator)activitiesFeedCtx.getBean("feedGenerator");
this.feedGeneratorJobDetail = (JobDetail)activitiesFeedCtx.getBean("feedGeneratorJobDetail");
this.postLookupJobDetail = (JobDetail)activitiesFeedCtx.getBean("postLookupJobDetail");
this.feedCleanerJobDetail = (JobDetail)activitiesFeedCtx.getBean("feedCleanerJobDetail");
this.postCleanerJobDetail = (JobDetail)activitiesFeedCtx.getBean("postCleanerJobDetail");
this.feedNotifierJobDetail = (JobDetail)activitiesFeedCtx.getBean("feedNotifierJobDetail");
this.feedCleaner = (FeedCleaner)activitiesFeedCtx.getBean("feedCleaner");
// Pause activities jobs so that we aren't competing with their scheduled versions
scheduler.pauseJob(feedGeneratorJobDetail.getKey());
scheduler.pauseJob(postLookupJobDetail.getKey());
scheduler.pauseJob(feedCleanerJobDetail.getKey());
scheduler.pauseJob(postCleanerJobDetail.getKey());
scheduler.pauseJob(feedNotifierJobDetail.getKey());
this.systemNetwork = new TestNetwork(TenantService.DEFAULT_DOMAIN, true);
}
void pauseJob(JobDetail jobDetail, Scheduler scheduler) throws SchedulerException {
scheduler.pauseJob(jobDetail.getName(), jobDetail.getGroup());
}
@Override
void pauseJob(JobDetail jobDetail, Scheduler scheduler) throws SchedulerException {
scheduler.pauseJob(jobDetail.getKey());
}
@PUT
@RolesAllowed({"IbisDataAdmin", "IbisAdmin", "IbisTester"})
@Path("/schedules/{groupName}/job/{jobName}")
@Relation("schedules")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response trigger(@PathParam("jobName") String jobName, @PathParam("groupName") String groupName, LinkedHashMap<String, Object> json) throws ApiException {
Scheduler scheduler = getScheduler();
String commandIssuedBy = servletConfig.getInitParameter("remoteHost");
commandIssuedBy += servletConfig.getInitParameter("remoteAddress");
commandIssuedBy += servletConfig.getInitParameter("remoteUser");
if(log.isInfoEnabled()) log.info("trigger job jobName [" + jobName + "] groupName [" + groupName + "] " + commandIssuedBy);
JobKey jobKey = JobKey.jobKey(jobName, groupName);
String action = ""; //PAUSE,RESUME,TRIGGER
for (Entry<String, Object> entry : json.entrySet()) {
String key = entry.getKey();
if(key.equalsIgnoreCase("action")) {//Start or stop an adapter!
action = (String) entry.getValue();
}
}
try {
if("pause".equals(action)) {
scheduler.pauseJob(jobKey);
}
else if("resume".equals(action)) {
scheduler.resumeJob(jobKey);
}
else if("trigger".equals(action)) {
scheduler.triggerJob(jobKey);
}
else {
throw new ApiException("no (valid) action provided! Expected one of PAUSE,RESUME,TRIGGER");
}
} catch (SchedulerException e) {
throw new ApiException("Failed to "+action+" job", e);
}
return Response.status(Response.Status.OK).build();
}
/**
* 暂停任务
*
* @param scheduler scheduler
* @param jobName jobName
* @param jobGroup jobGroup
*/
public static void pauseJob(Scheduler scheduler, String jobName, String jobGroup) throws SchedulerException {
log.info("暂定定时任务 jobName = {}, jobGroup = {}", jobName, jobGroup);
JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
scheduler.pauseJob(jobKey);
}
/**
* 暂停Job
* @param jobDetail
* @param scheduler
* @throws SchedulerException
*/
public static void pauseJob(JobDetail jobDetail, Scheduler scheduler) throws SchedulerException {
scheduler.pauseJob(jobDetail.getKey());
}