Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trapping a ConnectException in a JAX-WS webservice call

I am using JAX-WS 2.2.5 framework for calling WebServices. I want to identify the special case when the call fails because the Web Service is down or not accessible.

In some calls, i get a WebServiceException.

    catch(javax.xml.ws.WebServiceException e)
    {
        if(e.getCause() instanceof IOException)
            if(e.getCause().getCause() instanceof ConnectException)
                 // Will reach here because the Web Service was down or not accessible

In other places, I get ClientTransportException (class derived from WebServiceException)

    catch(com.sun.xml.ws.client.ClientTransportException ce)
    {

         if(ce.getCause() instanceof ConnectException)
              // Will reach here because the Web Service was down or not accessible

What's a good way to trap this error?

Should I use something like

    catch(javax.xml.ws.WebServiceException e)
    {
        if((e.getCause() instanceof ConnectException) || (e.getCause().getCause() instanceof ConnectException))
         {
                   // Webservice is down or inaccessible

or is there a better way of doing this?

like image 796
user93353 Avatar asked Dec 17 '14 06:12

user93353


1 Answers

Maybe you'll want to treat UnknownHostException also!

        Throwable cause = e.getCause();

        while (cause != null)
        {
            if (cause instanceof UnknownHostException)
            {
                //TODO some thing
                break;
            }
            else if (cause instanceof ConnectException)
            {
                //TODO some thing
                break;
            }

            cause = cause.getCause();
        }
like image 195
Luiz valença Avatar answered Nov 01 '22 02:11

Luiz valença