嗨,这是我的听众课程
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);
}
这是我的timertask课程
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
}
}
该任务未在特定事件中触发,任何人都可以解决其中的问题....
提前致谢
答案 0 :(得分:0)
为什么要创建自己的Timer类,应该重用java.util.timer
中的类只需使用以下代码并删除Timer类。
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);
确保已导入正确的Timer类
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
答案 1 :(得分:0)
您的代码中存在多个问题,例如:
Timer
是java.util
包中定义的类,用于创建计划任务。您可以尝试更改代码:
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
}
}