quartz简单实例实现

application.yml

quartz:
  corePoolSize: 10 #核心池大小
  maxPoolSize: 200 #最大池大小
  queueCapacity: 10 #队列容量

测试类:

  AsyncConfig.java(读取配置)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Value("${quartz.corePoolSize}")
    private int  corePoolSize;

    @Value("${quartz.maxPoolSize}")
    private int maxPoolSize;

    @Value("${quartz.queueCapacity}")
    private int queueCapacity;

    @Bean
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}
  TestJob.java(注:@Schedules 这个注解控制时间,多长时间调用一次)
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class TestJob {

    /*@Scheduled(cron = "0/2 * * * * *")  // 2秒执行一次
    public void job(){
        System.out.println("springboot - EnableScheduling ");
    }*/
}

  TestQuartz.java(有备注,不说了)

import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

@DisallowConcurrentExecution
public class TestQuartz extends QuartzJobBean {

    /**
     * 执行定时任务
     * @param context
     * @throws JobExecutionException
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
       // System.out.println("quartz定时任务");
    }
}

  TestQuartzConfig.java

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TestQuartzConfig {

    @Bean
    public JobDetail testQuartzDetail(){
        return JobBuilder.newJob(TestQuartz.class)
                .withIdentity("zxf").storeDurably()
                .requestRecovery(true).build();
    }


    @Bean
    public Trigger testQuartzTrigger(){
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("name","zxf");

        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder
                .simpleSchedule()
                .withIntervalInSeconds(2) //设置时间周期单位秒
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(testQuartzDetail())
                .withIdentity("zxf")
                .withSchedule(simpleScheduleBuilder)
                .usingJobData(jobDataMap)
                .build();
    }
}

  QuartzApplication.java(@EnableScheduling 这个注解别忘了,没他不能运行)

import org.apache.catalina.connector.Connector;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;
//@EnableScheduling
@SpringBootApplication
@EnableTransactionManagement // 启注解事务管理,等同于xml配置方式的 <tx:annotation-driven />
@ServletComponentScan //开启 过滤器 监听器
@RestController
public class QuartzApplication {

    public static void main(String[] args) {
        SpringApplication.run(QuartzApplication.class, args);
                }
}

 

posted @ 2020-08-14 17:50  四叶草的诗雨  阅读(325)  评论(0编辑  收藏  举报