标签:dep tput lin tst exce hot build width relative
1.pom文件
<?xml version="1.0" encoding="UTF-8"?> <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.zy</groupId> <artifactId>spring-boot-task-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-task-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.启动类
package com.zy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication // 支持异步线程,需要异步调用的service层方法上面加上@Async注解,二者联合,可起作用 @EnableAsync
//开启基于注解的定时任务
@EnableScheduling
public class SpringBootTaskDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTaskDemoApplication.class, args); } }
3.controller测试类
package com.zy.controller; import com.zy.service.AsyncServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RequestMapping("/task/") @RestController public class AsyncController { @Autowired private AsyncServiceImpl asyncService; @RequestMapping("/async") public Object async(String name){ return name; } }
4.service层
4.1异步任务实现类
package com.zy.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service("asyncService") public class AsyncServiceImpl { @Async public String async(String name){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return "good morning:" + name; } }
4.2定时任务实现类
package com.zy.service; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledServiceImpl { /** * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几). * 0 * * * * MON-FRI * 【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次 * 【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次 * 【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次 * 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次 * 【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次; */ // @Scheduled(cron = "0 * * * * MON-SAT") //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") // @Scheduled(cron = "0-4 * * * * MON-SAT") @Scheduled(cron = "0/50 * * * * MON-SAT") //每50秒执行一次 public void saySchedule(){ System.out.println("saySchedule"); } }
springboot中的任务(异步任务--定时任务--邮件任务)
标签:dep tput lin tst exce hot build width relative
原文地址:https://www.cnblogs.com/psy-code/p/9572198.html