开发者

schedule management

Finally after some trial and errors I have managed to make it work as I wanted.

But now I would like your advice to make the code more readable and simple it seems a made a lot of unnecessary code to archive what I wanted.

What this basicly do is, if you turn on the server app at a time a schedule task should be running, it will start the task and let it run for the time left from when it should have started otherwise it will be schedule to run at the hour it is supposed to run.

So if the sche开发者_开发百科dule time is 13:00:00 and should run for 120 minutes and you start the app at 13:30 it will run for 90 minutes. If you start it after that time, it will be normally schedule for the next day 13:00:00.

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        long start_time = calendar.getTimeInMillis() - System.currentTimeMillis();

        if (start_time < 0)
        {
            long minutes = (start_time*-1) / (60 * 1000);
            if (minutes > 0 && minutes < 120)
            {
                runTimeLeft = 120 - minutes;
                ThreadPoolManager.getInstance().schedule(new Runnable()
                {
                    public void run()
                    {
                        myTask();
                    }
                }, 0);
            }
            else
                runTimeLeft = 0;

            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, hour+24);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            start_time = calendar.getTimeInMillis() - System.currentTimeMillis();
        }

        ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
        {
            public void run()
            {
                myTask();
            }
        }, start_time, 24 * 60 * 60 * 1000);

So my question here now is what could I improve on the above code ?


Instead of using java.util.Timer alone, try using it with TimerTask. There is a good article from IBM on this.

Have a look at this link: http://www.ibm.com/developerworks/java/library/j-schedule.html

The code is also shared and seems to work for trivial routine job.


Use this instead for your first method:

int interval = 24 * 60 * 60 * 1000; // might be long instead of int
ThreadPoolManager.getInstance().scheduleAtFixedRate(new Runnable()
{
    public void run()
    {
        myTask();
    }
}, interval, interval);

This will create a simple timer that will call myTask() in 24 hours, and then every 24 hours after.

Your other requirement is a little different, though. If I understand your description correctly, you basically want your app to always execute some task at 12:00 AM if it happens to be up and running. If you don't care about down-to-the-millisecond accuracy for this, you could achieve it very simply by starting a Timer with a one minute period and checking the current system time in each tick - when you hit 12:00 AM run your daily task.

A fancier way would involve interacting with the OS such that it makes callbacks to your application at pre-scheduled times (possibly even starting your app if necessary), but this kind of thing is OS/platform specific (which you didn't specify).

Update: I know nothing about Linux, but it looks like a cron job is what you're looking for. See this question:

Running a scheduled task written in java on a linux server

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