Results 1 to 17 of 17
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default [SOLVED] Timer Task

    Trying to implement a Timer, runs once a day and should exec during 4-5 am or something. This is what I have so far:

    Java Code:
    class SCHEDULER extends java.util.TimerTask implements Runnable 
    {
        SCHEDULER()
        {
            super();
        }
        public void run()
        {
            ;
        }
    }
    It is a file scope class that is used solely to encapsulate the movement to the active position of one of the resources of a short array of Objects.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Could you give a bit more detail. I'm lost with all the jargon. I'm a bit of a trainee in communicating about programming with the current definitions.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Nick,

    Couple of questions:

    1) have you ever heard of the Sun java coding standards? Things like classes are things that StartWithCapitals and variables haveNamesLikeThis?

    2) why do this all in Java? Sounds like a good thing for a cron job. Then all you need to do is write a two line shell script to call your Java program.

    3) if you need it to be all in Java, what restarts the main loop when the JVM crashes, the server crashes, etc.?

    The basic way to do this is to have code to get the current time, have a Manifest constant (or shell argument) of when you want the code to run, and calculate the approximate time to sleep. Sleep for 90% of that, wake up, recalculate nap time, sleep for 90% of that, repeat until close enough, then fire off the real code.

  5. #5
    daGame is offline Member
    Join Date
    May 2008
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    Trying to implement a Timer, runs once a day and should exec during 4-5 am or something. This is what I have so far:

    Java Code:
    class SCHEDULER extends java.util.TimerTask implements Runnable 
    {
        SCHEDULER()
        {
            super();
        }
        public void run()
        {
            ;
        }
    }
    It is a file scope class that is used solely to encapsulate the movement to the active position of one of the resources of a short array of Objects.
    Scheduling a job? nothing simpler than Quartz Job Scheduler...the distribution has some nice examples to go with which helps.

    HTH.

    regards,

    game.

  6. #6
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default will use basic

    Quote Originally Posted by fishtoprecords View Post
    Nick,

    Couple of questions:

    1) have you ever heard of the Sun java coding standards? Things like classes are things that StartWithCapitals and variables haveNamesLikeThis?
    I use that naming convention routinely, I just copy-pasted from my prototyping sources that I had done within the last two or three minutes. I had placed this with several other private static finals - even though it was a class, it fit into the concept as something that is done at load and stays that way. It fires an event, sorta went to beginner because I am a beginner at this.

    2) why do this all in Java? Sounds like a good thing for a cron job. Then all you need to do is write a two line shell script to call your Java program.
    I have very limited skills with such things as cron and shells. How would a shell script invoke a 24/7/365 thread somewhere down deep in a process that was already running? This is set up on the server and is intended to run inside Tomcat, standalone and able to 'do-all' without any console or operator and do so without being a headless monster.

    3) if you need it to be all in Java, what restarts the main loop when the JVM crashes, the server crashes, etc.?
    The rack this is to run on is proven. They host ten or twenty or a hundred Businss Class sites, actually what we may say is that they vend to hosts that use their equipment,... I do not know the nomenclature. It is like I Deploy, if server crashes, they restart it. I work in an environment ( the code I am writing ) something like: "You're on your own." Seems obvious to me, Servlet has an init() method, it calls my init method. Requests come in, I handle them. Occasionally, the timer comes up and does some swapping of active file to retirement unit and checks memory usage and so on. All of this occurs during early morning hours that maintenance normally happens. I am asleep a thousand miles away.

    The basic way to do this is to have code to get the current time, have a Manifest constant (or shell argument) of when you want the code to run, and calculate the approximate time to sleep. Sleep for 90% of that, wake up, recalculate nap time, sleep for 90% of that, repeat until close enough, then fire off the real code.
    This is the approach I will use, but I have no idea what a manifest constant is. Obviously, we do not want to have a x = x - (1/x) that would drill in, we would use some sort of minimal time block such as you have called close enough. My question then becomes how do we get the Thread? Here is what I have so far, cleaned up for posting:
    Java Code:
    // This does not implement the sleep()
    // It is what I had last night.
    class Scheduler extends TimerTask implements Runnable
    {
        FileWriter[] fileStore;
        Scheduler(FileWriter[] fileArray )
        {
            super();
            fileStore = fileArray;//
        }
        public void run()
        {
            synchronized(fileStore)
            {
                ;
            }
        }
    }
    As we see, the constructor makes the .... I'm a little lost. All the synchronizing and so on is natural for me. I think what we need to do is bump an index into the array: if(++index > limit value){index = base;} and just lock on something in the code so that the round-robin closes the active file and puts the next available up in such a manner that the "main()" thread does not cross the Thread in the Timer. The best I can tell is that what I need to do is create or subclass TimerTask in java.util possibly calling the constructor for a Timer in the init() of my code. The problem here is that the Timer / TimerTask combination is setup as a fixed scheduling tool. That makes initializing it to run at low traffic times a matter of setting a countdown timer in init() that reads the time a calculates a value. To get that to work we could have the Timer fire on a 105 minute interval and check for Time being within a two hour time block set to an arbitrary early morning value.

    It sorta gets into some real computer science.

    Norm: File Scope means ( conceptually ) what is in view when one has a file open in an editor. Encapsulate is to keep reasonable sized work areas so that one is not confused with all the work at once. Active position means it's common terminology. Resources is used here in it's common meaning. An array is a fixed series of locations. Object is fancy terminology for something in computer memory.
    Last edited by Nicholas Jordan; 06-30-2008 at 06:14 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Guess I missed the turn coming into this thread. I thought I was in the "New to Java" thread. The above is a bit past that.
    Thanks for the definitions.

  8. #8
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    I have very limited skills with such things as cron and shells. How would a shell script invoke a 24/7/365 thread somewhere down deep in a process that was already running?

    This is the approach I will use, but I have no idea what a manifest constant is.
    You will have to address your solution space your self. The way that James T Kirk passed the impossible test was to change the rules.

    A cron script is simply kicked off by the operating system as specified. The cron daemon has settings for how often. Daily, hourly, every two minutes, etc. You then simply tell the cron daemon to run the script per the shedule.

    You can do anything in the shell script. You could run a program, tell java to run a particular .jar file at a specific entry point, etc. What I'd probably do here is have the script simply do a 'wget' to your server, using a secret command that caused the periodic action to happen.

    wget http://www.nick.com/secretcommand

    then you can just write a normal servlet to process the 'secretcommand'

    A "manifest constant' is simply a constant that you explicity call out, manifest a value.
    It comes from C, but in java would look like
    public static final float PI = 3.141526;

    or
    public static final int TimeToRunSecret = 030000;

    where you later convert the value into 03:00:00 and run in the middle of the night.

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default j t kirk

    It has been awhile since I watched S.T. - changing rules are for people who live within rules. I neither write them nor read them, what I do is watch the interaction between those who write rules v those who are supposed to read them and understand them. There are no rules where rulers live.

    http://www.nick.com/secretcommand returns 404

    Show how cron can start process Peuford at 4 pm on Mabylenes March Day then run a routine within that process a week later. Not by re-starting the process, instead hook into the instruction pointer for that process that has already paged in the loadable module .....

    Moderators, can you move this to advanced please, we will confuse beginners. Or just delete and I will consider the challenge further before doing additional work.

    This is not a job, this is a thread within a process.

  10. #10
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    You are jumping from a periodic task to a full blown scheduler. I have designed several of them. One was stolen and implemented by a firm in North Carolina that was sold to Seagate in the mid 1990s.

    If you want a general scheduler, you should put the tasks to do in a database table, have a time check process periodically wake up and look for work to do. Read the table, see if there is work to do, etc.

    Err, you have to replace the URL with an appropriate URL that is real and has servlets properly setup.
    Last edited by fishtoprecords; 07-01-2008 at 01:10 AM. Reason: remove bizzare editing

  11. #11
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default sample in Java Tips

    I cannot see your reply, it has not propogated, but I had what may be what I am looking for come up in Java Tips. Between that and the earlier about doing a few calcs and sleeping it off ( while dreaming about sailing ) we can drift along on while I work on this tonight.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default not a task

    This is not a task, it is neither a job nor a chron job. Exe is paged in once, when servlet is loaded. A 'pointer to jump main()' is constructed for the init() call in the servlet, and it just sits there in pageback ram with nothing happening, no instruction pointer or anything. A request comes in, calcs are done to call doPost(... and we now have a process 'running'

    At some time or another, we want to rotate out the active file and bring in a cleared file pointer. To do that, we have to already be within the process. cron cannot do this nor can any other tool except a Thread sitting on it "within process boundaries" sleeps do not cost much. Paging in a process does. If we had an actual "Operating System", we could do a volatile and just set an instruction pointer running on the process once in awhile.

    There is no way for cron to call into a running process, at least nothing that makes sense.

    See Don Lancaster - "Case Against Patents" for your sechduler loss, very remarkable work. Totally changed my World View on beliefs I did not know I had.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  13. #13
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    There is no way for cron to call into a running process, at least nothing that makes sense.
    Perhaps none that make sense in your problem domain.

    its trivial for a script to do a 'wget' to a URL, and the URL can talk to a servlet.
    The rest is just a small matter of programming (aka SMOP)

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default second machine

    its trivial for a script to do a 'wget' to a URL, and the URL can talk to a servlet.

    The rest is just a small matter of programming (aka SMOP)
    That requires that I:
    1. Be awake or have a cron job running.
    2. From a second machine.
    3. Network functional.

    I could, and certainly have given a definite place on my list of possible approaches, have the access protocol to pull the needed data off the server do the active file rotation....

    That accomplishes several tasks, all of them well beyond the scope of New to Java forum.

    Not the least of those is that I am awake, at the console and can see that the server is up after retirieving the data. It would be a straight one-day study for me to do an MD-5 ( or HMAC or SHA-??? ) and place several of them hardcoded in the servlet. Then I have several approach routes and can drill in to various maintinence tasks without keeping the keys anywhere. ( Okay, passphrase. )

    Let's see now;
    "Skip Jack the South Pacific Beer Maker.".....

    No, how about ......


    ( beginners, that's a joke for this guy - he will understand )
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  15. #15
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    That requires that I:
    1. Be awake or have a cron job running.
    2. From a second machine.
    3. Network functional.
    No.
    OK, #1 you have to have a cron script, which can be two lines and take one minute to setup. Either by you or your local sysadmin

    No #2, you can do wget locally

    No #3, you can do wget locally.

  16. #16
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default looks like fully workable solution

    Okay, I'm gonna work on this. The server admin is skilled and I am more than sure that I can place a chron script on the remote. Their help page already has directions on how to do so. Writing the servelet side of the action is well within the skills that I have recently accomplished. What using your apporach does in my problem domain is remove the need for a remote/local traffic pair. I will have to do get-going work on shell scripts generally and as well actually writing the script, it would call something like
    Java Code:
    /?source=script;action=rotate
    I saw the semicolon mentioned as a later standard for name value separators, that may not be correct nor current practice, that would be part of my studies. I can find the RFC within minutes, but given your skills a citation of the relevant RFC would not hurt.

    I studied the sample code on the subject of the post title from java tips, what I can do is have it remotely generate SecureRandom (Java 2 Platform SE v1.4.2), something I had wanted to be able to do anyway. I can setDaemon(true); and setPriority(low); in the initializer. By doing this, I remove the barrier of trying to transmit randomizing material. That would allow all the protections that I can achieve with my current skills to occur solely on the server, they already have some serious security tools in place and functional.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  17. #17
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    in urls and servlets, you use ampersand "&" to separate tag/value pairs, and use equal sign "=" between the tag and value

    someurl/servletalias?tag1=val1&anothertag=secondsval

Similar Threads

  1. Replies: 0
    Last Post: 06-26-2008, 07:32 PM
  2. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  3. Task Blocks 0.5
    By johnt in forum Java Software
    Replies: 0
    Last Post: 08-08-2007, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •