Code example for ScheduledExecutorService

Methods: scheduleWithFixedDelay, submit

0
	private	long	lastHeartbeatToEsbTime = 0;
	private	long	esbHeartbeatTimeout = 10 * 1000;
	private	int		reHeartbeatCount = 0;
	 
	private void doSendPending() { 
		exec.submit(new Runnable() {
 
			public void run() { 
				sendPending(); 
			}} ); 
    } 
     
    private	boolean isESBConnectionValid() { 
    	return	( (null != session) && (CONN_STATE_ONLINE == esbConnectionState) );
    } 
     
    private	void sendPending() { 
    	if ( isESBConnectionValid() ) { 
			try { 
				Object bean = pendings.poll(fetchSignalTimeout, TimeUnit.MILLISECONDS);
	    		if ( null != bean ) {
	    			session.write(bean);
	    		} 
			} catch (InterruptedException e) {
	    		logger.error("sendPending", e);
			} 
	    	finally { 
	    		doSendPending(); 
	    	} 
    	} 
    	else { 
    		//	esb not connected or not registered  
    		exec.schedule(new Runnable(){
 
				public void run() { 
					sendPending(); 
				}}, fetchSignalTimeout, TimeUnit.MILLISECONDS);
    	} 
    } 
     
    private	boolean	isHeartbeatToEsbTimeout() { 
    	long duration = System.currentTimeMillis() - lastHeartbeatToEsbTime;
    	return	( duration >= esbHeartbeatTimeout );
    } 
 
    private	boolean	isHeartbeatToEsbExceedInterval() { 
    	long duration = System.currentTimeMillis() - lastHeartbeatToEsbTime;
    	return	( duration >= esbHeartbeatInterval );
    } 
     
    private	void	sendHeartbeatToEsb() { 
    	lastHeartbeatToEsbTime = System.currentTimeMillis();
		HeartbeatToEsbReq	req = new HeartbeatToEsbReq();
		 
		req.setDstESBAddr(0xE000);
		req.setSeqNum(0);
  
		session.write(req);
    } 
     
    private	void	doProcessHeartbeat() { 
    	if ( HB_STATE_IDLE == this.esbHeartbeatState ) {
    		if ( isHeartbeatToEsbExceedInterval() ) { 
	    		esbHeartbeatState = HB_STATE_SENDED;
	    		sendHeartbeatToEsb(); 
    		} 
    	} 
    	else if ( HB_STATE_SENDED == this.esbHeartbeatState ) {
    		if ( isHeartbeatToEsbTimeout() ) { 
    			if ( reHeartbeatCount < 1 ) {
	    			reHeartbeatCount++;
	        		sendHeartbeatToEsb(); 
    			} 
    			else { 
    				//	heartbeat timeout for twice, disconnect session					 
    				this.session.close(true);
    			} 
    		} 
    	} 
    	else if ( HB_STATE_RECVD == this.esbHeartbeatState ) {
    		reHeartbeatCount = 0;
    		esbHeartbeatState = HB_STATE_IDLE;
    	} 
    	else { 
    		throw new RuntimeException("Internal Error: Unknown ESB Heartbeat State [" 
    				+ esbHeartbeatState + "]");
    	} 
    } 
     
    private	boolean	isRegisterToEsbTimeout() { 
    	long temp = lastRegisterToEsbRespTime > lastRegisterToEsbTime ? lastRegisterToEsbRespTime : System.currentTimeMillis();
    	long duration =  temp - lastRegisterToEsbTime;
    	return	( duration >= esbRespTimeout );
    } 
     
    private	void	sendRegisterToEsb() { 
    	lastRegisterToEsbTime = System.currentTimeMillis();
		RegisterToEsbReq	req = new RegisterToEsbReq();
		 
		req.setDstESBAddr(0xE000);
		req.setSeqNum(0);
		 
		//	鐢变簬鐜扮綉鏈変簺妯″潡涓嶆敮鎸佸彂閫佸績璺筹紝涓轰簡鍖哄垎锛屽鏋滄ā鍧楁敮鎸佸績璺筹紝 
		//	鍦ㄥ彂閫佺涓�釜娉ㄥ唽娑堟伅鏃讹紝璇峰皢ESB娑堟伅澶翠腑鐨凢LAG璁剧疆涓�銆� 
		req.setFlags(1);
  
		req.setModuleId(this.moduleId);
		req.setInstanceId(this.instanceId);
		 
		session.write(req);
    } 
     
    private	void	doCheckESB() { 
    	if ( CONN_STATE_OFFLINE == esbConnectionState ) {
    		sendRegisterToEsb(); 
    		esbConnectionState = CONN_STATE_REGISTERING;
    	} 
    	else if ( CONN_STATE_REGISTERING == esbConnectionState ) {
    		//	check registering timeout 
    		if ( isRegisterToEsbTimeout() && CONN_STATE_REGISTERING == esbConnectionState) {
    			reRegisterCount++;
    			//	register to esb timeout, try re-register 
        		sendRegisterToEsb(); 
    		} 
    	} 
    	else if ( CONN_STATE_ONLINE == esbConnectionState ) {
    		//	check weather to send heartbeat 
    		doProcessHeartbeat(); 
    	} 
    	else { 
    		throw new RuntimeException("Internal Error: Unknown ESB Connection State [" 
    				+ esbConnectionState + "]");
    	} 
    } 
     
	public void start() { 
    	exec.scheduleWithFixedDelay(new Runnable(){
 
			public void run() { 
				doCheckESB(); 
			}},  
			0, checkESBDelay, TimeUnit.MILLISECONDS);