zoukankan      html  css  js  c++  java
  • SpringBoot 使用定时任务

    SpringBoot使用定时任务

    pom文件 

    <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.example</groupId>
    	<artifactId>mydemo</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>mydemo</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<maven.compiler.source>1.8</maven.compiler.source>
    		<maven.compiler.target>1.8</maven.compiler.target>
    	</properties>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.9.RELEASE</version>
    		<relativePath />
    	</parent>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    	<build>
    		<plugins>
    			<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>
    

      

    定时任务

    package com.example.mydemo.task;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.TaskScheduler;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    
    @Configuration
    @EnableScheduling
    public class DemoTask {
    
    	@Bean
    	public TaskScheduler taskScheduler() {
    		ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    		taskScheduler.setPoolSize(2); //并行执行个数
    		taskScheduler.setThreadNamePrefix("hello"); //设置线程前缀
    		return taskScheduler;
    	}
    
    	@Scheduled(fixedDelay = 5000)
    	public void cronTask() {
    		System.out.println(Thread.currentThread().getName() + " 每5秒执行一次");
    		try {
    			Thread.sleep(10000);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	@Scheduled(cron = "*/5 * * * * *")
    	public void cronTask2() {
    		System.out.println(Thread.currentThread().getName() + " 每3秒执行一次");
    	}
    }
    

      

    package com.example.mydemo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    public class App {
    	public static void main(String[] args) {
    		SpringApplication app = new SpringApplication(App.class);
    		app.run(args);
    
    	}
    }
    

      

  • 相关阅读:
    C++ Primer 第五版-1.1
    C++ Primer 第五版笔记-1.0
    TouchID
    正则表达式
    duplicate symbol 错误
    第三方登录
    AFNetWorking
    IOS---通知
    左右点击--日期增减
    Xcode相关设置
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/14176989.html
Copyright © 2011-2022 走看看