This become pain in my neck!!! I have three queries.
1)I want to configure CommonsPool2TargetSource in my project for pooling of my custom POJO class.
What I have done so far :
MySpringBeanConfig class :
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.redirect.controller","com.redirect.business","com.redirect.dao.impl","com.redirect.model"})
@EnableTransactionManagement
@PropertySource("classpath:" + JioTUConstant.SYSTEM_PROPERTY_FILE_NAME + ".properties")
@Import({JioTUCouchbaseConfig.class,JioTUJmsConfig.class})
public class JioTUBeanConfig extends WebMvcConfigurerAdapter {
private static final Logger LOGGER = Logger.getLogger(JioTUConfig.class);
@Bean
public CommonsPool2TargetSource poolTargetSource() {
CommonsPool2TargetSource commonsPool2TargetSource = new CommonsPool2TargetSource();
commonsPool2TargetSource.setTargetBeanName("jioTUURL");
commonsPool2TargetSource.setMinIdle(5);
commonsPool2TargetSource.setMaxIdle(5);
commonsPool2TargetSource.setMaxSize(10);
return commonsPool2TargetSource;
}
@Bean
public ProxyFactoryBean proxyFactoryBean() {
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
proxyFactoryBean.setTargetSource(poolTargetSource());
return proxyFactoryBean;
}
@Bean
public MethodInvokingFactoryBean poolConfigAdvisor() {
MethodInvokingFactoryBean poolConfigAdvisor = new MethodInvokingFactoryBean();
poolConfigAdvisor.setTargetObject(poolTargetSource());
poolConfigAdvisor.setTargetMethod("getPoolingConfigMixin");
return poolConfigAdvisor;
}
}
My POJO class inside "com.redirect.model" package:
@Repository
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Document
public class JioTUURL{
@Id
private String keyword;
@Field
private String url;
@Field
private String title;
@Field
private String timestamp;
@Field
private String ip;
@Field
private Integer clicks;
@Field
private String user;
//Getter/Setter
}
Exception I am getting :
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.redirect.model.JioTUURL] is defined: expected single matching bean but found 2: jioTUURL,proxyFactoryBean
FYI I have not define any bean for JioTUURL explicitly. It is up to the @ComponentScan of spring
If I comment the following line, inside proxyFactoryBean() method of JioTUConfig.java class
@Bean
public ProxyFactoryBean proxyFactoryBean() {
ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
// proxyFactoryBean.setTargetSource(poolTargetSource());
return proxyFactoryBean;
}
then it is running fine with log information as below
09-08-2016 16:28:13.866|INFO |localhost-startStop-1|Bean 'poolTargetSource' of type [class org.springframework.aop.target.CommonsPool2TargetSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)|[PostProcessorRegistrationDelegate.java:328]
2)How to fetch objects from pool?
@Controller
public class JioTUController {
private static final Logger LOGGER = Logger.getLogger(JioTUController.class);
@Autowired
private JioTUCommonBusiness jioTUCommonBusiness;
@Autowired
private ObjectFactory<JioTUURL> jioTUURLObjectFactory;//Need to replace I guess
public JioTUController() {
LOGGER.info("Loading JioTUController complete");
}
@RequestMapping(method = RequestMethod.GET, value="/*")
public ModelAndView postDataAsJSON(HttpServletRequest request,HttpServletResponse response, ModelAndView modelAndView) {
//Should be replace with code that fetch object for me from the pool
JioTUURL jioTUURL = jioTUURLObjectFactory.getObject();
//App Business
}
}
3)Is those objects in pool recycled or is going to re-instantiate after each HTTP request served?
I've been confronted to the same problem and have reproduced a simple case that seems to be working. I suppose it is too late to help you, but I hope it might help future readers.
Configuring the pool
You have to configure three elements:
prototype
scope.CommonsPool2TargetSource
, which will require the name of the prototype bean you just configured.ProxyFactoryBean
that will use the just configuredTargetSource
.Quick test
My
Foo
bean is quite simple. It is associated to an ID on creation and simply logs it on logging.Here is a (dirty) class to run this bean several times, in parallel threads.
If you have a look at the log below, you'll see we use only two instances (corresponding to the
maxSize
I set). When both instances are in use, the next threads have to wait for the previous processings to be over, hence a pause of 1 s (the sleep time ofFoo
) in the logs.