hey, friends here, I need to write a code which can auto check whether the resource is available then assign jobs to this reource.
[here I got two problems]
1. how to make this code auto running that it can check from database and retrieve the status of resource and the suspended jobs, then assign jobs to resource. a while loop maybe a choice, but it is not user friendly, coz it is a non-stop running one. I am thinking is it possible to write a code that it can check the database every 5 or 8 mins and execute jobs.
can anyone suggest me a way or any software that I can rely on?
2. there are alway situation when available resource are less than waiting jobs. e.g. 2 workers available but 4 waiting jobs. but should I deal with those suspending jobs? to make it specifically, how to automatically let those jobs form a queue and assign job to resource whenever it is available?
You have a couple problems we can talk about separately, I think.
1) Do something periodically. There are timers and scheduling packages and a variety of techniques you can use. What is your environment? A desktop Swing app? EJBs? Some other server?
2) Match work to available resources. Sounds like a classic "producer consumer" situation. Again your environment will have a lot of influence on the design ... how stateful you want it to be, can you use threads and so on.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Jan 22, 2005
my working device is just a laptop and tomcat software within it.
can you introduce some timer and scheduling method?
Joined: Jan 29, 2003
Java has a Timer class that is pretty slick. You give it a Runnable command and it runs the command at some interval. You could use that to check the database for new data.
JDK 1.5 has a Queue which is a good mechanism to let resources pick up jobs. Look at BlockingQueue in the JavaDoc. As a resource becomes available it pulls a job from the queue; if no jobs are available the resource blocks.
You described assigning jobs to resources, which might work better with a pool of resources or a queue per resource and some algorithm to decide which resource to use for each job. That's a little trickier, so I'll pretend I didn't see it for now.
So, it may be as simple as the database monitor coded in a Runnable
And a bunch of resources that try to get work:
That leaves out a lot. Let us know if the concepts sound good and we can talk about some of the more detailed bits.
Joined: Jan 22, 2005
thanks bros, what you intorduced is just damn fit for my problem.
but can you decribe more or give more details that I can study more deep on it.
coz I am using jdk1.4, does that mean I have redownload the jdk1.5?
When I read your original question, the first thing that came to mind is making it multi-threaded. This topic was touched on by some of the above suggestions. (The Runnable interface is part of Java's threading mechanism.) I'd suggest that you look into multi-threading in more detail.
You know, this may be so simple I went right past it. How about an all database solution:
In my work life, our users have a "get next" button. We do something like this where "queues" are really just a database table. The "most appropriate task" is based on some fields on the rows in the queue table and SQL pulls them out very nicely. In your world there might be several tasks stacked up for a resource; the resource could do one task and repeat the query until no tasks are found then sleep (or exit) until the timer comes around again.
Loading em up in memory in queues and stuff is great fun, but it can be really tough to keep your queues and database in sync when you shut down or crash or work in a cluster.