Ejemplo n.º 1
0
 public void run() {
   synchronized (works) {
     works.add(this);
   }
   this.setPriority(threadPriority);
   int count = 0; // 每处理1000个事件检测线程池是否需要调整
   log.info("threadpool.work running:" + this.getName());
   while (!BProcessor.this.state.isStopping() && !BProcessor.this.state.isStopped()) {
     try {
       busy = false;
       currentMessage = msgQueue.take();
       if (null == currentMessage) { // 如果数据库连接异常,返回NULL。
         Thread.sleep(100);
         continue;
       }
       if (log.isDebugEnabled())
         log.debug(
             "Process up Message:"
                 + currentMessage
                     .getRawPacketString()); // log.debug("处理上行报文:"+currentMessage.getRawPacketString());
       if (currentMessage.getMessageType() == MessageType.MSG_KILLTHREAD) break;
       else if (currentMessage.getMessageType() == MessageType.MSG_GATE) {
         MessageGate mgate = (MessageGate) currentMessage;
         if (mgate.getHead().getCommand() == MessageGate.CMD_GATE_REPLY) {
           IMessage msg = mgate.getInnerMessage();
           if (!handleMessage(msg)) { // 后续数据库保存速度不足
             Thread.sleep(500);
           }
         }
       } else if (currentMessage.getMessageType() == MessageType.MSG_ZJ
           || currentMessage.getMessageType() == MessageType.MSG_GW_10) {
         if (!handleMessage(currentMessage)) { // 后续数据库保存速度不足
           Thread.sleep(500);
         }
       } else if (currentMessage.getMessageType() == MessageType.MSG_DLMS) {
         DlmsMessage msg = (DlmsMessage) currentMessage;
         DlmsEvent evt = new DlmsEvent(msg);
         evt.setSource(msg.getSource());
         DlmsEventProcessor.getInstance().handleEvent(evt);
       }
       // 检测队列中事件个数。如果太多,增加线程。如果为0,减少线程
       count++;
       if (count > 500) {
         justThreadSize();
         count = 0;
       }
     } catch (Exception exp) {
       log.error("BP WorkThread occur error", exp); // log.error("业务处理器报文处理线程处理浙规报文出错", exp);
       continue;
     }
   }
   synchronized (works) {
     works.remove(this);
   }
   log.info("Thread pool work thread exit" + this.getName()); // 线程池的工作线程退出:
 }
