Timer and TimerTask not update

I have an app that could set the time for processing. The problems is when I update the time, the processing will increase. For example: Initially the timer start at 07:00AM Let say, I update the timer to 08:00AM then the next day onwards, the program will run again at 07:00AM and also at 08:00AM. (The 07:00AM is still in scheduler, how to remove the 07:00AM?) How to make the scheduler to only run the 08:00AM the next day?

public void setKonfigurasi(String name, String value) {
    log.info(SERVLET_NAME + "Entering setKonfigurasi");
    amBean.setParam(name,value); //update the time into database
    //name = 'processFileConf|kodPT|userA|20140312 08:30 AM'
    // reschedule timer after configured by user
    try {
        String kodPT = name.substring(name.indexOf("|") + 1, name.indexOf("|",name.indexOf("|") + 1)); 
        String configStr = value.substring(2); //get the new time
        String currentStr = CommonUtil.getCurrentDate();
        DateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy KK:mm:ss a");
        Date currentDate=new Date() ;
        Date configDate = dateformat.parse(currentStr+" "+configStr);
        long config = configDate.getTime();
        long current = currentDate.getTime();

        // today
        long delay = config-current;
        if (delay < 0)
            // tomorrow
            delay += (1000*60*60*24);

        // create the timer and timer task objects
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() { 
            public void run() { 
                System.out.println("showtime for "+kodPT);
                processFile("auto"+kodPT);
            } 
        }, delay, 1000*60*60*24);

        ServletContextEvent servletContextEvent = EtServletContextListener.getContext();
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.removeAttribute ("timer");
        servletContext.setAttribute ("timer", timer);

    } catch (Exception e) {
        log.error("Exception on date format : "+e.getMessage());
    }            
    log.info(SERVLET_NAME + "Exiting setKonfigurasi");
}

Answers


You need to call cancel() on the previous timer and create a new one. The javadoc says:

Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.


I have found out about how to do what I want. Instead of using java.util.Timer to create the timer, we should use javax.ejb.Timer since the Timer in ejb have an info to identified each timer. Timer Service EJB


Need Your Help

Is it possible to have an effect of flowing flame around a div border

css css3

I just happen to recall a graphic effect which I saw many years ago in a Windows Mobile Phone.

Change hash and animate scroll to element

javascript jquery animation hash

I have a link and an element on the same page: