2017-10-11 76 views
1

嗨我有一個服務代理中的Web服務類,它有一個返回void的方法。我必須檢查服務是否正常運行。由於該方法返回void,我無法獲得服務的狀態。有沒有辦法通過使用或不使用ping來檢查此Web服務方法的狀態?如何Ping一個具有返回類型的Web服務方法void

下面是我的web服務方法,它有返回類型void。此Web服務方法將執行一些驗證並觸發另一個方法,以便它不會返回任何值。

@GET 
     @Path("/triggers/{name}") 
     public void triggerMethod(@PathParam("name") String triggername, @Context HttpServletRequest aHttpRequest){ 
      //code 

    } 

以下是ping功能已存在的代碼,但它會檢查響應的狀態。此代碼適用於webservices方法,該方法是returnig響應並接受APPLICATION_JSON。

private void invoketrigger(ServiceDataDTO myData){ 

      try{ 
     target.request().headers(getRequestHeaders()).accept(MediaType.APPLICATION_JSON).get(); 
      Client client = ClientBuilder.newClient(); 
        WebTarget target = client.target(myData.getServiceURI()); 
        Response response = target.request().headers(getRequestHeaders()).accept(MediaType.APPLICATION_JSON).get(); 
        if(response.getStatus() == 200){ 
         status = "green"; 
      } 
    } 




The code which I tried for my method is given below. 

private void invoketrigger(ServiceDataDTO myData){ 

     try{ 
    target.request().headers(getRequestHeaders()).accept(MediaType.APPLICATION_JSON).get(); 
     URL url = new URL(myData.getServiceURI()); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setConnectTimeout(10000); 
     connection.setReadTimeout(10000); 
     connection.setRequestMethod("GET"); 
     connection.connect(); 
     int response = connection.getResponseCode(); 

     if(response == 200){ 
      myData.setServiceStatus(ServicesDashboardConstants.STATUS_OK); 
     } 
     }catch(Exception e){ 
      System.out.println(e); 
     } 
    } 
+0

response.ok應返回 –

回答

1

我完全相信,如果不返回任何內容,您無法做到這一點。客戶端將不知道請求是否完成,除非返回的服務是OK,否則客戶端將等待響應。 所以你必須讓你的方法返回一個Response類型的對象。並且您不需要添加任何內容以響應僅告訴返回200的方法

response.ok (200) ; // this will tell the method what is the status code response 
相關問題