简体   繁体   English

如何将TimerTask响应发送到Servlet

[英]How to send TimerTask response to Servlet

I have configured Servlet with TimerTask as shown below 我已经用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.......");
        }

    }

Instead of using System.out.println and printing it on to the console Is there anyway can we send this to the servlet ? 而不是使用System.out.println并将其打印到控制台上,是否可以将其发送到servlet?

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)
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM