Solved

Schedule Task

Posted on 2004-09-21
35
378 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:Tension
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 11
  • 9
  • +2
35 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110500
What OS?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12110519
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
 

Author Comment

by:Tension
ID: 12110535

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 12110536
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110547
Best to use cron with a script to execute the java
0
 
LVL 35

Expert Comment

by:girionis
ID: 12110551
0
 
LVL 35

Expert Comment

by:girionis
ID: 12110555
> Best to use cron with a script to execute the java

I think cron runs only on Windows, or not?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110559
>>I think cron runs only on Windows, or not?

No - it only runs on *nix
0
 

Author Comment

by:Tension
ID: 12110561
Hi zzynx,
   Thankx, if you can provide me the full code, it would be fine.
0
 

Author Comment

by:Tension
ID: 12110566
The program should run on all OS {Windows,Linux,Unix}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110609
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110622
(and of course what would be wrapped is zzynx's code)
0
 

Author Comment

by:Tension
ID: 12110642
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110660
No - i'm using the word service in its Windoze sense. There, a 'service' means a background process to all intents and purposes
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110676
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
 

Author Comment

by:Tension
ID: 12110712
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110736
>>I need to call another java class

Please expand on this
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12110783
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12110809
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
 

Author Comment

by:Tension
ID: 12110818
HI CEHJ,
     Yeah its right, you need to call another class
0
 

Author Comment

by:Tension
ID: 12110887
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110893
>>
HI CEHJ,
     Yeah its right, you need to call another class
>>

Sorry - i don't understand
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12110925
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12110952
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12110981
>>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
 

Author Comment

by:Tension
ID: 12111001
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12111042
>>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
 
LVL 37

Expert Comment

by:zzynx
ID: 12111066
I know that CEHJ,

But "30 sep 2004" isn't yet passed today (21 sep), is it?
So, it shouldn't fire...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12111080
Maybe you should just copy/paste your entire code, just to check everything is correct?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12111082
Well it depends on where Tension's clock is at the moment ;-) If s/he's testing ...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12111089
>> 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
 
LVL 37

Accepted Solution

by:
zzynx earned 150 total points
ID: 12111147
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
 

Author Comment

by:Tension
ID: 12111196
Hi zzynx,
    thankx for your kindly help, its working as I wanted.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12111213
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12111217
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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question