Monitor mission design ideas

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/change_on/article/details/86179151

Here is not the drawing, written expression can be.
If there are several business subsystems together to complete a task, when a large number of tasks comes, how to ensure that each task can finish? This requires us to monitor mission, my thinking is this: create a new monitoring project, timed to traverse the task table in the database to see if there are no new task, anything taken out, join a queue to be executed and calculates the timeout, and then open a thread to judge whether or not to finish the task, if the timeout, retransmission mechanism is triggered, if the finish, and then close the thread.

Here are three questions to pay special attention to
1. monitoring thread must not cause intrusion into the existing business, must be an additional
2 monitor to make sure each task, a thread can be unified monitoring of all, you can open a thread for each task ( I choose the latter here)
Calculations 3. timeout (due to the multi-task, will heap in the queue, so the time-out time for each task is different)

This embodiment of the monitoring step

1. In the service, each complete a sub-service, put the next part of the path and data storage, here kafka do messaging, and therefore hold the queue, and status data can be

Here Insert Picture Description

2. In the monitoring project, calculated timeout

Timeout = task execution time budget * number of queues (FIFO queue must use strategy)

Timeout class

public class CmdRecordMonitor {
	//起始时间
	private Long startTime;
	//终止时间
	private Long overTime;
	//重发次数限制
	private int refairTime;
    ...
   setter and getter

Timeout Queue (a LinkedBlockingQueue)

//监听任务是否超时
for(String cmdId : cmdIds) {
	if(!CmdQueue.PRE_CMD_QUEUE.contains(cmdId)){
		CmdQueue.PRE_CMD_QUEUE.add(cmdId);
		...
		//超时时间
		Integer overtime = jsArray.size() * CmdQueue.PRE_CMD_QUEUE.size() * 2000;
		Long overtime2 = overtime.longValue();
		cmdRecordMonitor.setOverTime(overtime2);
		//保存监控任务
		CmdMonitor cmdMonitor = new CmdMonitor();
		CmdMonitor cmdMonitor2 = monitorService.getCmdMonitor(cmdId);
		if(cmdMonitor2 == null) {
			cmdMonitor.setCmdId(cmdInfo.getCmdId());
       		cmdMonitor.setCmdType(cmdInfo.getCmdType());
       		cmdMonitor.setCreateTime(new Date());
       		cmdMonitor.setStatus(1);
       		monitorService.saveCmdMonitor(cmdMonitor);
		}
		else{
			cmdMonitor = cmdMonitor2;
		}
		//启动线程监控该程序
		...
	}	
}

3. To determine the timeout and retransmission, just to give a general idea, specific business code I weed out the

@Override
	public void run() {
		logger.info("--------------------center-monitor:启动任务监控线程--------------------");
		boolean flg = false;
		//让线程一直循环,除非程序崩溃
		while(!flg){
			try{
		    	Long nowtime = System.currentTimeMillis();
		    	Long last = cmdRecordMonitor.getStartTime();
		    	//先判断是否完成了
		    	Date cmdTime = ...
		    	if(cmdTime != null) {
		    		logger.info("--------------------center-monitor:任务按时完成,关闭监控线程--------------------");
		    		CmdQueue.PRE_CMD_QUEUE.remove(cmdId);
		    		flg=true;
		    		return;
		    	}
		    	//超时,从队列里面踢出
		    	if(nowtime - last > cmdRecordMonitor.getOverTime() && status < 5) {
		    		logger.info("----------------center-monitor:超时了---------------");
		    		//记录异常,通知异常处理
		    		//判断状态在哪一步
		    		if(cmdInfo.getStatus() == 1) {
		    			//清除数据
		    			//重新发任务
		    		}
		    		else if(cmdInfo.getStatus() == 2) {
		    		}
                    else if(cmdInfo.getStatus() == 3) {   
		    		}
		    		//再次加入监控
		    		CmdQueue.PRE_CMD_QUEUE.remove(cmdId);
		    		flg=true;
		    		...
		    	}
			}catch(Exception e){
	        	e.printStackTrace();
	        }
		}
	}

Guess you like

Origin blog.csdn.net/change_on/article/details/86179151