Ejemplo n.º 1
0
  /** 线程启动方法 */
  public void run() {
    boolean iskeep = true;
    while (iskeep) {
      try {
        ObjectInputStream ois = new ObjectInputStream(connection.getSocket().getInputStream());
        TransactionMessage tm = (TransactionMessage) ois.readObject();
        // System.out.println("客户端="+new
        // String(tm.getData(),4,tm.getData().length-4));
        int tId = tm.getId();
        byte[] data = tm.getData();
        if (tId == SocketConfig.HB) {
          // 表示得到的事心跳包

          // 写入日志
          String logStr = " 客户端(主机)接收到服务端(从机)发送过来的心跳包";
          ClientStart.printLog(logStr);
        } else {

          if (!tm.isStatus()) {

            int messageId = SocketConfig.getB_ID(data);
            // 写入日志
            String logStr =
                " Server-" + connection.getIp() + " 分发事务ID" + tId + " 消息ID" + messageId + "的数据";
            ClientStart.printLog(logStr);

            System.out.println(logStr);
            // 表示拿到了真实的数据,需要调用外部接口
            STProcessMgr.stProcessHmuSvr(data);
          }
        }

      } catch (Exception e) {
        System.out.println("get server msg......" + e.getLocalizedMessage());
        iskeep = false;
        // ClientStart.socketFlag = false;
        connection.setConFlag(false);
      }
    }
  }
Ejemplo n.º 2
0
  public static void write(List<Connection> conLst, TransactionMessage message) throws Exception {
    // 向多server发送数据包
    boolean isSended = false; //
    int sendNum = 0;
    int id = message.getId();
    for (Connection con : conLst) {

      if (!con.isConFlag()) continue;

      try {
        send(con.getSocket(), message);
        String logStr = "";
        // 发心跳包
        if (id == SocketConfig.HB) {
          // 写入日志
          logStr = " IP为" + con.getSocket().toString() + "的向从机发送心跳包数据";
        } else { // 发业务
          int messageId = SocketConfig.getB_ID(message.getData());

          // 条件中的5类业务(hmuserver向webserver发送数据)只向一个server发送 add by yuht
          // 2013.11.05
          if ((messageId == MsgConst.Up_HMU_SWVERSION
              || messageId == MsgConst.UP_PKG_DOMAIN
              || messageId == MsgConst.UP_UPG_RECORD
              || messageId == MsgConst.INSERT_UPG_RECORD
              || messageId == MsgConst.NAGIOS)) isSended = true;
          logStr = "To-" + con.getIp() + " 发送事务ID:" + id + " 消息ID:" + messageId;
        }
        ClientStart.printLog(logStr);
        sendNum++;
      } catch (Exception e) {
        // 如果出现异常则表示此次Socket通信中断(相当于服务端挂掉),需要待重连后方能使用
        // 因此需要从事务列表中异常心跳包
        // e.printStackTrace();
        // ClientStart.socketFlag = false;
        System.out.println("send to " + con.getIp() + " exception!");
        con.setConFlag(false);
        // throw e;
      }

      // 发送成功即停止发送
      if (isSended) break;
    }

    // 如果sever全部未发送成功计入队列 下次发送
    if (sendNum == 0 && id != SocketConfig.HB && !message.isReload())
      ClientTransactionAccepted.setTransactionAccepted(message.getData());
  }