コード例 #1
0
  private void loadInitData() throws Exception {
    String url = baseUrl + MONITOR_APPLICATION_INIT_URL;
    File file = new File(NOTIFICATION_INFO_FILENAME);
    if (!file.exists()) {
      String responseStr =
          NotificationHttpSupport.post(
              url,
              new HashMap<String, String>() {
                {
                  put("ip", notificationProperties.getProperty("application.ip"));
                  put("port", notificationProperties.getProperty("application.port"));
                  put("appName", notificationProperties.getProperty("application.name"));
                }
              });

      if (NotificationResponseType.Exception.name().equalsIgnoreCase(responseStr)) {
        logger.error("Init data from monitor server url [" + url + "] exception.");
      } else if (NotificationResponseType.NotExist.name().equalsIgnoreCase(responseStr)) {
        return;
      } else {
        JSONObject jsonObject = JSON.parseObject(responseStr);
        applicationId = jsonObject.getString("applicationId");
        if (applicationId != null) {
          String urls = jsonObject.getString("urls");
          if (urls != null && !urls.equals("")) {
            NotificationServiceFactory.buildNotificationService()
                .initData(
                    jsonObject.getString("urls"),
                    notificationProperties.getProperty("application.name"));
          }

          file.createNewFile();
          OutputStream outputStream = null;
          try {
            outputStream = new FileOutputStream(file);
            outputStream.write(("agent.enable=true\nagent.id=" + applicationId).getBytes());
            outputStream.flush();
          } catch (Exception e) {
            logger.error("write file " + NOTIFICATION_INFO_FILENAME + "exception.", e);
          } finally {
            if (null != outputStream) {
              outputStream.close();
            }
          }
        }
      }
    } else {
      reloadApplicationId(file, url);
    }
  }
コード例 #2
0
  private void reloadApplicationId(File file, String url) {
    InputStream inputStream = null;
    try {
      inputStream = new FileInputStream(file);
      Properties agentInfoProperties = new Properties();
      agentInfoProperties.load(inputStream);
      applicationId = agentInfoProperties.get("agent.id").toString();
      String responseStr =
          NotificationHttpSupport.post(
              url,
              new HashMap<String, String>() {
                {
                  put("applicationId", applicationId);
                }
              });

      if (NotificationResponseType.Exception.name().equalsIgnoreCase(responseStr)) {
        logger.error("Init data from monitor server url [" + url + "] exception.");
      } else if (NotificationResponseType.NotExist.name().equalsIgnoreCase(responseStr)) {
        removeApplication();
      } else {
        JSONObject jsonObject = JSON.parseObject(responseStr);
        String urls = jsonObject.getString("urls");
        if (urls != null && !urls.equals("")) {
          NotificationServiceFactory.buildNotificationService()
              .initData(
                  jsonObject.getString("urls"),
                  notificationProperties.getProperty("application.name"));
        }
      }
    } catch (Exception e) {
      logger.error("read file " + NOTIFICATION_INFO_FILENAME + "exception.", e);
    } finally {
      if (null != inputStream) {
        try {
          inputStream.close();
        } catch (Exception e) {
          logger.error("input stream close exception.", e);
        }
      }
    }
  }