• 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);
                    }
    }
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    使用tidylib解决不规则网页问题
    Python读取Json字典写入Excel表格的方法
    Python version 3.6 required, which was not found in the registry错误解决
    pip错误-failed to create process/fatal error in launcher
    Scrapy安装
    mongo数据库导入导出数据
    Windows命令
    Requests中文乱码解决方案
    mysql 1005 错误
    OC 内存管理-02 ARC 内存管理
  • 原文地址:https://www.cnblogs.com/name-lizonglin/p/13503280.html
Copyright © 2020-2023  润新知