//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")
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());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有