我已经使用TimerTask配置了Servlet,如下所示
package com.scheduler;
public class SchedulerServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
long interval = Long.parseLong(config.getInitParameter("interval")) * 60 * 1000;
MyAction action = new MyAction();
Timer timer = new Timer();
timer.schedule(action, new Date(), interval);
}
}
package com.scheduler;
import java.util.TimerTask;
public class MyAction extends TimerTask {
public void run() {
System.out.println("Run method called.......");
}
}
而不是使用System.out.println并将其打印到控制台 无论如何我们可以将它发送到servlet吗?
答案 0 :(得分:1)
你是追求这样的吗?
public class SchedulerServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
long interval = Long.parseLong(config.getInitParameter("interval")) * 60 * 1000;
MyAction action = new MyAction(this);
Timer timer = new Timer();
timer.schedule(action, new Date(), interval);
}
public void updateSomething(Object obj) {
//This gets called from your timertask
}
}
import java.util.TimerTask;
public class MyAction extends TimerTask {
SchedulerServlet servlet;
public MyAction(SchedulerServlet servlet) {
this.servlet = servlet;
}
public void run() {
//Do something here...
//Send something back to your servlet class
this.servlet.updateSomething(obj)
}
}