Marco Ehrentreich wrote:Hi Jimi,
mabey Java's DelayQueue is of any help for you. Another interesting library I've found with a quick Google search seems to be this Task Queue API although I must admit that I haven't yet used neither the DelayQueue nor this API.
Anyway, I think no matter which solution or API you use there will be some work to be done on your side. You can't just simply slow down the execution of your Java code. So you have to define some kind of "steps" you want to be executed with a delay. I don't know the details of your project but I'm pretty sure that there will be no API available which can in a magical way figure out which steps of your program to slow down in a reasonable way.
Perhaps you should dissect the whole work to be done in smaller "working packages" or however you want to name it an use something like the DelayQueue to enqueue these tasks and execute them with some delay between each task.
Marco Ehrentreich wrote:Hi Jimi,
as I said it's hard to decide what would be a good solution in your specific case without knowing all details. So I can only come up with some (hopefully) useful advices...
For the size of your tasks I agree that it doesn't make much sense to make it smaller than one POST request at a time. Of course it doesn't make much sense to split one request and risk timeouts etc.
I'm still not sure what's the big deal with the delay you want to specify as late as possible. If you would use such a DelayQueue I don't see why it should be a problem to prepare a collection of task and built and populate the queue just when you know what the delay should be. You could even destroy the queue and build a new one if the delay changes in-between.
But I've found a specific ExecutorService in the JDK's concurrent util package which looks even more promising than the DelayQueue: ScheduledExecutorService
It's in general better and more high-level not to take care of the thread handling yourself but to use the ExecutorService facility for this. I just didn't know about this specific executor before. But it seems to do pretty much what you need.
There's a small example in the API doc, too. You'd basically have to encapsulate your working tasks (of sending a POST request) inside a Runnable or Future and enqueue it into the ExecutorService as soon as you know the delay between jobs.
The only other more flexible but also more complex solution for scheduling of jobs I know of, would be the Quartz framework. But for my understanding of what you're trying to achieve this would be overkill.
Hope this one is more helpful
Marco
Consider Paul's rocket mass heater. |