• Spring boot 集成三种定时任务方式


    三种定时任务方式分别为

    1 org.springframework.scheduling.annotation.Scheduled
    2 java.util.concurrent.ScheduledExecutorService
    3 java.util.Timer

    示例如下:

    1.   新建Maven项目  schedule

    2.   pom.xml

    <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.java</groupId>
        <artifactId>schedule</artifactId>
        <version>1.0.0</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.5.RELEASE</version>
        </parent>
    
    
        <dependencies>
    
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <!-- 热部署 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.8.RELEASE</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>provided</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <finalName>${project.artifactId}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <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>

     

    3.   ScheduleStarter.java

    package com.java;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    @EnableScheduling
    @SpringBootApplication
    public class ScheduleStarter {
    
        public static void main(String[] args) {
            SpringApplication.run(ScheduleStarter.class, args);
        }
    
    }

    4.   ScheduledDemo.java

    package com.java.schedule;
    
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ScheduledDemo {
    
        @Scheduled(cron = "0/5 * * * * *")
        public void demoTask() {
            System.out.println("[ ScheduledDemo ]		" + System.currentTimeMillis());
        }
    
    }

    5.   ScheduledExecutorDemo.java

    package com.java.schedule;
    
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ScheduledThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class ScheduledExecutorDemo {
    
        private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(5);
    
        @PostConstruct
        public void demoTask() {
            executor.scheduleAtFixedRate(new Runnable() {
    
                @Override
                public void run() {
                    System.out.println("[ ScheduledExecutorDemo ]	" + System.currentTimeMillis());
                }
            }, 0, 5, TimeUnit.SECONDS);
    
        }
    
        @PreDestroy
        public void destory() {
            executor.shutdown();
        }
    
    }

     

    6.   TimerDemo.java

    package com.java.schedule;
    
    import java.util.Timer;
    import java.util.TimerTask;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class TimerDemo {
    
        private Timer timer = new Timer(true);
    
        @PostConstruct
        public void demoTask() {
            timer.scheduleAtFixedRate(new TimerTask() {
    
                @Override
                public void run() {
                    System.out.println("[ TimerDemo ]			" + System.currentTimeMillis());
                }
            }, 0, 5000);
    
        }
    
        @PreDestroy
        public void destory() {
            timer.cancel();
        }
    
    }

    7.   运行ScheduleStarter.java,启动测试

    控制台每隔5秒打印如下信息

    [ ScheduledDemo ]        1548156125005
    [ TimerDemo ]            1548156127556
    [ ScheduledExecutorDemo ]    1548156127566

    三种定时任务都正常运行!

    .

  • 相关阅读:
    HTML表格布局例子
    WCF分布式开发必备知识(2):.Net Remoting (转)
    WCF分布式开发必备知识(1):MSMQ消息队列(转)
    WCF数据契约与序列化(转)
    Asp.net中图片存储数据库以及页面读取显示通用方法详解附源码下载
    2010年初的一点随想
    Windows7旗舰版磁盘分区详解—附分区步骤截图
    AjaxControltoolkit(工具包)安装步骤说明
    Windows7安装IIS中关于Windows 系列于谷歌Chrome系统争议一点联想
    Oracle 10G中关于约束在表和列中使用详解.
  • 原文地址:https://www.cnblogs.com/jonban/p/10305654.html
Copyright © 2020-2023  润新知