Spring Web Service Client via JaxWsPortProxyFactoryBean

0. Overview.


1. Maven Dependency.

<dependency>
      <groupId>org.springframework.ws</groupId>
      <artifactId>spring-ws-core</artifactId>
      <version>2.1.3.RELEASE</version>
</dependency>
Note that the artifactId is 'spring-ws-core', rather than 'spring-ws', some out of date posts use the latter one, which would not work now.
2. Generate stubs by JAX-WS wsimport

Since 1.6, JDK provides a handy wsimport tool to generate web service classes, before configuring JaxWsPortProxyFactoryBean, we should have the classes exist first. Of course, wsimport is just a means and personal preference, not a must, we have a hold of other alternatives to generate web service proxy classes.

wsimport -s wscdemo -p com.derek.demo.wsclient.stub http://127.0.0.1:8080/BetService.asmx?wsdl

Next move: copy the newly generated classes to Eclipse, 

wsimport

3. Configure JaxWsPortProxyFactoryBean

To configure JaxWsPortProxyFactoryBean, we'd better look into the wsdl file and the generated interface carefully. In this case, the 'serviceInterface' value we should take the generated interface's qualified name, in other words, this property's value depends on how we generate the stub classes. Another 4 properties' value lie in WSDL file, or you can resort to <Spring 3 in Action> for reference.

<bean id="betService"
	class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
	<property name="serviceInterface" value="com.derek.demo.wsclient.stub.BettingServiceSOAP" />
	<property name="wsdlDocumentUrl">
		<value>${uat.vip.wsdlurl}</value>
	</property>
	<property name="namespaceUri" value="http://tempuri.org/" />
	<property name="serviceName" value="BettingService" />
	<property name="portName" value="BettingServiceSOAP" />
</bean>


4. Inject JaxWsPortProxy

5. Timeout

We are able to set timeout via JAX-WS. The following configuration works:

<bean id="betService"
	class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
	<property name="serviceInterface" value="com.derek.demo.wsclient.stub.BettingServiceSOAP" />
	<property name="wsdlDocumentUrl">
		<value>${uat.vip.wsdlurl}</value>
	</property>
	<property name="namespaceUri" value="http://tempuri.org/" />
	<property name="serviceName" value="BettingService" />
	<property name="portName" value="BettingServiceSOAP" />
	<property name="customProperties" ref="jaxwsCustomProperties" />
</bean>

<util:map id="jaxwsCustomProperties">
	<entry key="com.sun.xml.ws.request.timeout">
		<value type="java.lang.Integer">2000</value>
	</entry>
	<entry key="com.sun.xml.ws.connect.timeout">
		<value type="java.lang.Integer">2000</value>
	</entry>
</util:map>
For testing timeout, I set the value as small as 10, and call the service, encounter this exception, which proves the timeout setting works.

org.springframework.remoting.RemoteAccessException: Could not access remote service at [null]; nested exception is javax.xml.ws.WebServiceException: java.net.SocketTimeoutException: Read timed out
    at org.springframework.remoting.jaxws.JaxWsPortClientInterceptor.doInvoke(JaxWsPortClientInterceptor.java:510)
    at org.springframework.remoting.jaxws.JaxWsPortClientInterceptor.invoke(JaxWsPortClientInterceptor.java:487)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy66.keepAlive(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.ws.WebServiceException: java.net.SocketTimeoutException: Read timed out
    at com.sun.xml.ws.transport.http.client.HttpClientTransport.readResponseCodeAndMessage(HttpClientTransport.java:211)
    at com.sun.xml.ws.transport.http.client.HttpTransportPipe.createResponsePacket(HttpTransportPipe.java:274)
    at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:265)
    at com.sun.xml.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:184)
    at com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:109)



6. Concurrent JaxWsPortProxyFactoryBean

Reference: 

1. <Spring 3 in Action>. Proxying JAX-WS services on the client side.

2. Timeout Configuration. http://helponjee.blogspot.hk/2011/10/set-timeout-while-calling-webservice-in.html. (This URL may not be accessible in mainland of China.)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值