简体   繁体   English

Java Timer不会在Websphere服务器上触发

[英]Java Timer doesn't fire at Websphere server

I am using java.Util.Timer library for schedule my job. 我正在使用java.Util.Timer库来安排我的工作。

I wanna call my function at 22:00 every sunday. 我想在每个星期天22:00致电我的职能部门。 So my code is like : 所以我的代码是这样的:

rspServer = new RSPServer();

Calendar scheduleStartTime = Calendar.getInstance();
scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
scheduleStartTime.set(Calendar.HOUR_OF_DAY, 22);
scheduleStartTime.set(Calendar.MINUTE, 0);
scheduleStartTime.set(Calendar.SECOND, 0);
scheduleStartTime.set(Calendar.MILLISECOND, 0);

rspServer.startScheduler(Class.forName("xx.xx.xx.xx.RSP.RSPServer"),rspServer, "PopulateModels", scheduleStartTime, 7 * 24 * 60 * 60 * 1000); 

I deployed that code and restarted websphere server at thursday. 我部署了该代码,并在星期四重新启动了Websphere Server。 I was expecting that function will be start at sunday. 我原以为该功能将在星期日开始。 But it didn't fire. 但是它没有开火。 There is no error or out log in the server. 服务器中没有错误或注销日志。

I tried that 我尝试过

I updated the start time as monday 11:00 am. 我将开始时间更新为星期一11:00 am。 And restarted server at 10:40 am on monday. 并于星期一上午10:40重新启动服务器。 After 20 minutes, my function worked at 11:00 am as expected. 20分钟后,我的功能按预期在上午11:00工作。 But if I restart my server at thursday and set the start time as sunday it did not fire. 但是,如果我在星期四重新启动服务器并将开始时间设置为星期日,则不会启动。

Is there a kind of idle thread kill time or sth else in webshere server? Webshere服务器中是否有某种空闲线程消磨时间或其他方式? What could be the reason for this? 这可能是什么原因?

here is my startScheduler function 这是我的startScheduler函数

public void startScheduler(final Class<?> serverClass, final Object server,
        final String methodName, Calendar _startAfterTime, int _repeatAfterTime) {
    timer = new Timer();
    TimerTask timerTask = new TimerTask() {
        private java.lang.reflect.Method method;

        @Override
        public void run() {
            try {
                method = serverClass.getMethod(methodName);
                method.invoke(server);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                System.err.println("Error!");
                e.printStackTrace();
            }
        }
    };
    timer.schedule(timerTask, _startAfterTime.getTime(), _repeatAfterTime);
}

There is no idle thread killing mechanism in WebSphere Application Server, so there should not be any reason the timer does not run. WebSphere Application Server中没有空闲的线程杀死机制,因此不应有任何原因导致计时器不运行。 I would suggest taking a core thread dump to see that your thread is still running (perhaps immediately after it was scheduled), and perhaps a core dump to inspect the field values to ensure they have expected values. 我建议您进行一次核心线程转储,以查看您的线程仍在运行(也许在计划之后立即运行),或者建议进行一次核心转储,以检查字段值以确保它们具有预期值。 My best guess would be that this logic to start the timer is not actually running for some reason. 我最好的猜测是由于某种原因,启动计时器的逻辑实际上并未运行。

That said, why are you using java.util.Timer in the first place? 就是说,为什么首先要使用java.util.Timer? That is not recommended in any Java EE server. 不建议在任何Java EE服务器中使用。 If you're using WebSphere 8.0 or later, I would recommend using an EJB scheduled time, which you can configure with cron-like syntax. 如果您使用的是WebSphere 8.0或更高版本,我建议您使用EJB计划时间,您可以使用类似于cron的语法进行配置。 Otherwise, if upgrading isn't an option, I would suggest using a WebSphere Scheduler. 否则,如果无法升级,则建议使用WebSphere Scheduler。

In websphere default java time configurations, the week starts with sunday. 在Websphere默认Java时间配置​​中,星期从星期日开始。 In Turkey the week starts with monday. 在土耳其,一周从星期一开始。 So when I set the date to Sunday at Wednesday by the code : 因此,当我通过代码将日期设置为星期三时:

scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

It sets next Sunday in my local but in websphere it sets previous Sunday. 它在我的本地设置下一个星期日,但在Websphere中设置在上一个星期日。

In timer function, if your set start date is smaller than current date, timer function does not fire. 在计时器功能中,如果您设置的开始日期小于当前日期,则计时器功能不会触发。 It is library restrict. 是图书馆限制。 So I fixed my problem like 所以我解决了我的问题

Calendar scheduleStartTime = Calendar.getInstance();
scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
scheduleStartTime.set(Calendar.HOUR_OF_DAY, 11);
scheduleStartTime.add(Calendar.DAY_OF_YEAR, 7); // added this line to set next sunday
scheduleStartTime.set(Calendar.MINUTE, 0);
scheduleStartTime.set(Calendar.SECOND, 0);
scheduleStartTime.set(Calendar.MILLISECOND, 0);

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

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