Java Webservice specifies timeout

Java Webservice specifies timeout

The usual operation steps when using JDK's support for Webservice 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. Obtain the corresponding service interface through the Service instance Proxy 
HelloService helloService = service . getPort(portName, HelloService . class);
 // 3. Call the corresponding service method 
helloService . sayHello( " Elim " ) through the proxy of the obtained Webservice service interface

In the above step 1, when the Service instance is constructed, an object of type ServiceDelegate will be constructed inside the Service and assigned to the attribute delegate, which is held internally. Then in the second step, the delegate object will be used to create a proxy object of the service interface, and it 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 an HTTP request is initiated, the timeout parameter will be obtained from the result returned by BindingProvider's getRequestContext(), corresponding to com.sun.xml.internal.ws.connection. timeout and com.sun.xml.internal.ws.request.timeout parameters, the former is the timeout for establishing a connection, and the latter is the timeout for obtaining the request response, in milliseconds. If the corresponding timeout is not specified or the specified timeout is 0, it means never timeout. So in order to specify the timeout period we can start with BindingProvider. for example:

public  class  Customer {

	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 " );
		 // An object of type ServiceDelegate will be created internally and assigned to the attribute delegate 
		Service service =  Service . create(wsdl, serviceName);
		 // A proxy object of the service interface will be created using delegate , and also proxy 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 of the request as 15 seconds    
		
		// When calling the interface method, an HTTP request will be initiated internally. When an HTTP request is initiated, the timeout parameter will be obtained from the result returned by BindingProvider's getRequestContext(), 
		// corresponding to com.sun.xml.internal.ws.connection.timeout respectively And the com.sun.xml.internal.ws.request.timeout parameter, 
		// the former is the timeout for establishing a connection, and the latter is the timeout for obtaining the request response, in milliseconds. If the corresponding timeout is not specified or the specified timeout is 0, it means never timeout.
		
		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 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());
	}
	
}

In the above server-side code, it randomly sleeps for 5-25 seconds, and the timeout specified by the client is 15 seconds, so when testing, you will see that sometimes the service call will timeout, and sometimes it will respond normally.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326841069&siteId=291194637