时间:2021-07-01 10:21:17 帮助过:20人阅读
2.配置文件
quartz.properties
mongo有密码时配置文件如下: org.quartz.jobStore.class=com.novemberain.quartz.mongodb.MongoDBJobStore org.quartz.jobStore.mongoUri=mongodb://用户名:密码@10.10.17.8:27017,10.10.17.9:27017,10.10.17.10:27017/数据库 org.quartz.jobStore.dbName=数据库 org.quartz.scheduler.instanceName = MyScheduler org.quartz.threadPool.threadCount=3 org.quartz.jobStore.collectionPrefix=qrtz org.quartz.jobStore.misfireThreshold = 1800000 mongo无密码时配置文件如下: org.quartz.jobStore.class=com.novemberain.quartz.mongodb.MongoDBJobStore org.quartz.jobStore.mongoUri= mongodb://localhost:27020 org.quartz.jobStore.dbName=quartz org.quartz.jobStore.collectionPrefix=qrtz org.quartz.threadPool.threadCount=100
3.定义job
import org.apache.commons.lang.time.DateFormatUtils; import org.quartz.*; import java.util.Date; @DisallowConcurrentExecution public class HelloJob implements InterruptableJob { private boolean stop = false; public void execute(JobExecutionContext context) throws JobExecutionException { if (stop) { System.out.println("--stop--"); return; } String time = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); System.out.println(time); JobKey key = context.getJobDetail().getKey(); JobDataMap dataMap = context.getJobDetail().getJobDataMap(); String jobSays = dataMap.getString("jobSays"); String myFloatValue = dataMap.get("myFloatValue").toString(); System.err.println("instance " + key + "of HelloJob says :" + jobSays + ", and val is : " + myFloatValue); } public void interrupt() throws UnableToInterruptJobException { stop = true; } }
4.job调用
package com.itstudy; import org.quartz.*; import org.quartz.impl.StdSchedulerFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component @Order(value=1) public class MyJobRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { SchedulerFactory schedFact = new StdSchedulerFactory(); Scheduler sched = schedFact.getScheduler(); sched.start(); try { JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("myJob", "group1") .usingJobData("jobSays", "Hello World!") .usingJobData("myFloatValue", 3.141f) .build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("myTrigger", "group1") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5) .repeatForever()) .build(); sched.scheduleJob(job, trigger); } catch (ObjectAlreadyExistsException e) { System.err.println("发现任务已经在数据库存在了,直接从数据库里运行:" + e.getMessage()); //中断执行 // sched.interrupt(new JobKey("myJob","group1")); // sched.unscheduleJob(new TriggerKey("myTrigger","group1")); //停止执行 //sched.pauseJob(new JobKey("myJob","group1")); } TimeUnit.SECONDS.sleep(16); sched.shutdown(true); System.out.println("--end--"); } }
5.应用启动
package com.itstudy; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import javax.servlet.MultipartConfigElement; /** * Hello world! */ @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication app = new SpringApplication(App.class); //关闭banner app.setBannerMode(Banner.Mode.OFF); app.run(args); } }
Quartz持久化到mongodb
标签:style world mode its www repeat stat interrupt maven