将对象添加到对象池

时间:2014-02-23 07:41:59

标签: java apache apache-commons apache-commons-pool

我正在尝试将对象添加到对象池。我正在使用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);

    }

现在我想将消费者对象添加到我之前创建的池中。我该怎么做?

1 个答案:

答案 0 :(得分:1)

查看此链接,了解有关使用池GenericObjectPool

的更多信息

另一篇可能有帮助的帖子是this

您需要将工厂与ObjectPool关联以创建对象。希望这会有所帮助。