Results 1 to 3 of 3

Thread: Initialization of CommonsPoolTargetSource

  1. #1
    Join Date
    Apr 2012
    Posts
    2

    Default Initialization of CommonsPoolTargetSource

    Hi
    I want to have a pool of objects and instantiate them all when I deploy the application.

    Here is the xml snippet where I declare the target bean, the pool and the proxy.

    Code:
    <bean id="myTargetBean" class="package.TargetBean" scope="prototype" lazy-init="false">
    </bean>
    
    <bean id="targetBeanPool" class="org.springframework.aop.target.CommonsPoolTargetSource" lazy-init="false">
      <property name="targetBeanName" value="myTargetBean" />
      <property name="minIdle" value="100" />
    </bean>
    
    <bean id="myPoolFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="targetSource" ref="targetBeanPool" />
    </bean>
    Below part of the code of the targetBean. Because I need some autowired stuff in the constructor, I used the @PostConstructor method

    Code:
    public class TargetBean {
      private Client client;
    
      @PostConstruct
      private void postConstructor(){
        //Some stuff
    
        log.debug("client instanciated : "+client);
      }
    
      public Client getClient() { return client; }
    }
    and ultimately, elsewhere in the code, I autowire the proxy to get the pooled objects.
    Code:
    @Autowired
    TargetBean myPoolFactory;
    
    Client client = myPoolFactory.getClient();
    This code works fine except that only one TargetBean is created when I start the application. I though minIdle would have solved the problem but apparently not.
    What did I miss ?
    Thanks

  2. #2
    Join Date
    May 2012
    Posts
    1

    Default Initialization of CommonsPoolTargetSource

    This method allows the bean instance to perform initialization only possible . If this FactoryBean is not fully initialized yet at the time of the call.



    Trępiller

  3. #3
    Join Date
    Apr 2012
    Posts
    2

    Default

    I have found a workaround. Create a class which extends the CommonsPoolTargetSource and add a public method to instantiate the objects :

    Code:
    	
    public void customInitPool() throws Exception {
      List<Object> beans = new ArrayList<Object>();
    		
      //create and retain minIdle objects to force the creation
      for(int i=0 ; i<this.getMaxSize() ; ++i)
        beans.add(this.getTarget());
        for(Object o : beans)
          this.releaseTarget(o);
    	
          beans.clear();
    }
    And then, add a call to this method in the xml configuration file :
    Code:
    <bean id="targetBeanPool" class="MyCustomPool" init-method="initPoolSeInfo" init-method="customInitPool">
    I am not sure this is the best way to do it, but it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •