2

Very less documentation is present for this online. Does anyone know how to add new K,V to this KeyedPool. There is an entity for addObject, but how is it referred to k, v?

Changes to question: [One example I found] http://www.javacodegeeks.com/2013/02/pool-of-ssh-connections-using-apache-keyedobjectpool.html

Suppose I want to implement a Keyed Pool of connections, where every key refers to one connection:

for example: KeyObjectPool item

So my question how do i add new connections to this? is there something like item.add(key, value) ?

2
  • What? I have no idea what you're asking. Could you try rephrasing the question, or maybe give an example what you're trying to do? Commented Sep 9, 2013 at 15:57
  • I have updated the question, please check Commented Sep 9, 2013 at 16:02

3 Answers 3

2

The answer is you can't, because you're not supposed to. The idea of a KeyedObjectPool is that the pool will make objects (the values) for you using a factory.

For an example implementation, take a look at GenericKeyedObjectPool:

Of particular interest are the methods starting on Line 1058.

Edit: It should be noted that the setFactory() method is being deprecated, so you want to avoid it.

I think the fundamental problem you are facing is that a KeyedObjectPool is designed to figure out object creation (or values) for you, but want to specifically define your own objects (in this case connection configurations) and assign them as values to keys.

I think what you really want to use is a Map, but perhaps you don't have control over this and are working with a framework already built around a KeyedObjectPool. If this is the case, you are facing deeper fundamental architectural design flaws in what you are trying to do.

Without knowing more of the big picture of what you're attempting, I can only suggest that maybe you need to rethink your design.

Sign up to request clarification or add additional context in comments.

4 Comments

Do it means whenever we do a addObject a new instance is created from Factory and placed in the pool? how would we work if we want different keys to have different connection configrations?
Depending on the implementation of KeyedObjectPool, usually yes. As to how you would make different keys have different connection configurations, you don't have control over that unless you have control over the implementation of KeyedObjectPool, or possibly the implementation of its KeyedPoolableObjectFactory. I'm wondering now why you are using KeyedObjectPool instead of just using a HashMap. I'm going to edit my answer to give some more thoughts.
But the design docs say "A keyed pool pools instances of multiple types. Each type may be accessed using an arbitrary key." what is meant by multiple types here then? ref : commons.apache.org/proper/commons-pool/api-1.6/org/apache/… Am i missing something?
I'm not sure what they mean by "type", their javadocs are unfortunately vague and seemingly inconsistent on this point. I think if anything it's the javadocs that are missing something.
0

In this case K and V are generic.
This means that they can be anything.

You will tell me, yes, but how I know that a K or a V is?
Simple! It's you that have to tell the class what Type that fields should be.

for Example.

KeyedObjectPool<String,String> kop = new KeyedObjectPool<String,String>();

You have created a new Object that hanldes Keys that are Strings and Values also Strings.

If you try this:

kop.addObject("hello");

will work, if you try this:

kop.addObject(new POJO());

will fail.

Pretty simple.

(p.s. POJO is a Plain Old java object, any class you create for example)

2 Comments

I agree on this. but how is "Hello" in your example linked to its value? Suppose I want to add <Hello, You> to this keyedobject pool kop.addObject("hello") but how is the value linked to it?
it depends on what kind of factory you are using, it's factory duty to create the obj, when you pass the key to the Pool it ask to the factory the obj. the factory creates the object and returns it to the pool. this or this are an interesting and important reading. I'm not wizard of obj pooling. I think if you want to implement such a thing you have to clear you mind upon it.
0

Pass an KeyedPoolableObjectFactory to the GenericKeyedObjectPool in the constructor. The KeyedPoolableObjectFactory has an method

public Object makeObject(Object key) throws Exception {

in which you contruct a new value for the pool based on the key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.