天天看点

使用Quartz调度多台机器的资源

需求:多实例机器处理各自分表

环境:1.多实例机器。2.已配置好 各台机器的IP 与 所有分表的对应关系。

效果:无论哪个机器得到的资源,它都要通过HTTP请求通知另外的实例去执行相应的方法。

(1)获取到系统中所有线程

代码参考自获取Java VM中当前运行的所有线程 - 苍穹冰尘 

/**
     *  获取Java VM中当前运行的所有线程
     * @return
     */
    public static Thread[] findAllThreads() {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup topGroup = group;
        // 遍历线程组树,获取根线程组
        while (group != null) {
            topGroup = group;
            group = group.getParent();
        }
        // 激活的线程数加倍
        int estimatedSize = topGroup.activeCount() * 2;
        Thread[] slacks = new Thread[estimatedSize];
         //获取根线程组的所有线程
        int actualSize = topGroup.enumerate(slacks);
        Thread[] threads = new Thread[actualSize];
        System.arraycopy(slacks, 0, threads, 0, actualSize);
        return threads;
    }      

(2)通过当前IP查到该机器需要处理的分表。

(3)对所有线程进行遍历,然后判断是否有处理某张分表的thread_${tableName}线程。

                没有则启动。  已启动则忽略。

                伪代码:

                if ( JVM中所有线程 . 不包含 ( thread_${tableName} ) ) {