关于监控

线程池的监控
根据上面ExecutorService对象即可获取一般线程池需要监控的所需信息。
@Override
public void monitor(Map, String> monitor) {
    long tmp = executor.getCompletedTaskCount();
    int qsize = executor.getQueue().size();
    monitor.put(QUEUE_LEN, String.valueOf(qsize));
    int activeCount = executor.getActiveCount();
    monitor.put(WORK_THREAD, String.valueOf(activeCount));
    monitor.put(ALIVE_THREAD, String.valueOf(executor.getPoolSize()));
    int corePoolSize = executor.getCorePoolSize();

    monitor.put(CORE_THREAD, String.valueOf(corePoolSize));
    monitor.put(MAX_THREAD, String.valueOf(executor.getMaximumPoolSize()));
    monitor.put(COMPLETE_TASK, String.valueOf(tmp));
    monitor.put(TOTAL_TASK, String.valueOf(executor.getTaskCount()));

    int maxPoolSize = executor.getMaximumPoolSize();
    int count = (int) (tmp - cmpcount);
    cmpcount = tmp;
    monitor.put(TPS, String.valueOf(count));
    Date d = new Date();
    SimpleDateFormat now = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
    String nowDate = now.format(d);
    //如果为空证明没有报过,如果不为空则证明报过忽略,放入告警10分钟后过期                !!!监控队列     但前activecount线程 总线成书。
    if ( (qsize * 100 / poolConfig.getQueueSize() >= 80) && (null == this.queueAlarmed.get(this.name))) {
        this.queueAlarmed.put(this.name, Boolean.TRUE, 10 * 60);
        this.alarm.sendAlarm(this.name, "PROBLEM:[" + this.name + "-pool]"+ ":" +activeCount+"/"+maxPoolSize+",队列:"+ qsize + "/"+ poolConfig.getQueueSize()+"/"+nowDate);

    } else if ((activeCount * 100 / maxPoolSize < 80) && (null != this.queueAlarmed.get(this.name))) {
        //已经报过警,并且活动线程数小于corepoolsize的70%则认为恢复
        this.alarm.sendAlarm(this.name, "OK:[" + this.name + "-pool]"+ ":" +activeCount+"/"+maxPoolSize+",队列:"+ qsize + "/"+ poolConfig.getQueueSize()+"/"+nowDate);
        this.queueAlarmed.remove(this.name);}
JVM的监控
主要涉及到lang包中的Runtiem类。
// max memory for this JVM
long maxMemory = Runtime.getRuntime().maxMemory();

// memory already allocated
long allocatedMemory = Runtime.getRuntime().totalMemory();
// already used allocated memory
long allocatedFreeMemory = Runtime.getRuntime().freeMemory();

// total usable memory for this JVM
long totalFreeMemory = maxMemory - allocatedMemory + allocatedFreeMemory;


// get thread count
ThreadGroup parentThread;
for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent())
   ;
int totalThread = parentThread.activeCount();

monitor.put(MAX_MEMORY, maxMemory+"");
monitor.put(FREE_MEMORY, totalFreeMemory+"");
monitor.put(THREAD_COUNTS, totalThread+"");

你可能感兴趣的:(并发问题)