1

We have a page that calls a servlet every minute. This servlet connects to another website to get some data, process this data, then sends the response through the response.getWriter() method.

If there are two different users accessing the page at the same time, two requests would be made to the same servlet. Though there is still only one instance of the servlet, there would be two processes (perhaps on two different threads) that would try to connect to the other website.

What we want is to have another class/method/servlet that would connect to the other website, process the data, then save it to cache/session. This class/method/servlet has to be invoke/called every minute so that when the first servlet is called from the page, it would just get the data from the cache/session and not try to connect to the website. How do we implement this? (problem is how the new class/method/servlet would be invoke/called automatically every minute without having to make a request from a page)

jmj
  • 237,923
  • 42
  • 401
  • 438
CodeAssasin
  • 97
  • 10

2 Answers2

2

Create a scheduled task that reads external web page(Use Jsoup for easier access) and reads the data into a synchronized Map and from Servlet you can just read that Map from application scope

getServletContext().getAttribute("dataMap");

and read the data from it

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thanks. Now I know what i'm looking for, "scheduled task". But how do I implement it? Looking into [link](http://stackoverflow.com/questions/2248971/running-periodic-task-at-server-side-for-servlet-jsp-mvc-website) – CodeAssasin Jul 17 '12 at 05:15
  • Yes that link describes it well – jmj Jul 17 '12 at 05:15
1

You will have to create a schedule task to accomplish this and interval for this schedule task would be 1 minute.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37