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中的方法只来得及执行第一句。

posted @ 2017-10-10 14:08  剑握在手  阅读(1069)  评论(0编辑  收藏  举报
返回顶部↑