Skip to content

Instantly share code, notes, and snippets.

@yarosla
Created January 16, 2017 14:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yarosla/e1cfe46087a93c2fa3b0d8ec49595dd3 to your computer and use it in GitHub Desktop.
Save yarosla/e1cfe46087a93c2fa3b0d8ec49595dd3 to your computer and use it in GitHub Desktop.
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) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment