Question

Hi this is my listener class

 public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("inside listener context");
Timer timer = new Timer();
Calendar cal = Calendar.getInstance();
  cal.set(Calendar.HOUR_OF_DAY, 19);
  cal.set(Calendar.MINUTE, 00); 
  cal.set(Calendar.SECOND, 00); 
  Time sqlTime4 = new Time(cal.getTime().getTime());
timer.schedule(new MyTimerTask(),sqlTime4);

}

This is my timertask class

package com.uttara.reg;
import java.util.Date;
import java.util.TimerTask;
public class Timer extends TimerTask {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("inside run of timer");
    }
    public void scheduleAtFixedRate(TimerTask timerTask, Date executionDate, long period)
    {
        System.out.println("run");
        // TODO Auto-generated method stub

    }

}

that task is not triggering in the specific event,could anybody plz rectify the problem in it....

Thanks in advance

Était-ce utile?

La solution

Multiple problems in your code, here are they:

  1. First of all you have picked a confusing name for your TimerTask child class. Timer is the class defined in java.util package, which is used to create the scheduled task.
  2. You are creating an anonymous class of type TimerTask that will be used to run the scheduled task.

You can try to change your code like this:

  public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("inside listener context");
        Timer timer = new Timer();
        Date executionDate = new Date();
        long period = 24 * 60 * 60 * 1000;
        timer.scheduleAtFixedRate(new MyTimerTask(), executionDate, period);    
  }

MyTimerTask

package com.uttara.reg;
import java.util.Date;
import java.util.TimerTask;
public class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("inside run of timer");
    }
    public void scheduleAtFixedRate(TimerTask timerTask, Date executionDate, long period)
    {
        System.out.println("run");
        // TODO Auto-generated method stub

    }

}

Autres conseils

Why are you creating your own Timer class you should reuse the one in java.util.timer

Just use the below code and remove the Timer class.

public static void main(final String[] args) {

    System.out.println("inside listener context");
    final Timer timer = new Timer();
    final Date executionDate = new Date();
    final long period = 24 * 60 * 60 * 1000;
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            System.out.println("inside lisner run con");
        }
    }, executionDate,

    period);

Make sure you have imported correct Timer classes

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top