9

Tôi đang sử dụng khuôn khổ JAX-WS 2.2.5 để gọi WebServices. Tôi muốn xác định trường hợp đặc biệt khi cuộc gọi không thành công vì Dịch vụ web bị lỗi hoặc không thể truy cập được.Trapped một ConnectException trong một cuộc gọi webservice JAX-WS

Trong một số cuộc gọi, tôi nhận được một 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 

Ở những nơi khác, tôi nhận được ClientTransportException (lớp có nguồn gốc từ 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 

một cách tốt để bẫy lỗi này là gì?

Tôi có nên sử dụng một cái gì đó giống như

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

hoặc là có một cách tốt hơn để làm điều này?

Trả lời

0

Trước tiên, bạn phải xác định cấp cao nhất Exception để bắt. Như bạn đã chỉ ra, ở đây là WebServiceException.

Những gì bạn có thể làm tiếp theo, việc này là chung chung hơn để tránh NullPointerException nếu getCause() trả lại null.

catch(javax.xml.ws.WebServiceException e) 
{ 
    Throwable cause = e; 
    while ((cause = cause.getCause()) != null) 
    { 
     if(cause instanceof ConnectException) 
     { 
      // Webservice is down or inaccessible 
      // TODO some stuff 
      break; 
     } 
    } 
} 
1

Có thể bạn cũng muốn điều trị UnknownHostException!

 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(); 
     } 
Các vấn đề liên quan