I have a simple Tomcat 7 Server where I want to implement a Java Web Service which offers some data I can get via my mobile phone.
The point is I want the data on the server being updated every once in a while. So I need a “background process” which updates the data.
I first tried to start a new thread in the constructor of my binding implementation class (which implements only my own Service – not a HttpServlet or so) like
public NewBindingImpl(){
Thread informationFetcher = new InformationFetcher();
informationFetcher.start();
}
But I didn’t think about the fact that this class gets created every time someone is using the service. Further more this would update the data only the moment I ask for them. But how could I update them lets say every two hours or so?
Hopefully someone here has an idea. Is that even possible for a “simple” web service?
Thank you very much,
Tobias
EDIT: —-
Maybe it helps to know that I tried this very basic tutorial here:
http://www.elearning.witnut.com/230/java-web-service-creation-using-top-development-approach/
Why not initialise the thread when the servlet’s
init()method is called ? You can shut it down when the correspondingdestroy()method is called. The thread will be bound to the lifecycle of the servlet and sinceinit()is only called once, you won’t have to worry about multiple instances.Here’s a brief tutorial on the init() method usage.
Since you want something running every two hours, check out the Timer class. For more complex scenarios Quartz is a serious contender.