0

I want to return content to users on a rest call, and I want to send a rest call to a tracking server about that content request. If the tracking server gets overwhelmed by too many responses, I don't care if it fails. I want it to timeout quickly and not hold up the main content request. To this end I have created the following:

@Configuration
@EnableAsync
public class AsyncConfiguration {

    @Bean (name = "taskExecutor")
    public Executor taskExecutor() {
        log.info("Creating Async Task Executor");
        final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(2);//higher in prod
        executor.setQueueCapacity(2);//higher in prod
        executor.initialize();
        executor.setThreadNamePrefix("AyncThread-");
        return executor;
    }
}

...

@Async
public void trackSearch(TrackingRequest request) {
    try {
        restTemplate.postForLocation(activityUri, request);
    }
    catch (Exception e) {
        log.info("Exception in tracking activity: " + e.getMessage());
    }
}

So, after getting the main content, I call trackSearch above. I don't care if it fails like I said. But when I'm testing this code in JMeter, I cannot seem to 'beat' the queue size. What I was expecting is that if there are too many users hitting this same end point, that the queue just fills up and it stops trying to call the trackSearch and therefore the restTemplate call. Right now, none of the calls fail.

I'm obviously misunderstanding something here, so if anyone can shed some light on what I'm doing wrong, I'd appreciate it.

wonza
  • 302
  • 1
  • 4
  • 15
  • How many users were you using concurrently to try and 'beat the queue size'? And how long does a single `postForLocation` take here? I [tested](https://ideone.com/RpwVng) this by submitting tasks which took 2 seconds and it did start rejecting tasks. – BeUndead Feb 28 '20 at 23:59
  • 1
    Do you call trackSearch within the same bean? try move tracking to another bean and then call it. Check out: https://stackoverflow.com/questions/40042505/async-in-spring-doesnt-work-in-service-class – Dawid D Feb 29 '20 at 09:34
  • It was called within the same bean, I just tried moving it a separate class and that solved the issue, thanks! Post that as the answer and I'll mark it, or set this as a duplicate. – wonza Feb 29 '20 at 18:37

0 Answers0