zoukankan      html  css  js  c++  java
  • 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);
                    }
    }
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    servlet异步处理机制
    分析logfilter+session
    java web后台工作原理
    xml的作用
    本学期学习目标 企业级运用和互联网运用的区别
    JAVA EE 思维导图
    第六周
    第五周
    第四周作业
    javaee第三周
  • 原文地址:https://www.cnblogs.com/name-lizonglin/p/13503280.html
Copyright © 2011-2022 走看看