简体   繁体   中英

Timeout mechanism with changeable usage time

I want to implement mechanism which will be closing connections if there are not used by specific period of time. This time is constant for all of the connections. Opened connections can be used many times, so I need to update usage time and always compute difference between current time and usage time. I also need to close connections which excess my timeout.

My opened connections are in Map. ( Map<Id, Connection> ) where Id is an Integer .

I thought about resolving my problem with DelayQueue , but there is no possible to update usage (in this case delay) time in this type of Queue .

I also know that this mechanism should work in separate thread.

Please, give me some tip about the best way of implementation or example. What kind of data structure should I use? I can use Spring also (maybe there is some good mechanism and I don't know about it).

If you're speaking about database connectivity then just use connection poolers such as c3po , hikariCP , BoneCP and so on. Don't reinvent the wheel.

Take a look at the HikariCP code. Specifically, look at:

While HikariCP is a database connection pool, you can use ConcurrentBag as is, use the HouseKeeper basically as is, slightly modify PoolBagEntry, and lift the basic gist from getConnection() and releaseConnection(), to create a generic pool.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM