Using jhipster on spring boot 1.5.4, I'm having a hard time getting background tasks to execute asynchronously; they appear to be running synchronously using a different taskExecutor and thread pool than the one I've configured.
All this happens in a service, which for bevity, is defined like so:
@Service
@Transactional
public class AppService {
@Scheduled(fixedDelay = 3000)
public void consumeData() {
// connect to a subscriber and push data to the workerBee
for(Tuple data : this.getTuples()) {
workerBee(data);
}
}
@Timed
@Async
public void workerBee(Tuple data) throws Exception {
// ... do something that takes 300ms ....
Thread.sleep(300);
}
}
Arguably a service isn't the perfect place for this work, but for demonstration purposes, it fits.
(also as an aside, it apears @Timed isn't working, but I read somewhere that @Timed doesn't work when called internally within the service)
Relevant section of application.yml:
jhipster:
async:
core-pool-size: 8
max-pool-size: 64
queue-capacity: 10000
Using the default, generated AsyncConfiguration.java, which looks like this:
@Override
@Bean(name = "taskExecutor")
public Executor getAsyncExecutor() {
log.debug("Creating Async Task Executor");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
executor.setThreadNamePrefix("app-Executor-");
return new ExceptionHandlingAsyncTaskExecutor(executor);
}
I have verified that the taskExecutor bean is getting created and is being used by liquibase.
When I connect visualvm I see all the work happening in pool-2-thread-1, which must be some kind of default and it's obvious that the work is happening synchronously, and not asynchronously.
Things I've tried:
- Specifying the executor in the @Async annotation like
@Async("taskExecutor") - Verifying configuration of the taskExecutor with 8 threads in the core-pool-size.
- Verifying that the application has the @EnableAsync annotation (it does by default).