Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ys
- import groovy.util.logging.Slf4j
- import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
- import org.springframework.beans.factory.annotation.Autowired
- import org.springframework.context.annotation.Bean
- import org.springframework.context.annotation.Configuration
- import org.springframework.scheduling.annotation.*
- import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
- import org.springframework.test.context.ContextConfiguration
- import spock.lang.Specification
- import ys.AsyncSpec.SchedulingConfiguration
- import java.util.concurrent.Executor
- import java.util.concurrent.Future
- @Slf4j
- @ContextConfiguration(classes = [SchedulingConfiguration.class])
- class AsyncSpec extends Specification {
- static final int VALUE = 111
- @Autowired
- FooService fooService
- def "test async"() {
- setup:
- Future<Integer> result = fooService.foo()
- expect:
- result.get() == VALUE
- }
- static class FooService {
- @Async
- Future<Integer> foo() {
- log.info('foo invoked')
- return new AsyncResult<Integer>(VALUE)
- }
- }
- @EnableScheduling
- @EnableAsync
- @Configuration
- static class SchedulingConfiguration implements AsyncConfigurer {
- @Bean
- FooService fooService() {
- return new FooService()
- }
- @Override
- Executor getAsyncExecutor() {
- ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler()
- scheduler.setPoolSize(10)
- scheduler.initialize()
- return scheduler
- }
- @Override
- AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
- return { e, method, params -> log.error("Async error in method {}", method, e) }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement