Monday, May 16, 2011

Setting timeout for web service invocations using SII client generated using Weblogic 8.1 SP6 ANT task

Even though the 11g version of weblogic has been released, most of the big organizations continue to use BEA Weblogic 8.1 version (there are number of reasons and I won't get into details). Since we have been invoking some very busy web services and wanted to utilize the timeout mechanism to throw an exception on the client side so that the user may be requested to try at a later time rather than waiting for ever. Over a number of blogs I saw people suggesting the use of weblogic.webservice.rpc.timeoutsecs property and provide the timeout values in seconds. It didn't seem to work for me and so was the experience of many users using the webservice client generated by weblogic ant tasks and using the weblogic.jar and webservice.jar in classpath. After a number of google searches, I found out a person who had succesfully used the above mentioned property to get the timeout functionality working. The secret to this person's solution was, he had used a DII (i.e. used a self written client instead of the generated client). The sample DII client for a Calculator service with an add webmethod is as below:
      String NS_XSD = "http://www.w3.org/2001/XMLSchema";
      String targetNamespace = "http://www.bea.com/examples/Calculator";

      System.setProperty("javax.xml.rpc.ServiceFactory", "weblogic.webservice.core.rpc.ServiceFactoryImpl");
      System.setProperty("weblogic.webservice.verbose", "false");
      System.setProperty("weblogic.webservice.UseWebLogicURLStreamHandler", "true");

      ServiceFactory factory = ServiceFactory.newInstance();
      QName serviceName = new QName(targetNamespace, "CalculatorService");
      QName portName = new QName(targetNamespace, "CalculatorServicePort");
      QName operationName = new QName(targetNamespace, "add");
      Service service = factory.createService(serviceName);

      Call call = service.createCall();
      call.setOperationName(operationName);
      call.setPortTypeName(portName);
      call.setProperty(Call.OPERATION_STYLE_PROPERTY, "rpc");

      call.addParameter("intVal", new QName(NS_XSD, "int"), Integer.class, ParameterMode.IN);
      call.addParameter("intVal0", new QName(NS_XSD, "int"), Integer.class, ParameterMode.IN);
      call.setReturnType(new QName(NS_XSD, "int"), Integer.class);
      call.setTargetEndpointAddress("http://localhost:7001/CalculatorService/CalculatorService");
      ((weblogic.webservice.core.rpc.CallImpl) call).setProperty("weblogic.webservice.rpc.timeoutsecs", "5");
      System.out.println("Invoked DII :");
      System.out.println("Result : " + call.invoke(new Object[] { new Integer(a), new Integer(b) }));
Now comes the second problem, all the existing client code for a huge number of webservices are SII codes (generated by the ANT task in webservices.jar). Instead of converting all the client codes to DII, I had to find the solution using SII. Since the code is not provided by BEA, I decompiled the jar to understand what's going on under the hood and found something strange, the weblogic.webservice.rpc.timeoutsecs set as a system property was fetched inside the jared code (in webservices.jar) but was only used in the logic that handled https invocations (may be missed out looking at the right place or it was never there). In order to confirm my findings, I exposed the service over https and modified the client code with some additional properties (related to https invocations and nothing to do with web service in particular) and VOILA!!!! It worked :)

Inference: To use the timeout property for rpc calls when using client generated SII, the webservice should be exposed in https (can't change the code in the jars used by the client code to fix it as the code is not open source). Or write a DII (example given above). For SII invocation over https you need to use the below mentioned properties:
       
System.setProperty("weblogic.security.SSL.trustedCAKeyStore", "C:/bea/weblogic81/server/lib/DemoIdentity.jks");
System.setProperty("weblogic.webservice.client.ssl.strictcertchecking", "false");
System.setProperty("weblogic.webservice.rpc.timeoutsecs", "10");
For invocations over https you shall need jsafeFIPS.jar & webserviceclient+ssl.jar in classpath in addition to webservices.jar & weblogic.jar (you can find all of those in the weblogic's lib folder). The invocation may also ask for license files (don't know why), then even put the license.bea & license_scale_limited.bea in the classpath.

For Weblogic 9 versions, the property to be set has changed. More details can be found at http://download.oracle.com/docs/cd/E13222_01/wls/docs92/webserv/client.html. Those properties are:
((Stub)service_action)._setProperty("weblogic.wsee.transport.read.timeout", 5000); //values are in millis
((Stub)service_action)._setProperty("weblogic.wsee.transport.connection.timeout", 5000); //values are in millis
Similiar properties for Weblogic 10 can be found at http://docs.oracle.com/cd/E13222_01/wls/docs100/webserv/client.html I hope you found this post helpful.