I want to retrieve from subscription and store feeds to DB from subscription after every 6 hours. I want to have a timer thread in background to accomplish this task.
What’s the best way? A normal timer thread or Quartz API?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To start, I wouldn’t use JSP for this. There it is not for.
When you’re on Java EE 5, use the container-provided jobscheduling APIs for this. Further detail depends on the container you’re using. JBoss AS 5 for example ships with Quartz out the box. Or when you’re using a framework on top of JSP/Servlet which offers jobscheduling APIs, like as Spring, then you should use it.
If there are none (e.g. you’re using just Tomcat 6), or you want to be independent from the container and/or framework, create a
ServletContextListenerwith aScheduledExecutorService. Further detail can be found in this answer.Or when you’re already on a Java EE 6 container which supports EJB 3.1 (JBoss AS 6, GlassFish 3, but thus not Tomcat 7), easiest is to create a
@SingletonEJB with@Schedulemethod.That’s it. No further configuration is necessary.
Update: as per the comments, you’re using Tomcat (6 or 7?). To start a thread during webapp’s startup which runs the task every 6 hours, use the example as provided in the beforelinked answer and make the following change in the
scheduleAtFixedRate()methodThe class
UpdateSubscriptionsmust implementRunnableand the actual job needs to be done in therun()method which you@Override, like as in the example in the linked answer.