Reputation: 171
I run a spring
web application (not spring boot
).
On startup the application requests a dataset from another server. This dataset takes about a minute to be processed. When I deploy this application to the tomcat, it takes a minute. The website itself will not be available until the dataset request has been processed completely. But actually I would like to see, that users are already able to login and the dataset is processed without stopping the rest of the application from working.
Currently I have a service class and use the @PostConstruct
-Annotation.
@Service
public class StartupService {
@PostConstruct
public void load() {
//perform the dataset request
...
}
}
I found similar article here on stackoverflow, that suggested trying the ApplicationListener. But this has the same effect. HTTP Requests to the website are not answered unless the dataset request has finished.
@Service
public class StartupService implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {
//perform the dataset request
...
}
}
Of course it would be possible, to start a new Thread
, but I would like to know, what the best approach for this problem would be.
Upvotes: 0
Views: 89
Reputation: 2756
From the PostConstruct doc:
... This method MUST be invoked before the class is put into service...
So Spring cannot service a request until the @PostContruct method finishes.
As you suggested, start a new thread manually, or:
See also: @EnableAsync and/or @EnableScheduling
Upvotes: 1