I am testing using SpringBootTest
. One of the services behind uses an asynchronous service running with an Executor service that has 50 threads.
But I am getting Shutting Down ExecutorService asycnExecutor, InterruptedException
then TaskRejectedException
.... [shutting down pool size =8, active thread = 8] did not accept task.
Here the threadname is pool-3-thread-1
and parent is main
i assume this is from the SpringBootTest
. Also it seems 8 tasks have been created and accepted but shutdown after that
Also no message in InterruptedException
when I caught it specifically. message is null;
I figured the following:
Something is shutting down the executor service when its running
Then it goes through interrupted exception and shutsdown
Rest of the tasks gets TaskRejectedException becuase executorservice shutsdown
Sample Code (tried to simplify as much as possible):
@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);
}
}
Tests;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) // also tried with classes = App.class
@Autowired
TestRestTemplate restTemplate
@Mock
MyObjectmyobject
@Test
public void testResponse() {
}
please help figure why it fails in the integration tests or some directions for debugging further?