Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To start, I wouldn't use JSP for this. There it is not for.</p> <p>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 <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/scheduling.html" rel="nofollow noreferrer">Spring</a>, then you should use it.</p> <p>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 <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html" rel="nofollow noreferrer"><code>ServletContextListener</code></a> with a <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html" rel="nofollow noreferrer"><code>ScheduledExecutorService</code></a>. Further detail can be found in <a href="https://stackoverflow.com/questions/4691132/how-to-keep-the-servlet-continously-running/4691650#4691650">this answer</a>.</p> <p>Or when you're already on a Java EE 6 container which supports EJB 3.1 (JBoss AS 6, GlassFish 3, but thus <em>not</em> Tomcat 7), easiest is to create a <a href="http://download.oracle.com/javaee/6/api/javax/ejb/Singleton.html" rel="nofollow noreferrer"><code>@Singleton</code></a> EJB with <a href="http://download.oracle.com/javaee/6/api/javax/ejb/Schedule.html" rel="nofollow noreferrer"><code>@Schedule</code></a> method.</p> <pre><code>@Singleton public class UpdateSubscriptions { @Schedule(hour="*/6", minute="0", second="0", persistent=false) public void run() { // Do your job here. } } </code></pre> <p>That's it. No further configuration is necessary.</p> <hr> <p><strong>Update</strong>: 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 <a href="https://stackoverflow.com/questions/4691132/how-to-keep-the-servlet-continously-running/4691650#4691650">beforelinked answer</a> and make the following change in the <a href="http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate%28java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit%29" rel="nofollow noreferrer"><code>scheduleAtFixedRate()</code></a> method</p> <pre><code>scheduler.scheduleAtFixedRate(new UpdateSubscriptions(), 0, 6, TimeUnit.HOURS); </code></pre> <p>The class <code>UpdateSubscriptions</code> must implement <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Runnable.html" rel="nofollow noreferrer"><code>Runnable</code></a> and the actual job needs to be done in the <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Runnable.html#run%28%29" rel="nofollow noreferrer"><code>run()</code></a> method which you <a href="http://download.oracle.com/javase/6/docs/api/java/lang/Override.html" rel="nofollow noreferrer"><code>@Override</code></a>, like as in the example in the linked answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload