一、普通的异步任务执行: spring boot 默认装配了一个ThreadPoolTaskExecutor类型的bean,用来执行异步任务,可以通过application.properties对该bean进行配置:
spring.task.execution.pool.max-size=16
spring.task.execution.pool.queue-capacity=100
spring.task.execution.pool.keep-alive=10s
示例 1.在启动类加上注解@EnableAsync,用于支持异步任务
package cn.edu.tju;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
}
2.在service中使用@Async注解定义异步任务
package cn.edu.tju.service;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Locale;
@Service
public class BasicService {
@Async
public void printInfo(){
System.out.println("线程名称:"+Thread.currentThread().getName());
System.out.println("当前时间:"+new Date().toLocaleString());
}
}
3.在controller中注入service,并调用异步任务的方法
package cn.edu.tju.controller;
import cn.edu.tju.domain.UserInfo;
import cn.edu.tju.service.BasicService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestController4 {
@Autowired
private BasicService basicService;
@RequestMapping("/test5")
public String getMessage5() throws Exception{
basicService.printInfo();
System.out.println("______________________________________");
System.out.println("线程名称:"+Thread.currentThread().getName());
String result="hello";
return result;
}
}
4.启动程序并访问/test5接口,看到控制台输出如下 可以看到controller中的逻辑早于service中异步任务的执行
二、定时任务的执行: spring boot默认装配了ThreadPoolTaskScheduler用于支持定时任务的执行, 可以通过application.properties对该bean进行配置:
spring.task.scheduling.thread-name-prefix=schedulingspring.task.scheduling.pool.size=2
使用方法如下: 1.在启动类加@EnableScheduling注解:
package cn.edu.tju;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
}
2.定义service,并在某个方法上加@Scheduled注解
package cn.edu.tju.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class ScheduledService {
//每5秒执行一次
@Scheduled(fixedRate = 5000)
private void logTime(){
System.out.println("当前时间: "+new Date().toLocaleString());
}
}
3.启动程序,并运行,可以看到控制台每5秒打印一次当前时间
|