Example #1
0
 @SuppressWarnings("unchecked")
 public static void publish(IMessage msg) {
   Config config = Config.getInstance();
   try {
     String data = JsonUtil.serialize(msg, Object.class);
     HttpUtil http = new HttpUtil(config.getHttpTimeOut());
     http.setMethod(HttpMethod.POST);
     http.addProperties("Accept", "application/json");
     data = URLEncoder.encode(data, charset);
     String channel = URLEncoder.encode(config.getRPChannel(), charset);
     http.sendRequest(
         config.getOperateInterface() + "/notify/publish/",
         String.format("channel=%s&&notify=%s", channel, data));
     String result = http.getResponse();
     Object object = JsonUtil.deserialize(result, Object.class);
     if (object == null) logger.error("解析publish结果出错");
     else {
       Map<String, Object> map = (Map<String, Object>) object;
       String returncode = (String) map.get("returnCode");
       if (Success.equals(returncode)) logger.info("publish消息成功,消息内容:" + data);
       else {
         logger.error(String.format("publish消息失败,调用接口返回结果:%s publish消息内容:%s", result, data));
       }
     }
   } catch (Exception e) {
     logger.error("publish消息出错", e);
   }
 }
  /**
   * @param ip
   * @param port
   * @param sessiontimeout session超时时间
   */
  private ZooKeeperWrapper() {
    Config config = Config.getInstance();
    this.RPchildnode = config.getLocalhost() + ":" + config.getPort();
    this.ip = config.getZooKeeperAddr();
    this.port = config.getZooKeeperPort();
    logger = new SysLogger(ZooKeeperWrapper.class.getName());

    client =
        CuratorFrameworkFactory.builder()
            .connectString(this.ip + ":" + String.valueOf(this.port)) // 连接字符串
            .retryPolicy(new RetryNTimes(3, 1000)) // 重连策略
            .sessionTimeoutMs(this.sessiontimeout) // session过期时间(ms)
            .build();
  }