springbootquartz设置线程数(SpringBoot大神教程3:集成Quartz)
springbootquartz设置线程数(SpringBoot大神教程3:集成Quartz)
要点- pom.xml引入Quartz依赖
- 配置quartz Configuration
- application.properties配置Quartz
- 下载SQL脚步,导入到数据库
- Job中实现业务
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency> 2. 配置Quartz Configuration
package com.ly.job; @Configuration public class QuartzConfig { //cron表达式 如cron=0 0/5 * * * ? @Value("${push.info.cron}") private String pushInfoCron; //设置Job @Bean public JobDetail pushOrderInfoJobDetail() { return Jobbuilder.newJob(PushOrderInfoJob.class).withIdentity("pushOrderInfoJob").storeDurably().build(); } //设置Trigger @Bean public Trigger pushOrderInfoJobTrigger() { CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(pushInfoCron); return TriggerBuilder.newTrigger().forJob(pushOrderInfoJobDetail()) .withIdentity("pushOrderInfoJob") .withSchedule(scheduleBuilder) .build(); } } 3. application.properties配置Quartz
spring.quartz.properties.org.quartz.scheduler.instanceName=clusteredScheduler spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO spring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdjdbcDelegate spring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_ spring.quartz.properties.org.quartz.jobStore.isClustered=true spring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval=10000 spring.quartz.properties.org.quartz.jobStore.useProperties=false spring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool spring.quartz.properties.org.quartz.threadPool.threadCount=10 spring.quartz.properties.org.quartz.threadPool.threadPriority=5 spring.quartz.properties.org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=true spring.quartz.job-store-type=jdbc 4. 下载SQL脚步,导入到数据库
去官网下载:http://www.quartz-scheduler.org/downloads/
根据版本不同,脚步一般在这2个地方
/docs/dbTables org/quartz/impl/jdbcjobstore 5. Job中实现业务
@Configuration @DisallowConcurrentExecution public class PushOrderInfoJob extends QuartzJobBean { private static final Logger logger = LoggerFactory.getLogger(PushOrderInfoJob.class); /** * 这儿写业务 **/ @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { logger.info("-----[PushOrderInfoJob]begin----"); logger.info("-----[PushOrderInfoJob]end----"); } } 6.如果你觉得好,可以使用我发布到iOS APP Store的产品