Java programming Webservice specifies timeout code details

  • 2020-11-30 08:19:36
  • OfStack

WebService is a remote invocation technology across programming languages and operating system platforms

A remote call is a method by which a program on a on one computer can call an object on another computer, such as The pos card swiping system offered by UnionPay to stores (which USES interactive questions to deepen understanding of the technology).

What is the use of remote invocation technology? Is the code for the transfer method invoked on the POS machine transfer in the mall on the bank server or on the pos machine transfer in the mall? When might the remote invocation technique be used? For example, amazon, weather forecast system, Taobao.com, Xiaonei and Baidu exposed their system services in the form of webservice services, so that the third party's websites and programs could call these service functions. In this way, the market share of their system was expanded and the so-called SOA application was promoted to a larger concept.

Cross-programming languages and cross-operating platforms mean that server-side programs are written in java, client-side programs can be written in other programming languages, and vice versa! Cross-operating system platforms mean that server programs and client programs can run on different operating systems.

In addition to WebService, common remote invocation technologies are RMI (Remotemethodinvoke) and CORBA, which are more widely used than the other two technologies due to their cross-platform and cross-programming language characteristics, but with slightly lower performance.

The usual steps for making an Webservice call using JDK to support Webservice are as follows:


//1 , create, 1 a javax.xml.ws.Service The instance 
javax.xml.ws.Service service = javax.xml.ws.Service.create(wsdl, serviceName);
//2 And through Service Instance gets the proxy for the corresponding service interface 
HelloService helloService = service.getPort(portName, HelloService.class);
//3 , through the acquisition of Webservice The proxy of the service interface invokes the corresponding service method 
helloService.sayHello("Elim")

In step 1 above, while building an instance of Service, an object of type ServiceDelegate is built inside Service to assign to the attribute delegate, which is held internally. Step 2 then USES delegate to create a proxy object for the service interface, as well as the BindingProvider and Closeable interfaces. Then in step 3 really started interface request, internal will initiate a HTTP request, initiate HTTP request from BindingProvider getRequestContext after obtaining the timeout parameter () returns the corresponding com. sun. xml. internal. ws. connection. timeout and com sun. xml. internal. ws. request. timeout parameters, the former is to establish a connection timeout, The latter is the timeout in milliseconds to get the request response. If the corresponding timeout period is not specified or if the specified timeout period is 0, it will never timeout. So to specify the timeout we can start with BindingProvider. Such as:


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");
		// The inside will create 1 a ServiceDelegate An object of type to an attribute delegate
		Service service = Service.create(wsdl, serviceName);
		// Make use of delegate create 1 A proxy object for a service interface that also ACTS as a proxy BindingProvider and Closeable Interface. 
		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 the connection is 10 seconds 
		requestContext.put("com.sun.xml.internal.ws.request.timeout", 15 * 1000);// Specifies that the response timeout for the request is 15 seconds 
		
		// When an interface method is called, it is initiated internally 1 a HTTP Request, initiate HTTP The request will be received from BindingProvider the getRequestContext() Returns the timeout parameter in the result, 
		// corresponding com.sun.xml.internal.ws.connection.timeout and com.sun.xml.internal.ws.request.timeout Parameters, 
		// The former is the timeout to establish the connection, and the latter is the timeout in milliseconds to get the request response. If the corresponding timeout period is not specified or the specified timeout period is 0 Both mean that it never runs out of time. 
		
		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 sayHello(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();
	@Override
		public String sayHello(String name) {
		try {
			TimeUnit.SECONDS.sleep(5 + random.nextint(21));
			// Random sleep 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());
	}
}

In the above server-side code there was a random sleep of 5-25 seconds, while the client specified a timeout of 15 seconds, so during the test you will see that sometimes the service calls will time out and sometimes they will respond normally.

conclusion

That is the end of this article on Java programming Webservice specified timeout code explanation, I hope to help you. If there is any deficiency, please let me know. Thank you for your support!


Related articles: