0

I need to create channels to other nodes (channels to RabbitMQ nodes which aren't thread safe). I can not create a new channel each time a user sends a message (It is too costly and also RabbitMQ has some limitations for it). So, I decided to create 20 channels and use only them (fixed channels which won't be closed if they are unused for some time). The problem is that I have to write a special queue for it with a special logic but it would be better if there is already a library which can do it.

Here is simple rules which have to be in the queue:
1. When user want to get an object he will get an already created object from the queue.
2. If there is no available object then user will wait till he get an object.
3. If user "A" asked for an object earlier than user "B" then user "B" will get an object only after user "A".

Is there an implementation of such resource pool queue?

Oleksandr
  • 3,574
  • 8
  • 41
  • 78
  • [apache commons-pool](https://commons.apache.org/proper/commons-pool/)? Combine with a `BlockingQueue` like in [here](http://stackoverflow.com/questions/15058247/java-concurrent-object-pool). – Adrian Colomitchi Oct 05 '16 at 13:51
  • I am not sure that it can be configured for it.. If you know how to write such a pool with Apache commons-pool post an answer please – Oleksandr Oct 05 '16 at 13:59
  • But I reckon you be able to understand how a `LinkedBlockingQueue` supports your [capacity-limited](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingQueue.html#LinkedBlockingQueue(int)) fair pooling - so that only the "release/return back to pool" problem need to be addressed, right? (it's 1AM where I live, don't ask me to write code at this hour). – Adrian Colomitchi Oct 05 '16 at 14:06
  • 1
    @AdrianColomitchi `LinkedBlockingQueue` uses non-fair locking though, so rule #3 would be violated in this case. You meant `ArrayBlockingQueue` I guess. – bashnesnos Oct 05 '16 at 15:46
  • @bashnesnos How's FIFO non-fair? Quoting with emphasis: "An optionally-bounded blocking queue based on linked nodes. *This queue orders elements FIFO (first-in-first-out).*" – Adrian Colomitchi Oct 05 '16 at 21:29
  • @bashnesnos thank you. It is what I need. – Oleksandr Oct 06 '16 at 05:47
  • @AdrianColomitchi bashnesnos is right. LinkedBlockingQueue doesn't support fairness but ArrayBlockingQueue does. FIFO is the method for elements ordering. Both queues use this method but fairness can be enabled only in ArrayBlockingQueue. – Oleksandr Oct 06 '16 at 05:52

0 Answers0