Schedule Task

Hi,
   I need to schedule a process every end of the month [like an cron job], for the specified hour, minute and seconds.
Can you please help me out in solving this problem.
TensionAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
Oh I see...
Tension you are testing it by setting your clock. (Of course you can't wait for a month ;°)

Well, if you don't want a fire if the time is passed,
then in the getLastfMonth() try replacing

        return cal.getTime();

by

        if ( (cal.getTime()).before(new Date()) ) {
            cal.add(Calendar.MONTH, 2);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        return cal.getTime();
0
 
CEHJCommented:
What OS?
0
 
zzynxSoftware engineerCommented:
You'll have to use

       java.util.Timer.schedule(TimerTask task, Date time);

with time being the specified time at the last day of this month.
When the task triggers, it has to be sheduled again for the end of next month and so on.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TensionAuthor Commented:

Haven't worked much on timer, so if you could provide me the code, it would be helpful for me.
The program generally runs on Unix, Linux.

It should be like a cron job, it generally calls another class on every last day of the month.
0
 
zzynxSoftware engineerCommented:
For determining the last day of this month:

      Calendar cal = Calendar.getInstance();
      System.out.println("The last day of the month " + new SimpleDateFormat("MMMM yyyy").format( cal.getTime()) );
      cal.add(Calendar.MONTH, 1);
      cal.set(Calendar.DAY_OF_MONTH, 1);
      cal.add(Calendar.DAY_OF_MONTH, -1);
      System.out.println("... is " + cal.get(Calendar.DAY_OF_MONTH) );
0
 
CEHJCommented:
Best to use cron with a script to execute the java
0
 
girionisCommented:
0
 
girionisCommented:
> Best to use cron with a script to execute the java

I think cron runs only on Windows, or not?
0
 
CEHJCommented:
>>I think cron runs only on Windows, or not?

No - it only runs on *nix
0
 
TensionAuthor Commented:
Hi zzynx,
   Thankx, if you can provide me the full code, it would be fine.
0
 
TensionAuthor Commented:
The program should run on all OS {Windows,Linux,Unix}
0
 
CEHJCommented:
You'd have to run it as a service on Windows or it would be too exposed. I asked you what OS and you said

>>The program generally runs on Unix, Linux.

or i'd have answered differently. I would use a cross-platform service wrapper such as http://wrapper.tanukisoftware.org/doc/english/index.html
0
 
CEHJCommented:
(and of course what would be wrapped is zzynx's code)
0
 
TensionAuthor Commented:
Hi CEHJ,
    It's program which will running on the server, on every last day of the month the program calls a different process. It's not like service.
0
 
CEHJCommented:
No - i'm using the word service in its Windoze sense. There, a 'service' means a background process to all intents and purposes
0
 
CEHJCommented:
The points is, since you seem to also want to run it on Windows, because Windows is so gui-oriented, if you run it just like any other Java process as opposed to as a service, someone's going to close it down accidentally at some point, if it doesn't crash on its own first
0
 
TensionAuthor Commented:
Hi,
   I think i've confused a bit.
 I need to call another java class every end of the month [like an cron job], for the specified hour, minute and seconds.
0
 
CEHJCommented:
>>I need to call another java class

Please expand on this
0
 
zzynxSoftware engineerCommented:
If you insist ;°)

/*
 * TimerDemo.java
 *
 */

import java.util.*;
import java.io.*;

/**
 *
 * @author  zzynx
 */
public class TimerDemo {
   
    private boolean triggered = false;
   
    /** Creates a new instance of TimerDemo */
    public TimerDemo() {
       
        new Timer().schedule(new TimerTask() {
            public void run() {
                triggered = true;

                // Do the call of that other class

            }
        }, getLastOfMonth());
    }
   
    public Date getLastOfMonth() {
        Calendar cal = Calendar.getInstance();
        if (triggered)
            cal.add(Calendar.MONTH, 1); // add an extra month
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);

        // set the correct time (e.g. 15:30:00)
        cal.set(Calendar.HOUR_OF_DAY, 15);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
   
    public static void main (String args[]) {
        new TimerDemo();
    }
}

You need the "triggered" variable because when it is triggered e.g. at the end of this month (30 sep 2004)
it must calculate the end of the "next" month, not this month.
0
 
zzynxSoftware engineerCommented:
Sorry, I forgot to re-schedule when triggered:

/*
 * TimerDemo.java
 *
 */

import java.util.*;
import java.io.*;
/**
 *
 * @author  zzynx
 */
public class TimerDemo {
   
    private boolean triggered = false;
   
    /** Creates a new instance of TimerDemo */
    public TimerDemo() {
        this(false);
    }
    public TimerDemo(boolean triggeredParam) {
       
        this.triggered = triggeredParam;
        new Timer().schedule(new TimerTask() {
            public void run() {
                new TimerDemo(true);    // <<<<<<<<<  Re-schedule
                // Do the call of that other class
            }
        }, getLastOfMonth());
    }
   
    public Date getLastOfMonth() {
        Calendar cal = Calendar.getInstance();
        if (triggered)
            cal.add(Calendar.MONTH, 1); // add an extra month
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
       
        // set the correct time (e.g. 15:30:00)
        cal.set(Calendar.HOUR_OF_DAY, 15);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
       
        return cal.getTime();
    }
   
    public static void main (String args[]) {
        new TimerDemo();
    }
}
0
 
TensionAuthor Commented:
HI CEHJ,
     Yeah its right, you need to call another class
0
 
TensionAuthor Commented:
Hi zzynx,
    Have one doubt in this, even if I set to different time [17:00:00] it fires, it shouldn't happen rite [if the time is passed or earlier]
0
 
CEHJCommented:
>>
HI CEHJ,
     Yeah its right, you need to call another class
>>

Sorry - i don't understand
0
 
zzynxSoftware engineerCommented:
>>Have one doubt in this, even if I set to different time [17:00:00] it fires, it shouldn't happen rite [if the time is passed or earlier]
I don't have that problem:

I add some logging output:

    public TimerDemo(boolean triggeredParam) {
       
        this.triggered = triggeredParam;
        Date d = getLastOfMonth();
        System.out.println("Scheduled for " + d);
        new Timer().schedule(new TimerTask() {
            public void run() {
                triggered = true;
                new TimerDemo(true);
                System.out.println("Fire!!");
            }
        }, d);
    }

and when I run it, it nicely prints:

Scheduled for Thu Sep 30 17:00:00 CEST 2004

So, it doesn't fire at all.
Can you replace your code by the above?
0
 
CEHJCommented:
That will only work once. The requirement is for periodic repeated execution is it not? If so, and once you've fixed it, you'll find you've rewritten cron. Is there are good reason for doing so, and if so, what is it?
0
 
zzynxSoftware engineerCommented:
>>That will only work once.
Not true. It will be triggered for every end of the month.


Tension,
...and when in the getLastMonth() function I replace

        return cal.getTime();
by

        return (!triggered) ? new Date() : cal.getTime();

It prints as expected:

Scheduled for Tue Sep 21 13:29:22 CEST 2004                     // <<<<< That's the immediate firing (instead of at the end of the month)
Scheduled for Sun Oct 31 17:00:00 CET 2004                       // <<<<< That's the new scheduling
Fire!!
0
 
TensionAuthor Commented:
I've replaced with your code....

when I run it, it prints:

Scheduled for Thu Sep 30 17:00:00 GMT+05:30 2004
Scheduled for Sun Oct 31 17:00:00 GMT+05:30 2004
Fire!!

but the actual time here is : 5:07 PM

If you see, the time has passed, but still it fires.
0
 
CEHJCommented:
>>If you see, the time has passed, but still it fires.

It will, as this the ctor that has been used

"Schedules the specified task for execution at the specified time. If the time is in the past, the task is scheduled for immediate execution."
0
 
zzynxSoftware engineerCommented:
I know that CEHJ,

But "30 sep 2004" isn't yet passed today (21 sep), is it?
So, it shouldn't fire...
0
 
zzynxSoftware engineerCommented:
Maybe you should just copy/paste your entire code, just to check everything is correct?
0
 
CEHJCommented:
Well it depends on where Tension's clock is at the moment ;-) If s/he's testing ...
0
 
zzynxSoftware engineerCommented:
>> If you see, the time has passed, but still it fires.
What do you mean with

       "the time has passed" ?

30 sep 2004 isn't passed yet, is it?

>> but the actual time here is : 5:07 PM
21 september, I presume?

0
 
TensionAuthor Commented:
Hi zzynx,
    thankx for your kindly help, its working as I wanted.
0
 
zzynxSoftware engineerCommented:
Thanks for accepting

>> thankx for your kindly help
You're welcome

>> its working as I wanted.
Good to hear.
But if it is, why didn't you award it with an A-grade? What did you expect more?
0
 
Giant2Commented:
I suggest you to use a "service system" in which you call every end of month the same java.
This java then call your particular java (following what you need to run in that end of month).
It's the most secure algorithm because scheduling a Timer after one month is not a sure way to manage it. During a month your Pc could be restarted and then your timer disappear (if you don't raise up again, like a service).

Hope this help you.
Bye, Giant.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.