如何调用每天的日程安排

发布于 2024-11-25 23:56:00 字数 4265 浏览 1 评论 0原文

我所做的是,当我第一次运行一个servlet(从jsp调用)时,每天将该服务的一个条目放入conf文件中。我想运行一个调度程序,它将调用程序(servlet-运行并发送邮件)每天提供该服务 10 .

下面是我用来执行任务的代码。但问题是当我停止服务器时,调度程序停止并且没有任何反应

public class Schedule
{
    public static final String CONF_PATH = "../webapps/selen/WEB-INF/scheduling.properties";
    public static Properties schProps = null;
    public static FileInputStream sis = null;
    public static long period;
    public static Timer timer = new Timer();
    public static String servicename = null;
    public static String keyValues = null;
    public static String reValues[] = null;
    public static String schedulingValue = null;
    public static String service_url = null;
        public static String browserlist = null;
        public static String testType = null;
    public static String mailCheacked = null;
        public static String toaddr = null;
    public static HttpServletRequest request = null;
    public static HttpServletResponse response = null;
    public static String serversURL = null;
    public static String contextPath = null;
        public static Date delay = null;
    public void scheduleLoad(String serviceValue) throws Exception
    {
        try
            {
            schProps = new Properties();
            sis = new FileInputStream(CONF_PATH);
            schProps.load(sis);
            servicename = SServlet.serviceName; 
            keyValues = schProps.getProperty(serviceValue);
            reValues = keyValues.split(",");
            String request = reValues[0];
            String response = reValues[1];
            schedulingValue = reValues[2];
            service_url = reValues[3];
            browserlist = reValues[4];
            testType = reValues[5];
            mailCheacked = reValues[6];
            toaddr = reValues[7];
            serversURL = reValues[8];
            contextPath = reValues[9];
            if(reValues[2].equals("Daily"))
            {

                Calendar cal =Calendar.getInstance();
                cal.set(Calendar.HOUR,10);
                cal.set(Calendar.MINUTE,20);
                cal.set(Calendar.SECOND,0);
                delay = cal.getTime();
                period = 1000 * 60 * 60 * 24;
                schedule();
            }
            else if(reValues[2].equals("Stop"))
            {
                stop();
            }   
        }
        catch(NullPointerException npe)
        {
            System.out.println("null point exception ");
        }
        finally
        {
            if(sis !=null)
            {
                sis.close();
            }
        }           

    }
    public static void schedule()
    {
        MyTimerTask mt = new MyTimerTask(request,response,servicename,service_url,browserlist,mailCheacked,testType,schedulingValue,toaddr,serversURL,contextPath);
        timer.schedule(mt,delay,period);
    }
    public static void stop()
    {
        timer.cancel();
    }

} 
class MyTimerTask extends TimerTask
{
    public HttpServletRequest request;
    public HttpServletResponse response;    
    public String servicename;
    public String service_url;
    public String browserlist;
    public String mailCheacked;
    public String testType;
    public String schedulingValue;
    public String toaddr;
    public String serversURL;
    public String contextPath;
    public MyTimerTask(HttpServletRequest request,HttpServletResponse response, String servicename,String service_url,String browserlist,String mailCheacked,String testType,String schedulingValue,String toaddr,String serversURL, String contextPath)
    {
        this.request = request;
        this.response = response;
        this.servicename = servicename;
        this.service_url = service_url;
        this.browserlist = browserlist;
        this.mailCheacked = mailCheacked;
        this.testType = testType;
        this.schedulingValue = schedulingValue;
        this.toaddr = toaddr;
        this.serversURL = serversURL;
        this.contextPath = contextPath;
    }
    public void run()
    {
        SServlet sservlet = new SServlet();
        sservlet.sServerloading(request,response,servicename,service_url,browserlist,mailCheacked,testType,schedulingValue,toaddr,false,1,serversURL,contextPath);
    }
}

what i do is ,when i run first time a servlet (which is invoked from jsp) that while put an entry of that service,daily in conf file.i want to run a scheduler which will invoke program(servlet- which runs and send mail) for that service daily 10 .

below is the code i use to execute a task.but problem is when i stop the server ,the scheduler stops and nothing happens

