@Override public void accept(java.lang.String string, Socket socket) { if (TextUtil.checkUUID(string)) { mExecutor.scheduleAtFixedRate( new HeartBeatWorker(string, socket), 0, mHeartBeatInterval, TimeUnit.SECONDS); } }
@Override public void run() { // Thread heartBeat = new Thread(new HeartBeatWorker(), "PUSH_SERVER_HEARTBEAT_WORKER"); // heartBeat.setDaemon(true); // heartBeat.start(); BiConsumer<String, Socket> consumer = new SocketConsumer(); mSocketPool.forEach( consumer); // only first time iterate the socket already exist in the socketPool to add // HeartBeat thread. // after that need to call addHeartBeat() add HeartBeat manually try { while (mServiceRunning) { ServerMessage msg = mPushQueue.deQueueMsg(); if (msg == null) { // timeout return null object // currently do nothing LogUtil.logV("timeout return null object"); } else { String UUID = msg.getMsg().getUUID(); if (TextUtil.checkUUID(UUID)) { Socket socket = mSocketPool.getSocket(UUID); if (socket != null && socket.isConnected()) { OutputStream os; try { os = socket.getOutputStream(); // os.write(Message.createHeatBeatMsg(UUID)); Message send = msg.getMsg(); send.setCommand(send.getCommand() | MessageState.CMD_PUSH_MSG_CLIENT); os.write(send.getData()); os.flush(); msg.setLastPushMsg(System.currentTimeMillis()); } catch (IOException e) { LogUtil.logE("IOException", e); } } else { // when msg in PushQueue didn't have corresponding socket, no need to put msg back to // push queue, msg pool have this copy, wait for client check unread msg with service // just ignore this msg from push queue LogUtil.logI("msg in PushQueue didn't have corresponding socket"); } } } } } catch (InterruptedException e) { LogUtil.logE("InterruptedException", e); } }
@Override public void run() { if (TextUtil.checkUUID(mUuid)) { ServerMessage msg = mPool.getMsg(mUuid); long now = System.currentTimeMillis(); if (msg != null && ((now - msg.getLastHeartBeat()) >= mHeartBeatInterval)) { try { if (mSocket != null && mSocket.isConnected()) { OutputStream os = mSocket.getOutputStream(); os.write(Message.createHeatBeatMsg(mUuid, msg.haveUnreadMsg())); os.flush(); msg.setLastHeartBeat(now); } } catch (IOException e) { LogUtil.logE("IOException", e); } } } /* long lastRun = System.currentTimeMillis(); long now; while (mServiceRunning) { now = System.currentTimeMillis(); if ((now - lastRun) < (mHeartBeatInterval*1000)) { try { Thread.sleep((mHeartBeatInterval*1000) - (now - lastRun)); } catch (InterruptedException e) { LogUtil.logE("InterruptedException", e); } } else if ((now - lastRun) > (mHeartBeatInterval*1000)) { LogUtil.logI("HeartBeatWork Overload "); } lastRun = now; } */ }
public void addHeartBeat(String uuid, Socket socket) { if (TextUtil.checkUUID(uuid)) { mExecutor.scheduleAtFixedRate( new HeartBeatWorker(uuid, socket), 0, mHeartBeatInterval, TimeUnit.SECONDS); } }