Java B2B2C多用戶商城 springcloud架構 (二十三)異步方法

建立工程

在pom文件引入相關依賴:html

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

  建立一個接收數據的實體:git

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@JsonIgnoreProperties (ignoreUnknown= true )
public class User {
private String name;
private String blog;
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getBlog() {
return blog;
}
public void setBlog(String blog) {
this .blog = blog;
}
@Override
public String toString() {
return "User [name=" + name + ", blog=" + blog + "]" ;
}
}

  建立一個請求的 githib的service:github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Service
public class GitHubLookupService {
private static final Logger logger = LoggerFactory.getLogger(GitHubLookupService. class );
private final RestTemplate restTemplate;
public GitHubLookupService(RestTemplateBuilder restTemplateBuilder) {
this .restTemplate = restTemplateBuilder.build();
}
@Async
public Future<User> findUser(String user) throws InterruptedException {
logger.info( "Looking up " + user);
String url = String.format( "https://api.github.com/users/%s" , user);
User results = restTemplate.getForObject(url, User. class );
// Artificial delay of 1s for demonstration purposes
Thread.sleep(1000L);
return new AsyncResult<>(results);
}
}

  

經過,RestTemplate去請求,另外加上類@Async 代表是一個異步任務。web

開啓異步任務:spring

複製代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@SpringBootApplication
@EnableAsync
public class Application extends AsyncConfigurerSupport {
public static void main(String[] args) {
SpringApplication.run(Application. class , args);
}
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize( 2 );
executor.setMaxPoolSize( 2 );
executor.setQueueCapacity( 500 );
executor.setThreadNamePrefix( "GithubLookup-" );
executor.initialize();
return executor;
}
}

  

經過@EnableAsync開啓異步任務;而且配置AsyncConfigurerSupport,好比最大的線程池爲2.api

測試

測試代碼以下:bash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Component
public class AppRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(AppRunner. class );
private final GitHubLookupService gitHubLookupService;
public AppRunner(GitHubLookupService gitHubLookupService) {
this .gitHubLookupService = gitHubLookupService;
}
@Override
public void run(String... args) throws Exception {
// Start the clock
long start = System.currentTimeMillis();
// Kick of multiple, asynchronous lookups
Future<User> page1 = gitHubLookupService.findUser( "PivotalSoftware" );
Future<User> page2 = gitHubLookupService.findUser( "CloudFoundry" );
Future<User> page3 = gitHubLookupService.findUser( "Spring-Projects" );
// Wait until they are all done
while (!(page1.isDone() && page2.isDone() && page3.isDone())) {
Thread.sleep( 10 ); //10-millisecond pause between each check
}
// Print results, including elapsed time
logger.info( "Elapsed time: " + (System.currentTimeMillis() - start));
logger.info( "--> " + page1.get());
logger.info( "--> " + page2.get());
logger.info( "--> " + page3.get());
}
}

  啓動程序,控制檯會打印:架構

1
2
3
2017 - 04 - 30 13 : 11 : 10.351 INFO 1511 — [ GithubLookup- 1 ] com.forezp.service.GitHubLookupService : Looking up PivotalSoftware
2017 - 04 - 30 13 : 11 : 10.351 INFO 1511 — [ GithubLookup- 2 ] com.forezp.service.GitHubLookupService : Looking up CloudFoundry
2017 - 04 - 30 13 : 11 : 13.144 INFO 1511 — [ GithubLookup- 2 ] com.forezp.service.GitHubLookupService : Looking up Spring-Projects

  架構代碼以下:異步

"分佈式b2b <wbr

資料和源碼來源地址async

Spring Cloud大型企業分佈式微服務雲架構源碼請加企鵝求求:一七九一七四三三八零

相關文章
相關標籤/搜索