public class Schedule
{
    public static final String CONF_PATH = "../webapps/selen/WEB-INF/scheduling.properties";
    public static Properties schProps = null;
    public static FileInputStream sis = null;
    public static long period;
    public static Timer timer = new Timer();
    public static String servicename = null;
    public static String keyValues = null;
    public static String reValues[] = null;
    public static String schedulingValue = null;
    public static String service_url = null;
        public static String browserlist = null;
        public static String testType = null;
    public static String mailCheacked = null;
        public static String toaddr = null;
    public static HttpServletRequest request = null;
    public static HttpServletResponse response = null;
    public static String serversURL = null;
    public static String contextPath = null;
        public static Date delay = null;
    public void scheduleLoad(String serviceValue) throws Exception
    {
        try
            {
            schProps = new Properties();
            sis = new FileInputStream(CONF_PATH);
            schProps.load(sis);
            servicename = SServlet.serviceName; 
            keyValues = schProps.getProperty(serviceValue);
            reValues = keyValues.split(",");
            String request = reValues[0];
            String response = reValues[1];
            schedulingValue = reValues[2];
            service_url = reValues[3];
            browserlist = reValues[4];
            testType = reValues[5];
            mailCheacked = reValues[6];
            toaddr = reValues[7];
            serversURL = reValues[8];
            contextPath = reValues[9];
            if(reValues[2].equals("Daily"))
            {

                Calendar cal =Calendar.getInstance();
                cal.set(Calendar.HOUR,10);
                cal.set(Calendar.MINUTE,20);
                cal.set(Calendar.SECOND,0);
                delay = cal.getTime();
                period = 1000 * 60 * 60 * 24;
                schedule();
            }
            else if(reValues[2].equals("Stop"))
            {
                stop();
            }   
        }
        catch(NullPointerException npe)
        {
            System.out.println("null point exception ");
        }
        finally
        {
            if(sis !=null)
            {
                sis.close();
            }
        }           

    }
    public static void schedule()
    {
        MyTimerTask mt = new MyTimerTask(request,response,servicename,service_url,browserlist,mailCheacked,testType,schedulingValue,toaddr,serversURL,contextPath);
        timer.schedule(mt,delay,period);
    }
    public static void stop()
    {
        timer.cancel();
    }

} 
class MyTimerTask extends TimerTask
{
    public HttpServletRequest request;
    public HttpServletResponse response;    
    public String servicename;
    public String service_url;
    public String browserlist;
    public String mailCheacked;
    public String testType;
    public String schedulingValue;
    public String toaddr;
    public String serversURL;
    public String contextPath;
    public MyTimerTask(HttpServletRequest request,HttpServletResponse response, String servicename,String service_url,String browserlist,String mailCheacked,String testType,String schedulingValue,String toaddr,String serversURL, String contextPath)
    {
        this.request = request;
        this.response = response;
        this.servicename = servicename;
        this.service_url = service_url;
        this.browserlist = browserlist;
        this.mailCheacked = mailCheacked;
        this.testType = testType;
        this.schedulingValue = schedulingValue;
        this.toaddr = toaddr;
        this.serversURL = serversURL;
        this.contextPath = contextPath;
    }
    public void run()
    {
        SServlet sservlet = new SServlet();
        sservlet.sServerloading(request,response,servicename,service_url,browserlist,mailCheacked,testType,schedulingValue,toaddr,false,1,serversURL,contextPath);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

π浅易 2024-12-02 23:56:00

JDK Timer 在 JVM 中运行,而不是在操作系统中运行。它不是 CRON 或 Windows 调度程序。因此,当您停止服务器(Tomcat?JBoss?Glassfish?)时,您实际上是停止了计时器所在的JVM,因此它当然不会再运行。如果您想要一个独立于服务器运行的计时器(调度程序),则必须在它自己的 JVM 中启动它,无论是使用 java 命令作为独立的 java 程序,还是在另一个服务器实例中启动它。

顺便说一句,如果您愿意接受一些批评,请对您的代码进行一次小审查:

  • 如果可能,请避免混合静态和非静态上下文。您的 Schedule 类实例方法 scheduleLoad() 大量使用静态成员变量来进行有状态存储。变量要么仅在方法的执行中使用(在这种情况下,它们应该在该方法内声明),要么用于描述对象的状态(在这种情况下,它们应该是类的私有实例成员),或者它们是全局常量或不可变全局变量(在这种情况下,它们应该声明为 static Final)。存在例外情况,但不太常见。

  • 如果成员变量不是final,请避免声明它们public。遵循 JavaBean 模式,使用 getter 和 setter。如果一个变量实际上是一个常量,那么它应该是 public static final

  • 避免使用超出范围的类或参数。例如,您的 MyTimerTask 使用 HttpServletRequestHttpServletResponse 作为成员变量和方法参数。这是没有意义的,因为 MyTimerTask 没有在 servlet 请求的范围内使用(并且随后将始终为 null,对吗?)。或者,如果情况确实如此,如果您在某些 servlet 中显式设置 Schedule 的静态成员,然后调用 scheduleLoad(),请参阅我关于不正确的第一点使用静态上下文。您的代码不是线程安全的,并且并发调用使用 Schedule 的任何 servlet 都会产生不可预测的行为。

更新:
很难知道从哪里开始,因为我不确定您在 Java 方面的专业水平如何。如果您不熟悉如何执行独立的 java 应用程序,我建议您尝试一些教程。 Oracle 在 http://download.oracle.com/javase/tutorial/ 上有很多。 http://download.oracle.com/javase/tutorial/getStarted/index.html 是一个很好的起点,因为它引导您完成一个带有 main 方法的非常基本的“hello world”类型应用程序,以及如何使用 java 执行它命令,以及一些常见的错误和问题。

一旦您弄清楚了所有这些,请花几分钟来弄清楚您的应用程序应该做什么、需要哪些资源以及是否需要调用任何“外部”系统。您提到它应该“执行 servlet 来发送邮件”。这是否意味着它必须调用特定的 servlet,或者只是发送邮件才是您真正想要的。在这种情况下,也许您可​​以将所有邮件发送逻辑移至独立程序中?如果没有,您将必须使用 http 请求来调用 servlet(就像浏览器那样)。有许多现有的框架可以完成类似的事情。 Apache HttpClient 是一种非常流行的客户端。

The JDK Timer runs in the JVM, not in the operating system. It's not CRON or Windows scheduler. So when you stop your server (Tomcat? JBoss? Glassfish?), you are effectivly stopping the JVM that the Timer lives in so of course it won't run any more. If you want a timer (scheduler) that runs independently of your server, you will have to start it in it's own JVM, either as a standalone java program using the java command or inside another server instance.

On a side note, if you're open to some critique, a small review of your code:

  • Avoid mixing static and non-static contexts if possible. Your Schedule class instance method scheduleLoad() makes heavy use of static member variables for statefull storage. Variables are either only used in the execution of a method (in which case they should be declared inside that method) or they are used to describe the state of an object (in which case they should be private instance members of the class) or they are global constants or immutable global variables (in which case they should be declared static final). Exceptions to these exist, but are less common.

  • Avoid declaring member variables public if they are not also final. Adhere to the JavaBean pattern, use getters and setters. If a variable is, in reality, a constant then it should be public static final.

  • Avoid using classes or parameters out of scope. For instance, your MyTimerTask uses HttpServletRequest and HttpServletResponse as member variables and method parameters. This makes no sense as MyTimerTask is not used in the scope of a servlet request (and will subsequently always be null, right?). Or, if that is indeed the case, if you are explicitly setting the static members of the Schedule in some servlet and then invoking scheduleLoad(), see my first point about improper use of static context. Your code would not be thread-safe and concurrent invocation of whichever servlet that uses the Schedule would produce unpredictable behaviour.

UPDATE:
It's hard to know where to start as I'm not sure what your level of expertise is in Java. If you are unfamiliar with how to execute stand-alone java applications, I would suggest having a go at some tutorials. Oracle has a bunch at http://download.oracle.com/javase/tutorial/. http://download.oracle.com/javase/tutorial/getStarted/index.html is a good place to start as it walks you through a very basic "hello world" type application with a main method and how to execute it using the java command, as well as some common mistakes and problems.

Once you've figured all that out, take a few minutes to figure out what your application should do, which resources it will require and if it needs to call any "external" systems. You mentioned that it should "execute a servlet to send mail". Does that mean that it has to call a specific servlet or is it just the sending mail that is really what you are after. In that case, maybe you can just move all the mail sending logic to your standalone program? If not, you will have to call the servlet using a http request (like a browser would). There are a number of existing frameworks for doing things like that. Apache HttpClient is a very popular one.

浅浅淡淡 2024-12-02 23:56:00

如果停止程序,它就不起作用。这不是一个错误。这是一个特点。顺便说一句,如果你关闭计算机,也不会发生任何事情:)。

但是如果您询问的是如何使我的计划任务更加健壮,例如如何使任务在服务器停止然后再次启动时继续工作,您必须在某个地方保留调度程序的状态,即在您的情况下是任务执行的最后一次。您可以自己实现:创建特殊文件并将数据存储在那里。您可以使用跨平台纯java Preferences API来做到这一点:数据将存储在Unix的文件系统和Windows的注册表中。您也可以将状态保存在数据库中。

但您可以使用已经实现此功能的其他产品。最流行和最知名的是石英。

但 Quartz 仍然需要一些 java 进程才能启动和运行。如果您希望即使没有运行 java 进程也能够运行任务,请使用与平台相关的工具:用于 Unix 的 cron tab 和用于 Windows 的调度程序 API(可通过 VBScript、JScript、命令行访问) 。
Unix 有 cron

If you stop program it does not work. It is not a bug. It is a feature. BTW if you shutdown your computer nothing happens too :).

But If you questing is how to make my scheduled task more robust, e.g. how to make task to continue working when server stops and then starts again you have to persist somewhere the state of you scheduler, i.e. in your case the last time of task execution. You can implement this yourself: create special file and store the data there. You can use cross platform pure java Preferences API to do this: the data will be stored in file system in Unix and Registry in windows. you can save state in DB too.

But you can use other products that have already implemented this functionality. The most popular and well-known is Quartz.

But Quartz still need some java process to be up and running. If you want to be able to run your tasks even if no java process is running use platform dependent tools: cron tab for Unix and scheduler API for windows (it is accessible via VBScript, JScript, command line).
Unix has cron

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文