2014-02-23 166 views
0

我正在嘗試將對象添加到對象池。我爲此使用了apache commons pool。將對象添加到對象池

我的代碼如下:

public ObjectPool<OAuthConsumer> consumerPool; 

public ObjectPool<OAuthConsumer> consumerPool; 


public void buildConsumerPool(){ 

    //setting the config path 
    PropertyHandler.setConfigPath("/twitter.properties"); 

    //fetching required tokens for all apps 
    String consumerKeySet = PropertyHandler.getProperty("consumerKey"); 
    String consumerSecretSet = PropertyHandler.getProperty("consumerSecret"); 
    String accessTokenSet = PropertyHandler.getProperty("accessToken"); 
    String tokenSecretSet = PropertyHandler.getProperty("tokenSecret"); 

    String[] splitconsumerKeys = consumerKeySet.split(","); 
    String[] splitconsumerSecret = consumerSecretSet.split("."); 
    String[] splitaccessToken = accessTokenSet.split(","); 
    String[] splittokenSecret = tokenSecretSet.split("."); 

    //creating consumer objects for each app 
    for (int numberOfAccounts = 0; numberOfAccounts < splitconsumerKeys.length; numberOfAccounts++) { 

      String consumerKey = splitconsumerKeys[numberOfAccounts]; 
      String consumerSecret = splitconsumerSecret[numberOfAccounts]; 
      String accessToken = splitaccessToken[numberOfAccounts]; 
      String tokenSecret = splittokenSecret[numberOfAccounts]; 
      OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); 
      consumer.setTokenWithSecret(accessToken, tokenSecret); 

    } 

現在我想消費對象添加到我之前創建的池。我怎麼做?

+0

consumerPool.addObject(消耗)沒有辦法嗎? – Raam

+0

addObject不接受參數。我檢查了。 –

回答

1

檢查此鏈接獲取更多信息使用池GenericObjectPool

另一個職位上可能有助於爲this

你需要一個工廠與ObjectPool的創建對象相關聯。希望這可以幫助。

+0

謝謝,我希望它比這更直接。 addObject應該完成了這項工作。現在試圖聯合工廠。 –

+0

@NischalHp請考慮接受答案,如果你認爲這有幫助。 – Raam

+0

完成,它實際上幫助,但我最終沒有使用它。 –