Ejemplo n.º 2
0
    private boolean handleMessage(IMessage msg) {
      /**
       * 业务处理器收到通信前置机上行报文,需要预先处理, 先检查是否存在此终端档案,存在才进行业务处理,不然只保存原始记录 任务,异常,主站请求返回处理:包括解析及回写数据库 原始报文保存
       */
      MessageZj zjmsg = null;
      MessageGw gwmsg = null;
      int rtua = 0;
      boolean result = true;
      if (msg.getMessageType() == MessageType.MSG_ZJ) {
        zjmsg = (MessageZj) msg;
        rtua = zjmsg.head.rtua;
      } else if (msg.getMessageType() == MessageType.MSG_GW_10) {
        gwmsg = (MessageGw) msg;
        rtua = gwmsg.head.rtua;
      }
      BizRtu rtu = (RtuManage.getInstance().getBizRtuInCache(rtua));
      if (rtu == null) {
        // 找不到终端则刷新数据库档案
        boolean refreshTag = manageRtu.refreshBizRtu(rtua);
        if (!refreshTag) log.warn("not find rtu in db:" + HexDump.toHex(rtua));
        else rtu = (RtuManage.getInstance().getBizRtuInCache(rtua));
      }
      if (rtu != null) {
        if (zjmsg != null) {

          // 任务   异常
          if (zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_TASK
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_EXP_ALARM
              || zjmsg.head.c_func == MessageConst.GG_FUNC_READ_TASK2
              || zjmsg.head.c_func == MessageConst.GG_FUNC_Event) {

            msgQueue.setLastHandleDataTime(System.currentTimeMillis());
            counter.add();
            // 如果超过处理队列最大值则放回缓存队列,以免丢失数据
            if (!asycService.addMessage(zjmsg)) {
              msgQueue.offer(zjmsg);
              result = false;
              log.warn("asycService.addMessage failed");
            }
            if (zjmsg.head.msta != 0x00
                && zjmsg.head.c_func == MessageConst.ZJ_FUNC_EXP_ALARM) { // zj事件主动召测进入这里,与主站交互.
              hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, zjmsg);
            }
          } else if (zjmsg.head.c_func == MessageConst.GG_UPGRADE) {
            GgUpgradeProcessor.getInstance().onUpgradeReturn(zjmsg);
            return true;
          } else if (zjmsg.head.c_func == MessageConst.ZJ_FUNC_RELAY
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_CUR
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_WRITE_ROBJ
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_WRITE_OBJ
              || zjmsg.head.c_func == MessageConst.ZJ_FUNC_READ_PROG
              || zjmsg.head.c_func == MessageConst.GG_FUNC_Action
              || zjmsg.head.c_func == MessageConst.GG_FUNC_READ_TASK1
              || zjmsg.head.c_func == MessageConst.GG_FUNC_AutoRegistered
              || zjmsg.head.c_func == MessageConst.GG_Pay_token) { // 主站请求返回					
            hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, zjmsg);
          } else if (zjmsg.isLogin()) {
            saveConnectSituation(zjmsg);
          }
        } else if (gwmsg != null) {
          if (gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA1
              || gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA2) {
            if (gwmsg.head.c_prm == 1) { // 主动上送的一、二数据报文都认为是任务
              gwmsg.setTask(true); // 设置任务状态					
            } else { // 一、二类数据回复数据需要判断是否为任务漏点补招返回
              int fseq = gwmsg.getFseq();
              List<RtuCmdItem> rcis = masterDbService.getRtuComdItem(HexDump.toHex(rtua), fseq);
              for (RtuCmdItem rci : rcis) {
                if (rci.getZdzjbz() == Operator.GWLDBZ
                    || rci.getZdzjbz() == Operator.ZZRW_SJCJ) { // 国网任务漏点补招请求返回或者主站任务轮招数据采集返回							
                  gwmsg.setTask(true); // 设置任务状态
                  break;
                }
              }
            }
          } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_AUTH) {
            GwUpdateKeyHandler.getInstance()
                .processUpdateKeyMsg(asycService, manageRtu, masterDbService, gwmsg);
            return true;
          } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_RELAY_READ) {
            if (msgQueue.isDlmsRelay(gwmsg.getLogicalAddress())) {
              // 国网规约接dlms规约表中继
              if (DlmsEventProcessor.getInstance().postUpRelayMessage(gwmsg)) {
                return result;
              }
            }
          }
          // 如果是任务或异常事件则放入相应队列
          if (gwmsg.isTask() || gwmsg.getAFN() == MessageConst.GW_FUNC_GET_DATA3) {
            msgQueue.setLastHandleDataTime(System.currentTimeMillis());
            counter.add();
            // 如果超过处理队列最大值则放回缓存队列,以免丢失数据
            if (!asycService.addMessage(gwmsg)) {
              msgQueue.offer(gwmsg);
              result = false;
              log.warn("asycService.addMessage failed");
            }
            if (gwmsg.head.c_prm == MessageConst.DIR_DOWN) // 主站召测
            hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, gwmsg);
          } else if (gwmsg.getAFN() == MessageConst.GW_FUNC_HEART) {
            // 当是登陆帧的时候,存储。
            if (gwmsg.isLogin()) {
              saveConnectSituation(gwmsg);
            }
          } else
            hostCommandHandler.handleExpNormalMsg(asycService, manageRtu, masterDbService, gwmsg);
        }
      }
      return result;
    }