selson
selson

Reputation: 99

Scheduler task in java

This is my scheduled timer program . it is deployed in tomcat. I want this to be run everyday at 11:46 AM.

so, if deploy that it tomcat, automcatically will it kick off at 11:46AM?

Please advise.

public class TimeScheduler {
    Timer timer;
    Date time = new Date();


    public TimeScheduler() {
        timer = new Timer();
        taskExecutionTime();
        timer.schedule(new RemindTask(), time);
    }

    public Date taskExecutionTime()
    {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 9);
        cal.set(Calendar.MINUTE, 46);
        cal.set(Calendar.SECOND, 0);

        time = cal.getTime();
        return time;
    }


    class RemindTask extends TimerTask {
        public void run() {
            System.out.format("Task is Running now!");
            try {
                CSVReader.parseCSV();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            timer.cancel(); //Terminate the timer thread
            System.out.format("Task is Complete!");
       }
   }

   public static void main(String args[]) {
       new TimeScheduler();
   }
}

Upvotes: 0

Views: 1816

Answers (1)

Eduardo Briguenti Vieira
Eduardo Briguenti Vieira

Reputation: 4579

I'm not sure it will run in tomcat. How would you call the static method to schedule? There's nothing calling it.

Here's an example (http://www.roseindia.net/servlets/ServletContextListenerTimer.shtml)

You need to declare the listener at your web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <listener>
    <listener-class>
        example.ServletContextExample
    </listener-class>
  </listener>
</web-app>

ServletContextListener with Timer

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* Application Lifecycle Listener implementation class MyServletContextListener
*
*/
public class MyServletContextListener implements ServletContextListener {

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
ServletContext servletContext = arg0.getServletContext();
System.out.println("*********ServletContextListener started*********");

int delay = 1000;
Timer timer = new Timer();
//final Calendar calendar = Calendar.getInstance();
//System.out.println("Tweet at Time = " + calendar.getTime());
//calendar.add(Calendar.SECOND, -60);
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
System.out.println("Running this code every 1 minute....");
}//End of Run
},delay, 60000);
servletContext.setAttribute ("timer", timer);
}

/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext servletContext = arg0.getServletContext();
// get our timer from the Context
Timer timer = (Timer)servletContext.getAttribute ("timer");

// cancel all pending tasks in the timers queue
if (timer != null)
timer.cancel();

// remove the timer from the servlet context
servletContext.removeAttribute ("timer");
System.out.println("ServletContextListener destroyed");

}
}

Upvotes: 1

Related Questions