springboot中集成quzrtz ,持久到mongodb
1.pom引用
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.itstudy</groupId>
<artifactId>upload-demo</artifactId>
<version>1.0-SNAPSHOT</version> <name>upload-demo</name> <url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency> <dependency>
<groupId>com.novemberain</groupId>
<artifactId>quartz-mongodb</artifactId>
<version>2.0.0-rc2</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
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); }
}