Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. package ys
  2.  
  3. import groovy.util.logging.Slf4j
  4. import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
  5. import org.springframework.beans.factory.annotation.Autowired
  6. import org.springframework.context.annotation.Bean
  7. import org.springframework.context.annotation.Configuration
  8. import org.springframework.scheduling.annotation.*
  9. import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
  10. import org.springframework.test.context.ContextConfiguration
  11. import spock.lang.Specification
  12. import ys.AsyncSpec.SchedulingConfiguration
  13.  
  14. import java.util.concurrent.Executor
  15. import java.util.concurrent.Future
  16.  
  17. @Slf4j
  18. @ContextConfiguration(classes = [SchedulingConfiguration.class])
  19. class AsyncSpec extends Specification {
  20.  
  21. static final int VALUE = 111
  22.  
  23. @Autowired
  24. FooService fooService
  25.  
  26. def "test async"() {
  27. setup:
  28. Future<Integer> result = fooService.foo()
  29. expect:
  30. result.get() == VALUE
  31. }
  32.  
  33. static class FooService {
  34. @Async
  35. Future<Integer> foo() {
  36. log.info('foo invoked')
  37. return new AsyncResult<Integer>(VALUE)
  38. }
  39. }
  40.  
  41. @EnableScheduling
  42. @EnableAsync
  43. @Configuration
  44. static class SchedulingConfiguration implements AsyncConfigurer {
  45.  
  46. @Bean
  47. FooService fooService() {
  48. return new FooService()
  49. }
  50.  
  51. @Override
  52. Executor getAsyncExecutor() {
  53. ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler()
  54. scheduler.setPoolSize(10)
  55. scheduler.initialize()
  56. return scheduler
  57. }
  58.  
  59. @Override
  60. AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
  61. return { e, method, params -> log.error("Async error in method {}", method, e) }
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement