// Path: src/main/java/com/yiji/falcon/agent/util/BlockingQueueUtil.java
// public class BlockingQueueUtil {
// 
//     /**
//      * 获取结果
//      * @param blockingQueue
//      * @param timeout
//      * @param unit
//      * @return
//      * @throws InterruptedIOException
//      */
//     public static Object getResult(BlockingQueue<Object> blockingQueue,long timeout, TimeUnit unit) throws InterruptedIOException {
//         Object result;
//         try {
//             result = blockingQueue.poll(timeout, unit);
//             if (result == null) {
//                 if (!blockingQueue.offer(""))
//                     result = blockingQueue.take();
//             }
//         } catch (InterruptedException e) {
//             throw ExceptionUtil.initCause(new InterruptedIOException(e.getMessage()), e);
//         }
//         return result;
//     }
// 
// }
// 
// Path: src/main/java/com/yiji/falcon/agent/util/ExecuteThreadUtil.java
// public class ExecuteThreadUtil {
//     private static ExecutorService executorService;
// 
//     static {
//         final int maxPoolSize = AgentConfiguration.INSTANCE.getAgentMaxThreadCount();
//         //定义并发执行服务
//         executorService = new ThreadPoolExecutor(5, maxPoolSize, 0L, TimeUnit.MILLISECONDS,
//                 new SynchronousQueue<>(),
//                 r -> {
//                     Thread t = new Thread(r);
//                     t.setName("agentThreadPool");
//                     return t;
//                 },new ThreadPoolExecutor.DiscardOldestPolicy()
//         );
//     }
// 
//     /**
//      * 执行线程任务
//      *
//      * @param task
//      */
//     public static void execute(Runnable task) {
//         executorService.submit(task);
//     }
// 
//     /**
//      * 执行线程任务
//      *
//      * @param task
//      * @param <T>
//      * @return
//      */
//     public static <T> Future<T> execute(Callable<T> task) {
//         return executorService.submit(task);
//     }
// 
//     /**
//      * 关闭线程池
//      */
//     public static void shutdown() {
//         executorService.shutdown();
//     }
// }


import com.yiji.falcon.agent.util.BlockingQueueUtil;
import com.yiji.falcon.agent.util.ExecuteThreadUtil;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
/*
 * www.yiji.com Inc.
 * Copyright (c) 2016 All Rights Reserved
 */
package com.yiji.falcon.agent.jmx;
/*
 * 修订记录:
 * guqiu@yiji.com 2016-08-09 10:25 创建
 */



/**
 * @author guqiu@yiji.com
 */
public class JMXConnectWithTimeout {

    /**
     * JMX连接
     * @param url
     * JMX连接地址
     * @param jmxUser
     * JMX授权用户 null为无授权用户
     * @param jmxPassword
     * JMX授权密码 null为无授权密码
     * @param timeout
     * 超时时间
     * @param unit
     * 超时单位
     * @return
     * @throws IOException
     */
    public static JMXConnector connectWithTimeout( final JMXServiceURL url,String jmxUser,String jmxPassword, long timeout, TimeUnit unit) throws Exception {
        final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
// Path: src/main/java/com/yiji/falcon/agent/util/BlockingQueueUtil.java
// public class BlockingQueueUtil {
// 
//     /**
//      * 获取结果
//      * @param blockingQueue
//      * @param timeout
//      * @param unit
//      * @return
//      * @throws InterruptedIOException
//      */
//     public static Object getResult(BlockingQueue<Object> blockingQueue,long timeout, TimeUnit unit) throws InterruptedIOException {
//         Object result;
//         try {
//             result = blockingQueue.poll(timeout, unit);
//             if (result == null) {
//                 if (!blockingQueue.offer(""))
//                     result = blockingQueue.take();
//             }
//         } catch (InterruptedException e) {
//             throw ExceptionUtil.initCause(new InterruptedIOException(e.getMessage()), e);
//         }
//         return result;
//     }
// 
// }
// 
// Path: src/main/java/com/yiji/falcon/agent/util/ExecuteThreadUtil.java
// public class ExecuteThreadUtil {
//     private static ExecutorService executorService;
// 
//     static {
//         final int maxPoolSize = AgentConfiguration.INSTANCE.getAgentMaxThreadCount();
//         //定义并发执行服务
//         executorService = new ThreadPoolExecutor(5, maxPoolSize, 0L, TimeUnit.MILLISECONDS,
//                 new SynchronousQueue<>(),
//                 r -> {
//                     Thread t = new Thread(r);
//                     t.setName("agentThreadPool");
//                     return t;
//                 },new ThreadPoolExecutor.DiscardOldestPolicy()
//         );
//     }
// 
//     /**
//      * 执行线程任务
//      *
//      * @param task
//      */
//     public static void execute(Runnable task) {
//         executorService.submit(task);
//     }
// 
//     /**
//      * 执行线程任务
//      *
//      * @param task
//      * @param <T>
//      * @return
//      */
//     public static <T> Future<T> execute(Callable<T> task) {
//         return executorService.submit(task);
//     }
// 
//     /**
//      * 关闭线程池
//      */
//     public static void shutdown() {
//         executorService.shutdown();
//     }
// }


// Path: src/main/java/com/yiji/falcon/agent/jmx/JMXConnectWithTimeout.java
import com.yiji.falcon.agent.util.BlockingQueueUtil;
import com.yiji.falcon.agent.util.ExecuteThreadUtil;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/*
 * www.yiji.com Inc.
 * Copyright (c) 2016 All Rights Reserved
 */
package com.yiji.falcon.agent.jmx;
/*
 * 修订记录:
 * guqiu@yiji.com 2016-08-09 10:25 创建
 */



/**
 * @author guqiu@yiji.com
 */
public class JMXConnectWithTimeout {

    /**
     * JMX连接
     * @param url
     * JMX连接地址
     * @param jmxUser
     * JMX授权用户 null为无授权用户
     * @param jmxPassword
     * JMX授权密码 null为无授权密码
     * @param timeout
     * 超时时间
     * @param unit
     * 超时单位
     * @return
     * @throws IOException
     */
    public static JMXConnector connectWithTimeout( final JMXServiceURL url,String jmxUser,String jmxPassword, long timeout, TimeUnit unit) throws Exception {
        final BlockingQueue<Object> blockingQueue = new ArrayBlockingQueue<>(1);
        ExecuteThreadUtil.execute(() -> {