Apache camel 中的 PropertyAccessException Spring Ws 配置

PropertyAccessException in Apache camel Spring Ws Configuration

我想配置 Spring-ws 连接管理器以使用 PoolingHttpClientConnectionManager。这是我的配置

驼色-context.xml

<camel-context>
    <route>
        <from uri="direct:sampleCall"/>
        <to uri="bean:sampleCommand"/>
        <to uri="spring-ws:http://{{sample.uri}}?timeout={{sample.timeout}}&amp;messageSender=#messageSender&amp;soapAction=urn:MidwareWebServiceIntf-MidwareWebService#sampleCommand"/>
        <to uri="bean:xmlProcessor"/>
        <to uri="bean:validateResponse"/>
    </route>

    <route>
        <from uri="direct:anotherSampleCall"/>
        <to uri="bean:anotherSampleCommand"/>
        <to uri="spring-ws:http://{{anotherSample.uri}}?timeout={{anotherSample.timeout}}&amp;messageSender=#messageSender&amp;soapAction=urn:MidwareWebServiceIntf-MidwareWebService#ExecuteCommand"/>
        <to uri="bean:trimResponse"/>
        <to uri="bean:xmlProcessorTrim"/>
        <to uri="bean:validateResponse"/>
    </route>



</camelContext>



<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
    <property name="httpClient" ref="httpClient"/>
    <property name="readTimeout" value="30000"/>
    <property name="connectionTimeout" value="30000"/>
</bean>


<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
      factory-method="custom">
    <property name="socketTimeout" value="30000" />
    <property name="connectTimeout" value="30000" />
</bean>

<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />

<bean id="poolingConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
    <property name="defaultMaxPerRoute" value="20"/>
    <property name="maxTotal" value="20"/>
</bean>

<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
      factory-method="create">
    <property name="defaultRequestConfig" ref="requestConfig" />
    <property name="connectionManager" ref="poolingConnectionManager"/>
</bean>



<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />

但是当我 运行 项目时,我得到以下异常

org.springframework.beans.factory.BeanCreationException: Error creating bean     with name 'messageSender' defined in class path resource [camel-context.xml]
: Error setting property values; nested exception is   org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1:  org.springframework.beans.MethodInvocationException: Property 'readTimeout'  threw exception; nested exception is java.lang.Uns
upportedOperationException
PropertyAccessException 2:  org.springframework.beans.MethodInvocationException: Property 'connectionTimeout' threw exception; nested exception is java.la
ng.UnsupportedOperationException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1506)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4751)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5175)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:724)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:700)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:919)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1704)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested   PropertyAccessExceptions (2) are:
PropertyAccessException 1:    org.springframework.beans.MethodInvocationException: Property 'readTimeout'    threw exception; nested exception is java.lang.Uns
upportedOperationException
PropertyAccessException 2:  org.springframework.beans.MethodInvocationException:  Property 'connectionTimeout' threw exception; nested exception is java.la
ng.UnsupportedOperationException
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1502)
    ... 26 more
15-Mar-2016 11:28:47.846 SEVERE [localhost-startStop-1]   org.apache.catalina.core.StandardContext.startInternal Error listenerStart
15-Mar-2016 11:28:47.847 SEVERE [localhost-startStop-1]       org.apache.catalina.core.StandardContext.startInternal Context [/camel##2.0.0]   startup failed due
to previous errors

这是否与我一起使用的API版本不兼容有关?

是的,HttpClient.getParams() 已被弃用,现在抛出 UnsupportedOperationException。

HttpComponentsMessageSender 的方法 setReadTimeout() 和 setConnectionTimeout() 除了在底层 HttpClient 上设置这些值外什么都不做。

因此您必须依赖 requestConfigBuilder(就像您已经为 socketTimeout 所做的那样)来直接配置 HttpClient。

感谢@smarquis 非常有用的回答,我终于可以正确配置 Spring-Ws Connection Manager。这是正确的配置。不要忘记使用 interceptedHttpClientBuilder 部分。如果没有它,您会遇到此异常:org.apache.http.ProtocolException: Content-Length header already present(有关详细信息,请参阅此 thread

骆驼语境

<camel-context>
    <route>
        <from uri="direct:sampleCall"/>
        <to uri="bean:sampleCommand"/>
        <to uri="spring-ws:http://{{sample.uri}}?messageSender=#messageSender&amp;soapAction=urn:MidwareWebServiceIntf-MidwareWebService#ExecuteCommand"/>
        <to uri="bean:trimResponse"/>
        <to uri="bean:xmlProcessorTrim"/>
        <to uri="bean:validateResponse"/>
    </route>

</camelContext>

<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
    <constructor-arg index="0" type="org.apache.http.client.HttpClient" ref="httpClient"/>
</bean>


<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig"
      factory-method="custom">
    <property name="socketTimeout" value="${bank.webservice.timeout}" />
    <property name="connectTimeout" value="${bank.webservice.timeout}" />
</bean>

<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />

<bean id="poolingConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager">
    <property name="defaultMaxPerRoute" value="20"/>
    <property name="maxTotal" value="20"/>
</bean>



<bean id="interceptedHttpClientBuilder" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="httpClientBuilder" />
    <property name="targetMethod" value="addInterceptorFirst"> </property>
    <property name="arguments">
        <list>
            <bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender.RemoveSoapHeadersInterceptor"/>
        </list>
    </property>
</bean>


<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder"
      factory-method="create">
    <property name="defaultRequestConfig" ref="requestConfig" />
    <property name="connectionManager" ref="poolingConnectionManager"/>
</bean>



<bean id="httpClient" factory-bean="interceptedHttpClientBuilder" factory-method="build" />