Pankaj Singhal
Pankaj Singhal

Reputation: 16053

implement a scheduler in spring

A project requires the following scenario to happen:

class A has some objects(dynamically created) which it generates along with a time interval associated with each object. This class needs the generated object after this time interval only. I need to implement a mechanism which provides the class with the objects after it's associated time interval. It may also, sometime, need a particular object before the time interval expires.

Here's what I did:

This is not a very efficient implementation itself. It has a logical error associated with it. When the objects are very low in number(like 1 or 2), the objects are not in the queue and whenever I try to remove the object from the queue, it shows unsuccessful, because the while loop is having the object all the time. And hence I had to put a sleep to avoid that. I don't want to check each and every time interval when the queue size grows up to 10K or so.

Is there any simple and efficient way to achieve the solution. Any help appreciated.

Upvotes: 0

Views: 292

Answers (1)

Alexander Kudrevatykh
Alexander Kudrevatykh

Reputation: 870

You can use ConcurrentTaskScheduler, or implement something like it using DelayQueue. You can use it with spring's Concurrent framework (for example subclassing ThreadPoolExecutorFactoryBean) or with core java Executors.

Upvotes: 1

Related Questions