简体   繁体   English

Openshift Cron执行Servlet-预定作业

[英]Openshift cron execute servlet - Scheduled Job

I need to execute a task every 5 minutes on my server to update some datas on a db, i've found that on openshift i have the cron that executes some script every tot time. 我需要每隔5分钟在服务器上执行一次任务,以更新db上的一些数据,我发现在openshift上,我的cron每次都可以执行一些脚本。 Is it possibile to make a script that makes a simple call to a servlet or to a java code to run this job? 是否可以制作一个脚本来简单地调用servlet或Java代码来运行此作业? I am quite new to server side programming so please speak easy! 我对服务器端编程还很陌生,所以请讲简单一点!

Ps. PS。 I am using a Tomcat 6 (Jboss EWS 1.0), mySQL 5.5 server 我正在使用Tomcat 6(Jboss EWS 1.0),mySQL 5.5服务器

AS I understand you, you need your application to run sth every XX minutes. 据我了解,您需要您的应用程序每隔XX分钟运行一次。 To calculate the start time I made a helper function "getStartTime" With that I can use the human readable time like "23:30" (attention, I am from german, so it is not for AM/PM, just change for your needs). 为了计算开始时间,我创建了一个辅助函数“ getStartTime”,这样我就可以使用人类可读的时间,例如“ 23:30”(注意,我来自德语,因此不是AM / PM,只需更改您的需求即可) )。

Helper Method: 辅助方法:

private static long getStartTime(String startTime) {
    int hour = Integer.parseInt(startTime.split(":")[0]);
    int minutes = Integer.parseInt(startTime.split(":")[1]); 
    Calendar cal = Calendar.getInstance();
    Date dateNow = cal.getTime();
    cal.set(Calendar.HOUR_OF_DAY, hour);
    cal.set(Calendar.MINUTE, minutes);
    cal.set(Calendar.SECOND, 0);

    if(cal.getTime().before(dateNow)) {
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 1);
        return cal.getTime().getTime();
    } else {
        return cal.getTime().getTime();
    }
}

Now you can use the ScheduledExecutorService from Java. 现在,您可以使用Java的ScheduledExecutorService。 Example: 例:

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);

    long startClearRequests = getStartTime(DEFAULT_JOB_START_TIME) - System.currentTimeMillis();

And set your needs into the scheduleAtFiexed Rate: 并将您的需求设置为scheduleAtFiexed Rate:

    scheduledExecutorService.scheduleAtFixedRate(clearRequests, startClearRequests, Math.round(DEFAULT_JOB_PERIOD_HOURS * 60 * 60 * 1000), TimeUnit.MILLISECONDS);

For example I use: 例如,我使用:

    private static final int NUM_OF_THREADS = 2;
    private static final String DEFAULT_JOB_START_TIME = "23:30";
    private static final double DEFAULT_JOB_PERIOD_HOURS = 24;

As you see, you can change the number of threads (depends of what your application is doing), the start time (this is just needed for application start (when to start the job the first time). And also the period (every XX hour the job shall run ... I took hours, but you need ti insert milliseconds at the end, so for 5 minutes (you have to tak 5 * 60 *1000 miliseconds. 如您所见,您可以更改线程数(取决于您的应用程序正在执行的操作),启动时间(这是应用程序启动所需要的(第一次启动作业时)以及周期(每XX个)这项工作将要运行一小时...我花了几个小时,但最后需要插入毫秒,因此持续5分钟(您必须花5 * 60 * 1000毫秒。

Greetings 问候

EDIT in respect to the athors comments: To start things on application start, you have several methods. 关于athors注释的EDIT:要在应用程序启动时启动操作,您有几种方法。 One method is to start a servlet on startup like this. 一种方法是像这样在启动时启动servlet。 Insert into the web.xml 插入web.xml

<servlet>
    <servlet-name>ServletStartups</servlet-name>
    <servlet-class>model.initialization.ServletStartups</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

This will call the Class ServletStartups on Application start (the number in load-on-startup is the priority, because you can have multiple entries and can decide which to start first (1, 2, 3 ...) 这将在应用程序启动时调用ServletStartups类(启动时加载中的数字为优先级,因为您可以有多个条目,并且可以决定先启动哪个(1、2、3 ...)

Now within your servlet you defines an init() method, which is automatically called, like that: 现在,在您的servlet中定义一个init()方法,该方法将自动调用,如下所示:

public class ServletStartups extends HttpServlet{


public void init() throws ServletException{
    // HEre you can put your methods as described above      //(scheduledExecutorService( ...   

}

} }

IMPORTANT NOTE: above I had a method "clearRequests", sorry this was my method, I have not renamed it to add it here. 重要说明:上面我有一个方法“ clearRequests”,很抱歉,这是我的方法,我没有重命名就可以在此处添加它。 THis method will be called in my application every 24 hours. 此方法将每24小时在我的应用程序中调用一次。

the methods you call from the ScheduledExecutorService have to be a callable, like this: 您从ScheduledExecutorService调用的方法必须是可调用的,如下所示:

private Runnable clearRequests = new Runnable() { 

    public void run() {
        try {
             // Here do your task
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
};

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

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