• Hystrix的一个坑,queue中的run方法没有被执行?


    今天学的时候随手测了一下Hystrix的queue的异步执行,发现执行queue之后,还没有打印run方法中的内容,程序就结束了:

    import com.netflix.hystrix.HystrixCommand;
    import com.netflix.hystrix.HystrixCommandGroupKey;
    import com.netflix.hystrix.HystrixCommandProperties;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    
    /**
     * Created by liu.yuxiang on 2017/10/10.
     */
    public class UserCommand extends HystrixCommand<String> {
        private String name;
        protected UserCommand(Setter setter,String name) {
            super(setter);
            this.name=name;
        }
    
        public String run() throws InterruptedException {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(int i=0;i<3;i++){
                Thread.sleep(1000l);
                Date d = new Date();
                System.out.println(sdf.format(d)+"休息一秒后打印--"+i+":hello "+name);
            }
            return "hello "+name;
        }
    
        public static void main(String[] args) throws Exception {
    
            UserCommand userCommand = new UserCommand(
                    Setter.withGroupKey(
                            HystrixCommandGroupKey.Factory.asKey("")
                    ).andCommandPropertiesDefaults(
                            HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(50000)
                    ),"tester");
    
    
            Future<String> f = userCommand.queue();
            new Thread(){
                public void run(){
                    for(int i=0;i<5;i++){
                        System.out.println("t1-"+i);
                    }
                }
            }.start();
            String result = null;
            //result = f.get();
            System.out.println("finaly:"+result);
        }
    }

    其实queue还是异步执行的,只不过使用queue创建的是一个 【守护线程】,该线程还没来得及执行,主线程就已经结束了,改成以下形式就能看出来:

    import com.netflix.hystrix.HystrixCommand;
    import com.netflix.hystrix.HystrixCommandGroupKey;
    import com.netflix.hystrix.HystrixCommandProperties;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.TimeoutException;
    
    /**
     * Created by liu.yuxiang on 2017/10/10.
     */
    public class UserCommand extends HystrixCommand<String> {
        private String name;
        protected UserCommand(Setter setter,String name) {
            super(setter);
            this.name=name;
        }
    
        public String run() throws InterruptedException {
            System.out.println("im in");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(int i=0;i<3;i++){
                Thread.sleep(1000l);
                Date d = new Date();
                System.out.println(sdf.format(d)+"休息一秒后打印--"+i+":hello "+name);
            }
            return "hello "+name;
        }
    
        public static void main(String[] args) throws Exception {
    
            UserCommand userCommand = new UserCommand(
                    Setter.withGroupKey(
                            HystrixCommandGroupKey.Factory.asKey("")
                    ).andCommandPropertiesDefaults(
                            HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(50000)
                    ),"tester");
    
    
            Future<String> f = userCommand.queue();
            new Thread(){
                public void run(){
                    try {
                        Thread.sleep(1000l);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    for(int i=0;i<5;i++){
                        System.out.println("t1-"+i);
                    }
                }
            }.start();
            String result = null;
            //result = f.get();
            System.out.println("finaly:"+result);
        }
    }

    run中的方法只来得及执行第一句。

  • 相关阅读:
    【模板整合计划】高阶数据结构
    【模板整合计划】高阶数据结构—线段树
    主席树【权值线段树】(转)
    Flask系列(二) 模板 templates
    用java实现Shazam 译文
    [转] 研究云计算与海量数据处理方向建议看的论文列表
    程序员应知 如何分析海量数据
    大数据技术大会
    android监控网络状态
    HP(惠普)大中华区总裁孙振耀退休感言
  • 原文地址:https://www.cnblogs.com/flying607/p/7645308.html
Copyright © 2020-2023  润新知