为什么flexy池在ApacheStorm中通过spring配置时会导致nosuchelementexception

n8ghc7c1  于 2021-06-24  发布在  Storm
关注(0)|答案(1)|浏览(304)

我有一场Apache风暴( 1.1.1 )基于spring的应用程序( 5.0.3 )配置和注入基于hibernate的( 5.2.12 )连接到postgresql数据库的数据库服务。
在hibernate和数据库之间,我配置了flexy池( 2.0.0 )在hikaricp之上( 2.7.6 )作为数据源(参见图a)。这个应用程序是在一些使用最新openjdk8版本的ubuntu16.04lts服务器示例上启动的。

我遇到了与 flexy-pool 开始我的工作后 Storm 拓扑结构。然而,这不是确定性的。有时它会工作,有时会抛出下面的异常。如果我不使用 flexy-pool 在我的应用程序(参见图b)中,每件事都按预期进行。

Caused by: 
 org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.vladmihalcea.flexypool.config.Configuration]: Factory method 'build' threw exception; nested exception is java.util.NoSuchElementException at 
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at 
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ... 42 more  
Caused by: 
 java.util.NoSuchElementException at 
sun.misc.URLClassPath$1.nextElement(URLClassPath.java:282) at 
sun.misc.URLClassPath$1.nextElement(URLClassPath.java:256) at 
java.net.URLClassLoader$3$1.run(URLClassLoader.java:603) at 
java.net.URLClassLoader$3$1.run(URLClassLoader.java:599) at 
java.security.AccessController.doPrivileged(Native Method) at 
java.net.URLClassLoader$3.next(URLClassLoader.java:598) at 
java.net.URLClassLoader$3.hasMoreElements(URLClassLoader.java:623) at 
sun.misc.CompoundEnumeration.next(CompoundEnumeration.java:45) at 
sun.misc.CompoundEnumeration.nextElement(CompoundEnumeration.java:58) at 
java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:357) at 
java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:393) at 
java.util.ServiceLoader$1.hasNext(ServiceLoader.java:474) at 
com.vladmihalcea.flexypool.metric.MetricsFactoryResolver.resolve(MetricsFactoryResolver.java:27) at 
com.vladmihalcea.flexypool.config.Configuration$Builder.build(Configuration.java:165) 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)

图b中使用的Spring配置如下(基于官方示例):

<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariCP"/>
    <property name="connectionTestQuery" value="SELECT 1"/>
    <property name="dataSourceClassName" value="${gmetric.dataSourceClassName}"/>
    <property name="dataSourceProperties">
        <props>
            <prop key="url">${shc.gmetric.url}</prop>
            <prop key="user">${shc.gmetric.username}</prop>
            <prop key="password">${shc.gmetric.pw}</prop>
        </props>
    </property>
</bean>

<bean id="poolingDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig"/>
</bean>

<bean id="configurationBuilder" class="com.vladmihalcea.flexypool.config.Configuration$Builder">
    <constructor-arg value="uniqueId"/>
    <constructor-arg ref="poolingDataSource"/>
    <constructor-arg value="#{ T(com.vladmihalcea.flexypool.adaptor.HikariCPPoolAdapter).FACTORY }"/>
</bean>

<bean id="configuration" factory-bean="configurationBuilder" factory-method="build"/>

<bean id="flexyPoolDataSource" class="com.vladmihalcea.flexypool.FlexyPoolDataSource" init-method="start" destroy-method="stop">
    <constructor-arg ref="configuration"/>
    <constructor-arg>
        <array>
            <bean class="com.vladmihalcea.flexypool.strategy.IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory">
                <constructor-arg value="5"/>
            </bean>
            <bean class="com.vladmihalcea.flexypool.strategy.RetryConnectionAcquiringStrategy.Factory">
                <constructor-arg value="2"/>
            </bean>
        </array>
    </constructor-arg>
</bean>

<bean id="entityManagerFactoryGMetrics"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="flexyPoolDataSource"/>
    <property name="persistenceUnitName" value="${gmetric.context.name}"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <!--<property name="generateDdl" value="true" />-->
            <property name="databasePlatform" value="${hibernate.dialect}"/>
            <property name="showSql" value="${hibernate.showSQL}"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.connection.driver_class">${gmetric.driverClassName}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
        </props>
    </property>
</bean>

我的问题是:

是我的基于spring的(默认)配置 flexy-pool 对的?
有没有一个解决方案来防止与此相关的运行时异常 flexy-pool ?
原因是什么 NoSuchElementException ?

g2ieeal7

g2ieeal71#

我发现dropwizard metrics 4(这是 flexy-pool )在我的着色jar中丢失了,因此发生了此有线运行时异常:

<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>${metrics.version}</version>
</dependency>

据笔者介绍,这是相关的方式 flexy-pool 检测相关的dropwizard度量处理程序(因为它支持dm3和dm4,不能互换使用)。

相关问题