Source:
public void run() { while(true) { try { String path = "api/v1/namespaces"; URL url = new URL(NamespaceHandler.KUBERNETES_MASTER_URL + "/" + path + "?watch=true"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Authorization", "Bearer " + token); connection.setConnectTimeout(5000); connection.setReadTimeout(0); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == 200) { JsonFactory jsonFactory = new JsonFactory(); jsonFactory.setCodec(new ObjectMapper()); InputStream inputStream = connection.getInputStream(); JsonParser jsonParser = jsonFactory.createParser(inputStream); JsonNode jsonNode = jsonParser.readValueAsTree(); while (jsonNode != null) { JsonNode metaData = jsonNode.get("object").get("metadata"); String projectName = metaData.get("name").asText(); String projectID = metaData.get("uid").asText(); String action = jsonNode.get("type").asText(); if (action.equals("ADDED")) { log.debug("Project " + projectName + "[" + projectID + "] has been added."); namespaces.put(projectName, projectID); } else if (action.equals("DELETED")) { log.debug("Project " + projectName + "[" + projectID + "] has been removed."); namespaces.remove(projectName, projectID); } jsonNode = jsonParser.readValueAsTree(); } jsonParser.close(); inputStream.close(); namespaces.clear(); } else { log.error("Error getting metadata. Response code " + responseCode); try { Thread.sleep(1000); } catch (InterruptedException e) { log.error(e); } } } catch (IOException e) { log.error(e); try { Thread.sleep(1000); } catch (InterruptedException ie) { log.error(ie); } } } }

Target:
public void run() { while(run && !Thread.currentThread().isInterrupted()) { try { String path = "api/v1/namespaces"; URL url = new URL(NamespaceHandler.KUBERNETES_MASTER_URL + "/" + path + "?watch=true"); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Authorization", "Bearer " + token); connection.setConnectTimeout(5000); connection.setReadTimeout(0); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == 200) { JsonFactory jsonFactory = new JsonFactory(); jsonFactory.setCodec(new ObjectMapper()); InputStream inputStream = connection.getInputStream(); JsonParser jsonParser = jsonFactory.createParser(inputStream); JsonNode jsonNode = jsonParser.readValueAsTree(); while (jsonNode != null) { JsonNode metaData = jsonNode.get("object").get("metadata"); String projectName = metaData.get("name").asText(); String projectID = metaData.get("uid").asText(); String action = jsonNode.get("type").asText(); if (action.equals("ADDED")) { log.debugf("Project %s [%s] has been added.", projectName, projectID); namespaces.put(projectName, projectID); } else if (action.equals("DELETED")) { log.debugf("Project %s [%s] has been removed.", projectName, projectID); namespaces.remove(projectName, projectID); } jsonNode = jsonParser.readValueAsTree(); } jsonParser.close(); inputStream.close(); namespaces.clear(); } else { log.warnf("Error getting metadata. Response code %s", responseCode); try { Thread.sleep(1000); } catch (InterruptedException e) { log.error(e); stop(); } } } catch (Exception e) { log.error(e); try { Thread.sleep(1000); } catch (InterruptedException ie) { log.error(ie); stop(); } } } }