软件编程
位置:首页>> 软件编程>> java编程>> Java编程Webservice指定超时时间代码详解

Java编程Webservice指定超时时间代码详解

作者:mengwei  发布时间:2023-11-02 23:17:12 

标签:java,webservice,超时

WebService是一种跨编程语言和跨操作系统平台的远程调用技术

所谓远程调用,就是一台计算机a上的一个程序可以调用到另外一台计算机b上的一个对象的方法,譬如,银联提供给商场的pos刷卡系统(采用交互提问的方式来加深大家对此技术的理解)。

远程调用技术有什么用呢?商场的POS机转账调用的转账方法的代码是在银行服务器上,还是在商场的pos机上呢?什么情况下可能用到远程调用技术呢?例如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以webservice服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率,往大的概念上吹,就是所谓的SOA应用。

所谓跨编程语言和跨操作平台,就是说服务端程序采用java编写,客户端程序则可以采用其他编程语言编写,反之亦然!跨操作系统平台则是指服务端程序和客户端程序可以在不同的操作系统上运行。

除了WebService外,常见的远程调用技术还有RMI(Remotemethodinvoke)和CORBA,由于WebService的跨平台和跨编程语言特点,因此比其他两种技术应用更为广泛,但性能略低。

使用JDK对Webservice的支持进行Webservice调用时通常的操作步骤如下:


//1、创建一个javax.xml.ws.Service实例
javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdl, serviceName);
//2、通过Service实例获取对应的服务接口的代理
HelloService helloService = service.getPort(portName, HelloService.class);
//3、通过获取到的Webservice服务接口的代理调用对应的服务方法
helloService.sayHello("Elim")

在上述的步骤一在构建Service实例的同时,在Service内部会构建一个ServiceDelegate类型的对象赋给属性delegate,内部持有。然后在第二步会利用delegate创建一个服务接口的代理对象,同时还会代理BindingProvider和Closeable接口。然后在第三步真正发起接口请求时,内部会发起一个HTTP请求,发起HTTP请求时会从BindingProvider的getRequestContext()返回结果中获取超时参数,分别对应com.sun.xml.internal.ws.connection.timeout和com.sun.xml.internal.ws.request.timeout参数,前者是建立连接的超时时间,后者是获取请求响应的超时时间,单位是毫秒。如果没有指定对应的超时时间或者指定的超时时间为0都表示永不超时。所以为了指定超时时间我们可以从BindingProvider下手。比如:


public class Client {

public static void main(String[] args) throws Exception {
String targetNamespace = "http://test.elim.com/ws";
QName serviceName = new QName(targetNamespace, "helloService");
QName portName = new QName(targetNamespace, "helloService");
URL wsdl = new URL("http://localhost:8888/hello");
//内部会创建一个ServiceDelegate类型的对象赋给属性delegate
Service service = Service.create(wsdl, serviceName);
//会利用delegate创建一个服务接口的代理对象,同时还会代理BindingProvider和Closeable接口。
HelloService helloService = service.getPort(portName, HelloService.class);

BindingProvider bindingProvider = (BindingProvider) helloService;
Map<String, Object> requestContext = bindingProvider.getRequestContext();
requestContext.put("com.sun.xml.internal.ws.connection.timeout", 10 * 1000);//建立连接的超时时间为10秒
requestContext.put("com.sun.xml.internal.ws.request.timeout", 15 * 1000);//指定请求的响应超时时间为15秒

//在调用接口方法时,内部会发起一个HTTP请求,发起HTTP请求时会从BindingProvider的getRequestContext()返回结果中获取超时参数,
//分别对应com.sun.xml.internal.ws.connection.timeout和com.sun.xml.internal.ws.request.timeout参数,
//前者是建立连接的超时时间,后者是获取请求响应的超时时间,单位是毫秒。如果没有指定对应的超时时间或者指定的超时时间为0都表示永不超时。

System.out.println(helloService.sayHello("Elim"));
}

}

完整示例如下:

服务接口:


@WebService(portName="helloService", serviceName="helloService", targetNamespace="http://test.elim.com/ws")
public interface HelloService {

String sayHello(String name);

}

服务接口实现:


@WebService(portName="helloService", serviceName="helloService", targetNamespace="http://test.elim.com/ws")
public class HelloServiceImpl implements HelloService {
private Random random = new Random();
@Override
public String sayHello(String name) {
try {
TimeUnit.SECONDS.sleep(5 + random.nextint(21));
//随机睡眠5-25秒
}
catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello " + name;
}
}

服务端代码:


public class Server {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/hello", new HelloServiceImpl());
}
}

在上述的服务端代码中随机睡眠了5-25秒,而客户端指定的超时时间是15秒,所以在测试的时候你会看到有时候服务调用会超时,有时会正常响应。

来源:http://elim.iteye.com/blog/2394410

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com