Skip to content

Commit 4b7ed50

Browse files
committed
【Spring】Spring高级话题-多线程-TaskExecutor
1 parent 9bb6b5c commit 4b7ed50

File tree

10 files changed

+376
-98
lines changed

10 files changed

+376
-98
lines changed

springBoot/.idea/workspace.xml

Lines changed: 136 additions & 98 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

springBoot/src/main/java/cn/hncu/p3/p1_SpringAware/AwareService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.apache.commons.io.IOUtils;
44
import org.springframework.beans.factory.BeanNameAware;
5+
import org.springframework.context.MessageSource;
6+
import org.springframework.context.MessageSourceAware;
57
import org.springframework.context.ResourceLoaderAware;
68
import org.springframework.core.io.Resource;
79
import org.springframework.core.io.ResourceLoader;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.hncu.p3.p2_TaskExecutor;
2+
3+
import org.springframework.scheduling.annotation.Async;
4+
import org.springframework.stereotype.Service;
5+
6+
/**
7+
* Created with IntelliJ IDEA.
8+
* User: 陈浩翔.
9+
* Date: 2016/11/18.
10+
* Time: 上午 10:57.
11+
* Explain:任务执行类
12+
*/
13+
@Service
14+
public class AsyncTaskService {
15+
16+
@Async
17+
public void executeAsyncTask(Integer i){
18+
System.out.println("executeAsyncTask:"+i);
19+
}
20+
21+
@Async
22+
public void executeAsyncTaskPlus(Integer i){
23+
System.out.println("executeAsyncTaskPlus:"+i);
24+
}
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.hncu.p3.p2_TaskExecutor;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
/**
6+
* Created with IntelliJ IDEA.
7+
* User: 陈浩翔.
8+
* Date: 2016/11/18.
9+
* Time: 上午 11:04.
10+
* Explain:运行类
11+
*/
12+
public class Main {
13+
public static void main(String[] args) {
14+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskExecutorConfig.class);
15+
16+
AsyncTaskService asyncTaskService = context.getBean(AsyncTaskService.class);
17+
for(int i=0;i<10;i++){
18+
asyncTaskService.executeAsyncTaskPlus(i);
19+
asyncTaskService.executeAsyncTask(i+1);
20+
}
21+
context.close();
22+
23+
}
24+
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cn.hncu.p3.p2_TaskExecutor;
2+
3+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.scheduling.annotation.AsyncConfigurer;
7+
import org.springframework.scheduling.annotation.EnableAsync;
8+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
9+
10+
import java.util.concurrent.Executor;
11+
12+
/**
13+
* Created with IntelliJ IDEA.
14+
* User: 陈浩翔.
15+
* Date: 2016/11/18.
16+
* Time: 上午 9:35.
17+
* Explain:配置类
18+
*/
19+
@Configuration
20+
@ComponentScan("cn.hncu.p3.p2_TaskExecutor")
21+
@EnableAsync //利用@EnableAsync注解开启异步任务支持
22+
public class TaskExecutorConfig implements AsyncConfigurer{
23+
//配置类实现AsyncConfigurer接口并重写getAsyncExcutor方法,并返回一个ThreadPoolTaskExevutor
24+
//这样我们就获得了一个基于线程池TaskExecutor
25+
@Override
26+
public Executor getAsyncExecutor() {
27+
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
28+
taskExecutor.setCorePoolSize(5);//线程池维护线程的最少数量
29+
taskExecutor.setMaxPoolSize(10);//线程池维护线程的最大数量
30+
taskExecutor.setQueueCapacity(25);//线程池所使用的缓冲队列
31+
taskExecutor.initialize();
32+
return taskExecutor;
33+
}
34+
35+
@Override
36+
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
37+
return null;
38+
}
39+
}
Binary file not shown.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
#分析
3+
4+
Spring的依赖注入的最大亮点就是你所有的Bean对Spring容器的存在是没有意识的。
5+
6+
也就是说,你可以把你的容器换成别的容器,如Google Guice,这时Bean之间的耦合度很低。
7+
8+
但是在实际项目中,基本上不可避免的要用到Spring容器本身的功能资源,这时你的Bean必须要意识到Spring容器的存在,才能调用Spring所提供的资源,这就是所谓的Spring Aware。
9+
10+
其实Spring Aware本来就是Spring设计用来框架内部使用的,如果使用了Spring Aware,你的Bean就会和Spring框架耦合。也就不能换容器了。
11+
12+
现在把Spring提供的Aware接口列出来:
13+
14+
| Spring提供的Aware接口 ||
15+
| ------------- |:-------------:|
16+
| BeanNameAware | 获得容器中Bean的名称 |
17+
| BeanFactoryAware | 获得当前bean factory,这样可以调用容器的服务 |
18+
| ApplicationContextAware* | 当前的application context,这样可以调用容器的服务 |
19+
| MessageSourceAware | 获得message source,这样可以获得文本信息 |
20+
| ApplicationEventPublisherAware | 应用实践发布器,可以发布事件 |
21+
| ResourceLoaderAware | 获得资源加载器,可以获得外部资源文件 |
22+
23+
Spring Aware的目的是为了让Bean获得Spring容器的服务。
24+
25+
因为ApplicationContext接口集成了MessageSource接口、ApplicationEventPublisher接口和ResourceLoader接口,所以Bean继承ApplicationContextAware可以获得Spring容器的所有服务,但是,原则上我们还是用到什么接口了,就实现什么接口。
26+
27+
在这里的示例,简单的演示BeanNameAware接口和ResourceLoaderAware接口。
28+
29+
一样的,进行本示例的演示,需要先配置好Maven和Spring哦、
30+
见:
31+
<a href="http://blog.csdn.net/qq_26525215/article/details/53010442" target='_blank'>【Spring】基于IntelliJ IDEA搭建Maven</a>
32+
33+
#示例
34+
35+
因为要演示外部资源,所以先准备好一个外部文件资源,
36+
我就建在java文件目录下,test.txt文件,内容:
37+
```
38+
测试文件的内容
39+
```
40+
41+
##Spring Aware 演示Bean
42+
43+
```
44+
package cn.hncu.p3.p1_SpringAware;
45+
46+
import org.apache.commons.io.IOUtils;
47+
import org.springframework.beans.factory.BeanNameAware;
48+
import org.springframework.context.ResourceLoaderAware;
49+
import org.springframework.core.io.Resource;
50+
import org.springframework.core.io.ResourceLoader;
51+
import org.springframework.stereotype.Service;
52+
53+
import java.io.IOException;
54+
55+
/**
56+
* Created with IntelliJ IDEA.
57+
* User: 陈浩翔.
58+
* Date: 2016/11/16.
59+
* Time: 下午 6:37.
60+
* Explain:Spring Aware演示Bean
61+
*/
62+
@Service
63+
public class AwareService implements BeanNameAware,ResourceLoaderAware {
64+
//实现BeanNameAware,ResourceLoaderAware接口,获得Bean名称和资源加载的服务
65+
66+
private String beanName;
67+
private ResourceLoader loader;
68+
69+
@Override
70+
public void setResourceLoader(ResourceLoader resourceLoader) {//实现ResourceLoaderAware需要重写setResourceLoader方法
71+
this.loader = resourceLoader;
72+
}
73+
74+
@Override
75+
public void setBeanName(String beanName) {//实现BeanNameAware需要重写setBeanName方法
76+
this.beanName = beanName;
77+
}
78+
79+
public void outputResult(){
80+
System.out.println("Bean的名称为:"+beanName);
81+
Resource resource = loader.getResource("cn/hncu/p3/p1_SpringAware/test.txt");
82+
83+
try {
84+
System.out.println("ResourceLoader加载的文件内容为: "+ IOUtils.toString(resource.getInputStream()));
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
}
88+
89+
}
90+
91+
}
92+
93+
```
94+
95+
##配置类
96+
97+
```
98+
package cn.hncu.p3.p1_SpringAware;
99+
100+
import org.springframework.context.annotation.ComponentScan;
101+
import org.springframework.context.annotation.Configuration;
102+
103+
/**
104+
* Created with IntelliJ IDEA.
105+
* User: 陈浩翔.
106+
* Date: 2016/11/16.
107+
* Time: 下午 6:48.
108+
* Explain:配置类
109+
*/
110+
@Configuration
111+
@ComponentScan("cn.hncu.p3.p1_SpringAware")
112+
public class AwareConfig {
113+
}
114+
115+
```
116+
117+
##运行类
118+
119+
```
120+
package cn.hncu.p3.p1_SpringAware;
121+
122+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
123+
124+
/**
125+
* Created with IntelliJ IDEA.
126+
* User: 陈浩翔.
127+
* Date: 2016/11/16.
128+
* Time: 下午 6:49.
129+
* Explain:运行类
130+
*/
131+
public class Main {
132+
public static void main(String[] args) {
133+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
134+
135+
AwareService awareService = context.getBean(AwareService.class);
136+
awareService.outputResult();
137+
138+
context.close();
139+
}
140+
141+
}
142+
143+
```
144+
145+
#运行结果
146+
147+
![](http://img.blog.csdn.net/20161116193220921)
148+
Binary file not shown.
1.18 KB
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)