WebService is a remote call technology that crosses programming languages and crosses operating system platforms.
The so-called remote call is a method in which a program on a computer a can call an object on another computer b. For example, the Pos card swiping system provided by UnionPay to the mall (using interactive questions to deepen everyone's understanding of this technology).
What is the use of remote call technology? Is the code of the transfer method called for POS machine in the mall on the bank server or on the Pos machine in the mall? Under what circumstances can remote calling technology be used? For example, Amazon, weather forecasting system, Taobao, campus network, Baidu, etc. expose their system services in the form of webservice services, allowing third-party websites and programs to call these service functions, which expands the market share of their own system and blows to the larger concept, which is the so-called SOA application.
The so-called cross-programming language and cross-operation platform means that the server program is written in Java, while the client program can be written in other programming languages, and vice versa! A cross-operating system platform means that server programs and client programs can run on different operating systems.
In addition to WebService, common remote calling technologies include RMI (Remotemethodinvoke) and CORBA. Due to the cross-platform and cross-programming language characteristics of WebService, it is more widely used than the other two technologies, but has a slightly lower performance.
The usual steps when using JDK to make Webservice calls are as follows:
//1. Create a javax.xml.ws.Service instance javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdl, serviceName);//2. Get the proxy for the corresponding service interface through the Service instance HelloService helloService = service.getPort(portName, HelloService.class);//3. Call the corresponding service method helloService.sayHello("Elim")In the above step 1, while building a Service instance, an object of Service Delegate type will be built inside the Service to assign the attribute delegate to the internal hold. Then in the second step, delegate will be used to create a proxy object for the service interface, and will also proxy the BindingProvider and Closeable interfaces. Then, when the interface request is actually initiated in the third step, an HTTP request will be initiated internally. When the HTTP request is initiated, the timeout parameters will be obtained from the result of the getRequestContext() return of BindingProvider, corresponding to the com.sun.xml.internal.ws.connection.timeout and com.sun.xml.internal.ws.request.timeout parameters respectively. The former is the timeout time for establishing the connection, and the latter is the timeout time for obtaining the request response, which is in milliseconds. If the corresponding timeout is not specified or the specified timeout is 0, it means that the timeout will never expire. So in order to specify the timeout time, we can start with BindingProvider. for example:
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");//A ServiceDelegate type object will be created internally to assign it to the attribute delegateService service = Service.create(wsdl, serviceName);//It will use delegate to create a proxy object for the service interface, and it will also proxy the BindingProvider and Closeable interfaces. 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);//The timeout for establishing a connection is 10 seconds requestContext.put("com.sun.xml.internal.ws.request.timeout", 15 * 1000);//Specify the response timeout for the request to be 15 seconds//When calling the interface method, an HTTP request will be initiated internally. When an HTTP request is initiated, the timeout parameters will be obtained from the result of the return of the BindingProvider's getRequestContext(), // Corresponding to the com.sun.xml.internal.ws.connection.timeout and com.sun.xml.internal.ws.request.timeout parameters respectively. //The former is the timeout for establishing a connection, and the latter is the timeout for obtaining the request response, and the unit is milliseconds. If the corresponding timeout is not specified or the specified timeout is 0, it means that the timeout will never expire. System.out.println(helloService.sayHello("Elim"));}} The complete example is as follows:
Service interface:
@WebService(portName="helloService", serviceName="helloService", targetNamespace="http://test.elim.com/ws")public interface HelloService {String saysHello(String name);}Service interface implementation:
@WebService(portName="helloService", serviceName="helloService", targetNamespace="http://test.elim.com/ws")public class HelloServiceImpl implements HelloService {private Random random = new Random();@Overridepublic String saysHello(String name) {try {TimeUnit.SECONDS.sleep(5 + random.nextint(21));//Random sleep for 5-25 seconds}catch (InterruptedException e) {e.printStackTrace();}return "Hello " + name;}}Server code:
public class Server {public static void main(String[] args) {Endpoint.publish("http://localhost:8888/hello", new HelloServiceImpl());}}The server code mentioned above randomly sleeps for 5-25 seconds, while the timeout specified by the client is 15 seconds, so during the test, you will see that sometimes the service call will time out and sometimes it will respond normally.
Summarize
The above is all the detailed explanation of the Java programming Webservice timeout code, I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!