From October 2015

Improve your WEBServices Consumer Apps Making WS Invocations in Parallel


If your application is calling several WS and there is no need to do it in serial, you can implement a multithreaded approach:

calling ws in parallel

Example:

Class1Service class1Service = new Class1Service();

        x = class1Service.getClass1Port();

        setWSTimeout(x); // esto lo veremos en la siguiente seccion

        class Task implements Callable<Object> {

             private Object result;

             public Object call() {

                  // compute result

                  try{

                      x.getCosa();  

                  }

                  catch (Exception e){

                      e.printStackTrace(System.out);

                  }

                  return result;

             }

        }

        Task c1 = new Task();

        Task c2 = new Task();

        ExecutorService exec = Executors.newFixedThreadPool(2);

        Future<Object> f1 = exec.submit(c1);

        Future<Object> f2 = exec.submit(c2);

        try{

            f1.get();

            f2.get();

        }

        catch(Exception e){

            e.printStackTrace(System.out);

        }

Enjoy 😉

Avoid hogging & stuck threads in WLS when you call Web Services


If your app server has lots of threads waiting for response from a ws endpoint that doesn’t run as well as you would like you should start thinking in some kind of timeout control implementation.

If your are using OSB this configuration can be done in the PS settings, but if your integration in point to point tell your developers/partners to implement timeout this way:

Class1Service class1Service = new Class1Service();

        x = class1Service.getClass1Port();

        setWSTimeout(x, 30000); // esperamos 30 segundos

       x.getCosa(); // si mi servicio no responde en 30 segundos habrá una excepción que poder atrapar

 

    public void setWSTimeout(Object o, int timeout){

        Map<String, Object> context = ((BindingProvider)o).getRequestContext();

        //Set timeout params

        context.put(“com.sun.xml.internal.ws.connect.timeout”, CONST_CONNECT_TIMEOUT);

        context.put(“com.sun.xml.internal.ws.request.timeout”, timeout);

        context.put(“com.sun.xml.ws.request.timeout”, timeout);

        context.put(“com.sun.xml.ws.connect.timeout”, CONST_CONNECT_TIMEOUT);

    }

When timeout is reached you can catch the exception avoiding stucks:

javax.xml.ws.WebServiceException: java.net.SocketTimeoutException: Read time out after 30000 millis

          at com.sun.xml.ws.transport.http.client.HttpClientTransport.readResponseCodeAndMessage(HttpClientTransport.java:210)

          at com.sun.xml.ws.transport.http.client.HttpTransportPipe.createResponsePacket(HttpTransportPipe.java:241)

          at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:232)

          at com.sun.xml.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:145)

          at com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:110)

          at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:1136)

          at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:1050)

          at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:1019)

          at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:877)

          at com.sun.xml.ws.client.Stub.process(Stub.java:463)

          at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:191)

          at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)

          at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:92)

          at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:161)

          at com.sun.proxy.$Proxy200.getCosa(Unknown Source)

Enjoy 😉