//package   cn.wise.sc.energy.power.plant.business.opentsdb;
//
//import   cn.wise.sc.energy.power.plant.business.opentsdb.bean.PutExt;
//import   cn.wise.sc.energy.power.plant.business.opentsdb.bean.PutRequestExt;
//import   cn.wise.sc.energy.power.plant.business.opentsdb.bean.QueryExt;
//import   cn.wise.sc.energy.power.plant.business.opentsdb.bean.QueryRequestExt;
//import com.jd.platform.async.callback.ICallback;
//import com.jd.platform.async.callback.IWorker;
//import com.jd.platform.async.executor.Async;
//import com.jd.platform.async.executor.timer.SystemClock;
//import com.jd.platform.async.worker.WorkResult;
//import com.jd.platform.async.wrapper.WorkerWrapper;
//import kotlin.jvm.Volatile;
//import net.opentsdb.client.api.put.request.PutRequest;
//import net.opentsdb.client.api.put.response.PutResponse;
//import net.opentsdb.client.api.query.request.QueryRequest;
//import net.opentsdb.client.api.query.response.QueryResponse;
//import net.opentsdb.client.bean.Aggregator;
//import net.opentsdb.client.bean.DataPoint;
//import net.opentsdb.client.bean.Filter;
//import net.opentsdb.client.bean.Query;
//
//import java.io.IOException;
//import java.net.URISyntaxException;
//import java.util.HashMap;
//import java.util.LinkedList;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.CompletableFuture;
//import java.util.concurrent.CompletionStage;
//import java.util.concurrent.ExecutionException;
//import java.util.function.BiConsumer;
//import java.util.function.BiFunction;
//import java.util.function.Consumer;
//import java.util.function.Function;
//import java.util.function.Supplier;
//
//public class QueryTest {
//
//    public static void main(String[] args) throws IOException, URISyntaxException {
//
//        List<Filter> filters = new LinkedList<>();
//
//        // queries
//        List<Query> queries = new LinkedList<>();
//
//        QueryRequest request = QueryRequest.builder()
//                .start("2012/09/05-20:00:00")
//                .end("2012/09/05-20:01:40")
//                .queries(queries)
//                .build();
//
////        QueryRequestExt requestz = QueryRequestExt.builder()
////                .start("3h-ago")
////                .build()
////                .addQuery(QueryExt.builder()
////                        .aggregator(Aggregator.NONE)
////                        .metric("sys.cpu.nice")
////                        .build()
////                        .putTag("host","test01")
////                        .putTag("dc","lga")
////                );
////
////        QueryResponse response = OpentsdbOkHttpClient.query(requestz);
////        System.out.println(response);
//
//// queries
//        System.out.println("=================PUT=================");
////        List<DataPoint> put = new LinkedList<>();
////        HashMap map = new HashMap();
////        map.put("host", "test01");
////        map.put("dc", "lga");
//////        DataPoint dataPoint = DataPoint.builder()
//////                .metric("sys.cpu.nice")
//////                .tags(map)
//////                .value(18)
//////                .timestamp(System.currentTimeMillis())
//////                .build();
//////        put.add(dataPoint);
//////
//////        PutRequest putRequest = PutRequest.builder()
//////                .dataPoints(put)
//////                .build();
//////        PutResponse put1 = OpentsdbOkHttpClient.put(putRequest);
//
//        PutRequestExt putRequestExt = PutRequestExt.builder()
//                .details(true)
//                .build().addDataPoints(PutExt.builder()
//                        .metric("sys.cpu.nice")
//                        .value(18)
//                        .timestamp(System.currentTimeMillis())
//                        .build()
//                        .putTag("host", "test01")
//                        .putTag("dc", "lga"));
//        OpentsdbOkHttpClient opentsdbOkHttpClient = new OpentsdbOkHttpClient();
//        PutResponse put2 = opentsdbOkHttpClient.put(putRequestExt);
//
//
//        System.out.println(put2);
//
//    }
//
//    static volatile int a = 1;
//
//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//
//        //异步执行方法  不带返回值
//        CompletableFuture<Void> future = CompletableFuture.runAsync(new Runnable() {
//            @Override
//            public void run() {
//                for (int i = 0; i < 10; i++) {
//                    a++;
//                    System.out.println(a);
//                }
//            }
//        });
//        future.get();
//        System.out.println("================有返回值====================");
//        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                return a + 999;
//            }
//        });
//        future1.get();
//        System.out.println(future1.get());
//
//        System.out.println("================计算结果完成时触发回调====================");
//        CompletableFuture<Long> future2 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future2执行开始！");
//                return 8L;
//            }
//        }).whenComplete(new BiConsumer<Long, Throwable>() {
//            //完成时触发 是执行当前任务的线程执行继续执行 whenComplete 的任务。
//            //whenCompleteAsync 交由线程处理 Executor
//            //串行 执行完成回调 但ABC拿到的aLong都是8L 执行顺序: A->B,A->C,A->D
//            @Override
//            public void accept(Long aLong, Throwable throwable) {
//                System.out.println("future2执行结束！第一个完成回调开始");
//                aLong++;
//                System.out.println("future2的结果:" + aLong);
//            }
//        }).whenComplete(new BiConsumer<Long, Throwable>() {
//            @Override
//            public void accept(Long aLong, Throwable throwable) {
//                System.out.println("future2执行结束！第二个完成回调开始");
//                aLong++;
//                System.out.println("future2的结果:" + aLong);
//            }
//        }).whenComplete(new BiConsumer<Long, Throwable>() {
//            @Override
//            public void accept(Long aLong, Throwable throwable) {
//                System.out.println("future2执行结束！第三个完成回调开始");
//                aLong++;
//                System.out.println("future2的结果:" + aLong);
//            }
//        }).exceptionally(new Function<Throwable, Long>() {
//            //异常时触发
//            @Override
//            public Long apply(Throwable throwable) {
//                System.out.println("处理future2的异常!" + throwable.getMessage());
//                return null;
//            }
//        });
//        future2.get();
//
//        System.out.println("==============================将线程串行起来==========================");
//        CompletableFuture<Long> future3 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future3开始执行了");
//                return 3L;
//            }
//        }).thenApply(new Function<Long, Long>() {
//            //操作2个线程串行化
//            //和上面的成功回调有锤子个区别？？？？？
//            //这个只接受正常处理(等同于上面的whenCompleteAsync),异常就gg
//            //同时这个待第二个线程的返回值  成功回调是不带返回值得
//            //完成时触发 是执行当前任务的线程执行继续执行 whenComplete 的任务。
//            //whenCompleteAsync 交由线程处理 Executor
//            //串行 执行完成回调 但ABC拿到的aLong都是8L 执行顺序: A->B,A->C,A->D
//            @Override
//            public Long apply(Long aLong) {
//                System.out.println("future3执行完毕!");
//                System.out.println("第一次串行结果:" + aLong);
//                return aLong++;
//            }
//        }).thenApply(new Function<Long, Long>() {
//            @Override
//            public Long apply(Long aLong) {
//                System.out.println("future3执行完毕!");
//                System.out.println("第二次串行结果:" + aLong);
//                return aLong++;
//            }
//        }).thenApply(new Function<Long, Long>() {
//            @Override
//            public Long apply(Long aLong) {
//                System.out.println("future3执行完毕!");
//                System.out.println("第三次串行结果:" + aLong);
//                return aLong++;
//            }
//        });
//        future3.get();
//
//        System.out.println("=================第三种串行方法handle===================");
//        CompletableFuture<Long> future4 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future4开始执行!");
//                return 4L;
//            }
//        }).handle(new BiFunction<Long, Throwable, Long>() {
//            //这玩意完全等同上面的成功回调了   不说了
//            //区别成功回调出异常会被抛出exceptionally()需处理
//            //而handle不会抛出
//            //handle 会把第二次处理得返回值返回出去
//            @Override
//            public Long apply(Long aLong, Throwable throwable) {
//                System.out.println("future4开始执行结束或异常!");
//                System.out.println(aLong);
//                return aLong++;
//            }
//        }).handle(new BiFunction<Long, Throwable, Long>() {
//            @Override
//            public Long apply(Long aLong, Throwable throwable) {
//                System.out.println("future4开始执行结束或异常!");
//                System.out.println(aLong);
//                return aLong++;
//            }
//        });
//        System.out.println("===================接收线程得结果并消费 无返回值====================");
//        CompletableFuture<Void> future5 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future5开始执行");
//                return 5L / 0;
//            }
//        }).thenAccept(new Consumer<Long>() {
//            //消费结果  无返回值  所以只能Void接收
//            //这个我确实没看出和成功回调有什么不一样了
//            @Override
//            public void accept(Long aLong) {
//                System.out.println("future5生产得" + aLong + "已被消费？？？？");
//            }
//        }).exceptionally(new Function<Throwable, Void>() {
//            @Override
//            public Void apply(Throwable throwable) {
//                return null;
//            }
//        });
//        future5.get();
//
//        System.out.println("===================完成执行,不关注是否成功======================");
//        CompletableFuture<Void> future6 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future6开始执行了!");
//                return 6L / 0;
//            }
//        }).thenRun(new Runnable() {
//            // 是个Runnable  拿不到结果  不关注异常
//            @Override
//            public void run() {
//                System.out.println("future6已完成!但我不管你成功没成功!");
//            }
//        });
//
//        System.out.println("=====================合并处理完后再执行=======================");
//        //这个  合并任务得图解大概是:  等AB都执行完后执行C同时能在C中拿到AB结果
//        // A->
//        //     =>C  C有返回值
//        // B->
//        CompletableFuture<Long> future7 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future7开始执行!");
//                return 7L;
//            }
//        });
//        CompletableFuture<Long> future8 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future8开始执行!");
//                return 8L;
//            }
//        });
//        CompletableFuture future78 = future7.thenCombine(future8, new BiFunction<Long, Long, Long>() {
//            @Override
//            public Long apply(Long aLong, Long aLong2) {
//                System.out.println("future7和future8都执行完了,我future78要出头了");
//                return aLong + aLong2;
//            }
//        });
//        System.out.println(future78.get());
//
//        System.out.println("===========================合并AB,由C消耗=====================");
//        //图解同上
//        // A->
//        //     =>C  C无返回值
//        // B->
//        CompletableFuture<Long> future9 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future9开始执行!");
//                return 9L;
//            }
//        });
//        CompletableFuture<Long> future10 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future10开始执行!");
//                return 10L;
//            }
//        });
//        CompletableFuture future910 = future9.thenAcceptBoth(future10, new BiConsumer<Long, Long>() {
//            @Override
//            public void accept(Long aLong, Long aLong2) {
//                System.out.println("future9和future10都执行完了,我future910要出头了");
//            }
//        });
//        future910.get();
//
//        System.out.println("====================applyToEither acceptEither  ===================");
//        //AB谁先完成  用谁得结果 执行C  和上面得区别 就是 带不带返回值 accept消耗不返回 apply返回
//
//        System.out.println("=============runAfterEither  runAfterBoth==================");
//        //前者任意一个完成 就执行后续C操作,后者等AB都完成才操作,C都不带返回值
//
//
//        System.out.println("====================重头戏,实现多线程得串行,成与不成？ 成了！！=======================");
//        //图解  A->B->C->D->E  串行了
//        CompletableFuture<Long> future11 = CompletableFuture.supplyAsync(new Supplier<Long>() {
//            @Override
//            public Long get() {
//                System.out.println("future11开始执行了:" + 11L);
//                return 11L;
//            }
//        }).thenCompose(new Function<Long, CompletionStage<Long>>() {
//            @Override
//            public CompletionStage<Long> apply(Long aLong) {
//                return CompletableFuture.supplyAsync(new Supplier<Long>() {
//                    @Override
//                    public Long get() {
//                        Long a = aLong + 1;
//                        System.out.println("future11执行结束了,我future12k开始了!" + a);
//                        return a;
//                    }
//                });
//            }
//        }).thenCompose(new Function<Long, CompletionStage<Long>>() {
//            @Override
//            public CompletionStage<Long> apply(Long aLong) {
//                return CompletableFuture.supplyAsync(new Supplier<Long>() {
//                    @Override
//                    public Long get() {
//                        Long a = aLong + 1;
//                        System.out.println("future12执行结束了,我future13开始了!" + a);
//                        return a;
//                    }
//                });
//            }
//        });
//        future11.get();
//
//        System.out.println("============async tool===================");
//        ParWorker w = new ParWorker();
//        ParWorker w1 = new ParWorker();
//        ParWorker w2 = new ParWorker();
//
//        WorkerWrapper<String, String> workerWrapper2 = new WorkerWrapper.Builder<String, String>()
//                .worker(w2)
//                .callback(w2)
//                .param("2")
//                .build();
//
//        WorkerWrapper<String, String> workerWrapper1 = new WorkerWrapper.Builder<String, String>()
//                .worker(w1)
//                .callback(w1)
//                .param("1")
//                .build();
//
//        WorkerWrapper<String, String> workerWrapper = new WorkerWrapper.Builder<String, String>()
//                .worker(w)
//                .callback(w)
//                .param("0")
//                .build();
//
//        long now = SystemClock.now();
//        System.out.println("begin-" + now);
//
//        Async.beginWork(1500, workerWrapper, workerWrapper1, workerWrapper2);
////        Async.beginWork(800, workerWrapper, workerWrapper1, workerWrapper2);
////        Async.beginWork(1000, workerWrapper, workerWrapper1, workerWrapper2);
//
//        System.out.println("end-" + SystemClock.now());
//        System.err.println("cost-" + (SystemClock.now() - now));
//        System.out.println(Async.getThreadCount());
//
//        System.out.println(workerWrapper.getWorkResult());
//        Async.shutDown();
//
//    }
//
//    static class ParWorker implements IWorker<String, String>, ICallback<String, String> {
//
//        public String action(String object) {
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "result = " + SystemClock.now() + "---param = " + object + " from 1";
//        }
//
//        @Override
//        public String action(String s, Map<String, WorkerWrapper> map) {
//            return null;
//        }
//
//        @Override
//        public String defaultValue() {
//            return "worker1--default";
//        }
//
//        @Override
//        public void begin() {
//            //System.out.println(Thread.currentThread().getName() + "- start --" + System.currentTimeMillis());
//        }
//
//
//        @Override
//        public void result(boolean success, String param, WorkResult<String> workResult) {
//            if (success) {
//                System.out.println("callback worker1 success--" + SystemClock.now() + "----" + workResult.getResult()
//                        + "-threadName:" + Thread.currentThread().getName());
//            } else {
//                System.err.println("callback worker1 failure--" + SystemClock.now() + "----" + workResult.getResult()
//                        + "-threadName:" + Thread.currentThread().getName());
//            }
//        }
//
//    }
//
//
//}