您的位置:首页 > 编程语言 > Java开发

Spring Web Service Client via JaxWsPortProxyFactoryBean

2013-05-31 07:37 2291 查看
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[/code] Next move: copy the newly generated classes to Eclipse,



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.)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: