Announcement Announcement Module
Collapse
No announcement yet.
Initialization of CommonsPoolTargetSource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment

      Working...
      X