本文主要是介绍spring 整合WebService 设置超时,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前段时间在用spring 整合WebService 设置超时时有个问题一直解决不了
最后不在spring配置文件里写直接获取代理类,对代理类进行设置。
例子:
package test;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.cxf.server.HolloWrold;
public class TestMessage {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
HolloWrold a =(HolloWrold) factory.getBean("holloWrold");
Client proxy = ClientProxy.getClient(a);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(3000); // 连接超时时间 30
policy.setReceiveTimeout(2400);// 请求超时时间. 4分钟240秒
//policy.setContentType("text/xml;charset=UTF-8");
//policy.setElementType(type);
conduit.setClient(policy);
System.out.println(a.sayHi("aaa"));
}
}
事后经过百度得知错误原因:
name 的不是wsdl上面的name (不知道什么原因,官方文档说的就是 等于 WSDL中的 port name和".http-conduit"组成)
而是客户端接口的targetNamespace和name + Prot
name="{targetNamespace }name+Port.http-conduit"
例如我客户端接口: @WebService(targetNamespace = "http://server.cxf.com/", name = "HolloWrold")
name="{http://server.cxf.com/}HolloWroldPort.http-conduit"
<http-conf:conduit name="{http://server.cxf.com/}HolloWroldPort.http-conduit">
<http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000" ContentType="text/xml;charset=UTF-8"/>
</http-conf:conduit>
例子:
一、在spring的配置文件中进行设置(客户端)。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd ">
<http-conf:conduit name="{http://server.cxf.com/}HolloWroldPort.http-conduit">
<http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000" ContentType="text/xml;charset=UTF-8"/>
</http-conf:conduit>
</beans>
- 这里需要注意的有几个地方:
- 需要指定http-conf名称空间 xmlns:http-conf=http://cxf.apache.org/transports/http/configuration
- 指定模式位置: http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
- http-conf:conduit中的name属性,指定设置生效的服务,如例子中,只对服务名为{http://impl.service.product.sww.com/}ProjectService的服务生效.
- 使用下面的设置则会对所有服务生效
- <http-conf:conduit name="*.http-conduit">
- </http-conf:conduit>
还有一种方法:
就是name = " WebService 路径" 只对这个 WebService 起作用(ContentType是设置编码格式)
例子:
<http-conf:conduit name="http://localhost:8080/WSSSpring/webservice/hollo">
<http-conf:client ConnectionTimeout="3000" ReceiveTimeout="3000" ContentType="text/xml;charset=UTF-8"/>
</http-conf:conduit>
这篇关于spring 整合WebService 设置超时的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!