I have following scenario.
- there is some kind of daily calculation, which can be done retroactively for months.
- it's very time consuming, involves lots of calculations
- no day is dependent on the result of any other day, every day is standalone.
- calculations will be triggered from web API
- all the results will be saved for later use
- all methods are void
So, I was thinking about for loop that would calculate multiple days at time, since there is no need to know about results of other days, but i cant figure how to do this properly.
where do I put the @Async annotation?
method for JAX is as follows:
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/calculated")
public void calculated(String req,
final @Context SecurityContext securityContext,
final @Suspended AsyncResponse response2) {
logger.info("calculating");
ThymeDate thymeDate = parseJSONDate(req);
thymeNewDateRange.getDataForDateRange(thymeDate, null);
}
this will send object that has date a and date b into getDataForDateRange method, which will then loop days between a and b.
public void iterateOverDays(ThymeDate thymeDate) {
for (LocalDate date : new DateRange(thymeDate.getDate_a(), thymeDate.getDate_b())) {
ControlService.dailyCalculation(date, null);
}
and as stated before, dailyCalculation method is huge so i will not paste the code for it, but it is a void method with many calculations in it.
I've tried to put @Async into dailyCalculation method, but application started to skip days and do multiple calculation on some days, seemingly randomly.
I also tried to put it in JAX and for loop method, which lead to async not being used at all.
i have following Executor in my AppConfig:
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(4);
executor.setMaxPoolSize(4);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("calc-thread-");
executor.initialize();
return executor;
}