Skip to content

Commit d0bfad0

Browse files
committed
[FIX] #26 SpringBootAsync
1 parent b00578d commit d0bfad0

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.kotlin.config
2+
3+
import org.springframework.context.annotation.Configuration
4+
import org.springframework.scheduling.annotation.AsyncConfigurerSupport
5+
import org.springframework.scheduling.annotation.EnableAsync
6+
7+
import java.util.concurrent.Executor
8+
import java.util.concurrent.Executors
9+
10+
@Configuration
11+
@EnableAsync
12+
class AsyncConfig : AsyncConfigurerSupport() {
13+
14+
override fun getAsyncExecutor(): Executor {
15+
// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
16+
// executor.setCorePoolSize(2);
17+
// executor.setMaxPoolSize(10);
18+
// executor.setQueueCapacity(500);
19+
// executor.setThreadNamePrefix("heowc-async-");
20+
// executor.initialize();
21+
return Executors.newWorkStealingPool()
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.kotlin.service
2+
3+
import org.slf4j.LoggerFactory
4+
import org.springframework.scheduling.annotation.Async
5+
import org.springframework.stereotype.Service
6+
7+
@Service
8+
class BasicService {
9+
10+
@Async
11+
fun onAsync() {
12+
try {
13+
Thread.sleep(1000)
14+
} catch (e: InterruptedException) {
15+
e.printStackTrace()
16+
}
17+
18+
logger.info("onAsync")
19+
}
20+
21+
fun onSync() {
22+
try {
23+
Thread.sleep(1000)
24+
} catch (e: InterruptedException) {
25+
e.printStackTrace()
26+
}
27+
28+
logger.info("onSync")
29+
}
30+
31+
companion object {
32+
33+
private val logger = LoggerFactory.getLogger(BasicService::class.java)
34+
}
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.kotlin.web
2+
3+
import com.example.kotlin.service.BasicService
4+
import org.slf4j.LoggerFactory
5+
import org.springframework.beans.factory.annotation.Autowired
6+
import org.springframework.http.HttpHeaders
7+
import org.springframework.http.MediaType
8+
import org.springframework.web.bind.annotation.GetMapping
9+
import org.springframework.web.bind.annotation.PathVariable
10+
import org.springframework.web.bind.annotation.RestController
11+
import org.springframework.web.reactive.function.client.WebClient
12+
import reactor.core.publisher.Mono
13+
14+
@RestController
15+
class BasicController {
16+
17+
@Autowired
18+
private lateinit var service: BasicService
19+
20+
private val logger = LoggerFactory.getLogger(BasicController::class.java)
21+
22+
private val webClient = WebClient.builder()
23+
.baseUrl("https://api.github.com")
24+
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE)
25+
.build()
26+
27+
@GetMapping("/async")
28+
fun goAsync(): Mono<String> {
29+
service.onAsync()
30+
val str = "Hello Spring Boot Async!!"
31+
logger.info(str)
32+
logger.info("==================================")
33+
return Mono.just(str)
34+
}
35+
36+
@GetMapping("/sync")
37+
fun goSync(): Mono<String> {
38+
service.onSync()
39+
val str = "Hello Spring Boot Sync!!"
40+
logger.info(str)
41+
logger.info("==================================")
42+
return Mono.just(str)
43+
}
44+
45+
@GetMapping("/{userName}")
46+
fun showGithubReposByUserName(@PathVariable userName: String): Mono<List<*>> {
47+
return webClient.get()
48+
.uri(String.format("/users/%s/repos", userName))
49+
.retrieve()
50+
.bodyToMono(List::class.java)
51+
}
52+
}

0 commit comments

Comments
 (0)