11、Spring
3.2 多线程
- Spring通过任务执行器(TaskExecutor)来实现多线程和的并发编程
- 任务执行器(TaskExecutor)通过线程池任务执行器(ThreadPoolTaskExecutor)实现
- 在实际中开发的任务都是异步的,因此需要在Config中开启异步支持,在Bean中声明异步方法
Config
/* config
1、利用@EnableAsync开启异步支持
2、重写getAsyncExecutor()方法:创建实例.set值、返回一个ThreadPoolTaskExecutor
3、重写
*/
@Configration
@ComponentScan("com.wisely.highlight_spring4.ch3.taskexecutor")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer{
@Override
public Executor getAsyncExecutor(){
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCoreSize(5);
taskExecutor.setMaxSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler(){
return null;
}
}
Bean
/* Bean
1、方法注解异步,注解了异步的方法自动使用TaskExecutor
*/
public class AsyncTaskService{
@Async
public void executorAsyncTask(Integer i){
System.out.println("执行异步任务:"+i)
}
@Async
public void executorAsyncTaskPlus(Integer i){
System.out.println("执行异步任务+1:"+(i+1))
}
}
Main
/* Main:并发,而不是顺序执行
*/
public class Main {
public static void main(String [] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class)
for(int i = 0;i<10;i++){
asyncTaskService.executorAsyncTask(i);
asyncTaskService.executorAsyncTaskPlus(i);
}
context.close();
}
}
3.3 计划任务
Config
/*
计划任务
*/
/* config
1、开启计划任务支持
*/
@Configration
@ComponentScan("com.wisely.highlight_spring4.ch3.taskscheduler")
@EnableScheduling
public class TaskAScheduleConfig{
}
Bean
/* Bean
1、为计划任务添加注解声明:用两种方式
*/
public class ScheduledTaskService {
pvivate stetic final SimpleDateFormat dateFormat = new impleDateFormat("HH:MM:SS");
@Scheduled(fixRate = 5000)
public void reportCurrentTime(){
System.out.println("每隔5秒执行一次"+dateFormat.format(new Date()))
}
@Scheduled(cron = "0 28 11 ? * *")
public void fixTimeCurrentTime(){
System.out.println("在指定时间"+dateFormat.format(new Date())+"执行")
}
}
Main
/* Main
*/
public class Main {
public static void main (string [] args){
AnnotationConfigApplicationContext Context = new AnnotationConfigApplicationContext();
}
}
3.4 条件注解@Conditional
@Conditonal注解:需要满足一个特定的条件时创建一个特定的Bean。
示例:判断当前环境状态,如果是windows,就输出windows的Bean,如果是Linux,就输出Linux的Bean。
判定Windows的条件
/* 判断条件一:是不是windows
*/
public class WindowsCondition implements Condition {
public boolean match (ConditionContext context,AnnotatedTypeMetadata metadatea){
return context.getEnvironment().getProperty("os.name").contains("Windows")
}
}
判定Linux的条件
/* 判断条件二:是不是Linux
*/
public class LinuxCondition implements Condition {
public boolean matches (ConditionContext context,AnnotatedTypeMetadata metadatea){
return context.getEnvironment().getProperty("os.name").contains("Linux")
}
}
接口
/* 接口
*/
public interface ListService{
public String showListCmd();
}
Windows下要创建的Bean
/*
Windows下要创建的Bean
*/
public class WindowsListService implements ListService{
@Override
public String showListCmd(){
return "dir"
}
}
Linux下要创建的Bean
/*
Linux下要创建的Bean
*/
public class implements ListService{
@Override
public String showListCmd(){
return "ls"
}
}
Config
/* Config
*/
@Configration
public class ConditionConfig {
@Bean
@Condition(WindowsCondition.class)
public ListService windowsListService(){
return new WindowsListService();
}
@Bean
@Condition(LinuxCondition)
public ListService linuxListService(){
return new LinuxListService();
}
}
Main:加载Config
/* Main
*/
public class Main {
public static void main (String []args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (ConditionConfig.class);
ListService listService = context.getBean(ListService.class);
System .out.println(context.getEnvironment().getProperty("os.name")+"系统下的列表命令是:"+listService.showListCmd());
}
}