Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2008, 12:15 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
[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:

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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-30-2008, 05:18 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 977
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-30-2008, 05:21 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,076
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Me too, I'm not clear exactly what you going to do here?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-30-2008, 06:28 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 257
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-30-2008, 10:04 AM
Member
 
Join Date: May 2008
Posts: 21
daGame is an unknown quantity at this point
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-30-2008, 06:47 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
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.

Quote:
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.

Quote:
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.

Quote:
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:
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Last edited by Nicholas Jordan : 06-30-2008 at 07:14 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-01-2008, 12:37 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 977
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-01-2008, 12:51 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 257
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-01-2008, 01:21 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-01-2008, 01:30 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 257
fishtoprecords is on a distinguished road
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 02:10 AM. Reason: remove bizzare editing
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-01-2008, 01:46 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-01-2008, 01:56 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-01-2008, 02:11 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 257
fishtoprecords is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-01-2008, 02:42 AM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
second machine
Quote:
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 )
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-01-2008, 02:55 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 257
fishtoprecords is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-01-2008, 07:39 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 430
Nicholas Jordan is on a distinguished road
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
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.
__________________
Please provide your feedback on our
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-02-2008, 02:21 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 257
fishtoprecords is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Timer class to schedule a task to execute once 5 seconds have passed Java Tip java.util 0 06-26-2008 08:32 PM
Using a custom task in Ant JavaForums Java Blogs 0 05-25-2008 12:40 PM
Ant exec task JavaForums Java Blogs 0 05-20-2008 01:40 PM
How to cancel an individual timer in spite of canceling whole timer Java Tip java.util 0 04-04-2008 03:46 PM
Task Blocks 0.5 johnt Java Announcements 0 08-08-2007 09:43 PM


All times are GMT +3. The time now is 12:42 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org