javatimerwebspheretimertaskibm-was

Java Timer doesn't fire at Websphere server


I am using java.Util.Timer library for schedule my job.

I wanna call my function at 22:00 every sunday. 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. 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. And restarted server at 10:40 am on monday. After 20 minutes, my function worked at 11:00 am as expected. 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? What could be the reason for this?

here is my startScheduler function

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

Solution

  • In websphere default java time configurations, the week starts with sunday. 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.

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