0

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);
}
neverwinter
  • 810
  • 2
  • 15
  • 42

2 Answers2

1

There is no idle thread killing mechanism in WebSphere Application Server, so there should not be any reason the timer does not run. 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? That is not recommended in any Java EE server. 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. Otherwise, if upgrading isn't an option, I would suggest using a WebSphere Scheduler.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
0

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);
neverwinter
  • 810
  • 2
  • 15
  • 42
  • Aren’t you referring to behavior of the [`java.util.Calendar`](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html) class? That has nothing to do in particular with [WebSphere](https://en.wikipedia.org/wiki/IBM_WebSphere), so its mention in this Answer is confusing. Also the behavior of `Calendar` depends on the JVM’s current default `Locale`. So your first sentence about a “default java time configurations, the week starts with sunday” is confusing, as there is no one default configuration. Or have I misunderstood? I suggest editing the Answer to clarify. – Basil Bourque May 27 '16 at 23:48
  • I want to emphasize that there is difference between local computer java time and server java time. The reason is this for my question. Sorry for my terms. – neverwinter May 30 '16 at 06:27