Reputation: 32331
I have configured Servlet with TimerTask as shown below
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.......");
}
}
Instead of using System.out.println and printing it on to the console Is there anyway can we send this to the servlet ?
Upvotes: 0
Views: 845
Reputation: 768
Is it something like this that you are after?
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)
}
}
Upvotes: 1