带有异步服务的SpringBootTest中的InterruptedException和TaskRejectedException

时间:2018-07-17 13:40:34

标签: java spring-boot asynchronous completable-future spring-boot-test

我正在使用SpringBootTest进行测试。后面的服务之一使用异步服务,该异步服务与具有50个线程的Executor服务一起运行。

但是我得到关闭ExecutorService asycnExecutor ,先InterruptedException,然后再TaskRejectedException...。[关闭池大小= 8,活动线程= 8]不接受任务。 在这里,线程名是pool-3-thread-1,父线程是main,我假设它来自SpringBootTest。而且似乎已经创建并接受了8个任务,但此后将其关闭

当我特别发现它时,InterruptedException中也没有消息。消息为空;

我发现以下内容:

  • 某些东西在执行程序服务运行时关闭了

  • 然后它经历了中断的异常并关闭

  • 其余任务由于执行者服务而获得TaskRejectedException 关闭

示例代码(尝试尽可能简化):

@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableAsync
public class App extends springBootServletInitializer
{
    public static voice main(String[] args){
        SpringApplication.run(App.Class, args)
   }

   @Bean(name = "asycnExecutor")
   public Executor asycnExecutor() {
           ThreadPoolTaskExecutor exc = new ThreadPoolTaskExecutor();
           //setcore = 50;
           //setmaxpool = 1000
           //setqueue = 2000
           //setthreadnameprefix = "customAsycn"
           //initialize
           return exc;
   }
}

NormalService.java

@Component
public class NormalService{

    @Autowired
    MyAsycnService myasyncService;

    @Cacheable("myobject")
    public MyObject someservice(){
       do_someService();
    }

    @CachePut("myobject")
     public MyObject do_someService(){
        if(many){
              /// some steps
              foreach()
              {
                CompletableFuture<myobject> blah = myasyncService.doTask();
              }
              //allof / join / .get on the list of CompletableFuture<Void> and accumulate result
         }
         else {
             ///some otherstep
             CompletableFuture<myobject> blah myasyncService.doTask();
            //just 1 result
         }
         return myObject;
     }
}

MyAsycnService.java

@Service
public class MyAsycnService {

    @Autowired
     RestTemplate restTemplate;

     @Asynch("asycnExecutor")
      public CompletableFuture<MyObject> doTask()
      {  
         ResponseEntity<string> r = restTemplate.getEntity();
         MyObject myobject = process(r);
         return CompletableFuture.completedFuture(myobject);

      }

     private MyObject process(ResponseEntity<responseEntity> r)
     {
       MyObject o = new MyObject();
       ///someprcess
      return o;
     }
}

ScheduledControllerCallerConfig

@Component
pulic class ScheduledClass {
  @Autowired
  NormalService ns;
  @Scheduled (fixedDelay = 10000, initialDelay = 500)
  public void do_something()
   {
        ns.someservice();
    } 

}

MyController.Java

@Controller
public class MyController {

 @AutoWired
  NormalService ns;

  @ApiOperation
  @RequestMapping
  public ResponseEntity<MyObject> getSomething()
  {
    MyObject = ns.someservice();
    return ResponseEntity.ok(result);
  } 

}

测试;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // also tried with classes = App.class

@Autowired
TestRestTemplate restTemplate

@Mock
MyObjectmyobject

@Test
public void testResponse() {

}

请帮忙弄清楚为什么它在集成测试中失败或进一步调试的一些指导?

0 个答案:

没有答案