如何在春季创建一个豆池?

时间:2019-07-04 05:16:04

标签: java spring connection-pooling pool

在我的项目中,我需要调用其他应用程序SOAP URL来获取连接,在该连接obj之上,我需要传递各种请求以获取响应。我需要解析此响应以获取实际结果数据。

持有连接时,有一个称为Retrival类对象的API。需要将其用于连接,即传递请求并获取响应对象。

因此我可以将其作为原型范围来获取这些Retrival对象。 但是我使用的Akka Actor使用这些对象获取不同的数据。

我无法使用原型范围创建未知数量的Retrival类的bean。以后可能会导致OOM异常。

我想创建一些说20个Retrival类的bean并使用它们。 我在春季如何使用春季最新版本。

我尝试使用commonspool2targetsource,但是就像从其他应用程序jar导入的Retrival类一样……在类路径中无法识别Retrival.class。

不确定如何处理,非常感谢您的帮助。

以下是我正在执行的示例代码:

@Bean(name="retriver")
    @Scope("prototype")
    public Retriver retriver() {

        Retriver dr = null;
        try {
            dr = new Retriver(config.getUserName(),
                    config.getPassword(),
                    config.getUrl());
        } catch (Exception e) {
            logger.error("Exepction in creating SOAP datasource connection");
        } 

        return dr;
    }


    @Bean
    public CommonsPool2TargetSource RetriverPooledTargetSource() {
        final CommonsPool2TargetSource commonsPoolTargetSource = new CommonsPool2TargetSource();
        commonsPoolTargetSource.setTargetBeanName("Retriver");
        commonsPoolTargetSource.setTargetClass(Retriver.class); //Getting error here....
        commonsPoolTargetSource.setMaxSize(10);
        return commonsPoolTargetSource;
    }

    @Bean
    public ProxyFactoryBean proxyFactoryBean() {
        ProxyFactoryBean p = new ProxyFactoryBean();
        p.setTargetSource(RetriverPooledTargetSource());
        return p;
    }

错误:无法解析类型org.apache.commons.pool2.PooledObjectFactory。从所需的.class文件中间接引用了

第2部分问题:

更改到common-pool2以下。.先前的错误消失了。

<dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>          
    </dependency>

但是当我说如下的pool.getActive时,显示的计数为0

CommonsPool2TargetSource pool  = configurationUtils.retrieverPooledTargetSource();
        System.out.println( " pool.getActiveCount " +  pool.getActiveCount());

也没有例外,但是当我检查了上面的池对象时。 有一个文件名为creationException

内容如下:

java.lang.Exception
    at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:147)
    at org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:105)
    at org.springframework.aop.target.CommonsPool2TargetSource.createObjectPool(CommonsPool2TargetSource.java:225)
    at org.springframework.aop.target.CommonsPool2TargetSource.createPool(CommonsPool2TargetSource.java:205)
    at org.springframework.aop.target.AbstractPoolingTargetSource.setBeanFactory(AbstractPoolingTargetSource.java:83)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1767)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1732)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:394)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:366)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f.dataRetrieverPooledTargetSource(<generated>)
    at com.spgmi.ca.prescore.config.ConfigurationUtils.proxyFactoryBean(ConfigurationUtils.java:60)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f.CGLIB$proxyFactoryBean$2(<generated>)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f$$FastClassBySpringCGLIB$$861ac14b.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
    at com.spgmi.ca.prescore.config.ConfigurationUtils$$EnhancerBySpringCGLIB$$f3eea85f.proxyFactoryBean(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getSingletonFactoryBeanForTypeCheck(AbstractAutowireCapableBeanFactory.java:974)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryBean(AbstractAutowireCapableBeanFactory.java:848)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isTypeMatch(AbstractBeanFactory.java:565)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:514)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:485)
    at org.springframework.beans.factory.BeanFactoryUtils.beanNamesForTypeIncludingAncestors(BeanFactoryUtils.java:227)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1403)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1202)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
    at com.spgmi.ca.prescore.PreScoreProducerApplication.main(PreScoreProducerApplication.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

问题3

致电时

   CommonsPool2TargetSource pool  = configurationUtils.dataRetrieverPooledTargetSource();

    dataRetriever  = (DataRetriever) pool.getTarget();
    System.out.println(" " + dataRetriever);

它引发以下异常:我在这里做什么错了?

由于:java.lang.IllegalStateException:没有可用的Commons ObjectPool     在org.springframework.util.Assert.state(Assert.java:73)     在org.springframework.aop.target.CommonsPool2TargetSource.getTarget(CommonsPool2TargetSource.java:234)     在com.spgmi.ca.prescore.actor.CompanyTransIndustryActor.preStart(CompanyTransIndustryActor.java:63)     在akka.actor.Actor $ class.aroundPreStart(Actor.scala:510)     在akka.actor.UntypedActor.aroundPreStart(UntypedActor.scala:95)     在akka.actor.ActorCell.create(ActorCell.scala:590)处     ...还有9个

1 个答案:

答案 0 :(得分:1)

您需要添加以下依赖关系(这是针对Maven构建的):

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC