Hystrix的一个坑,queue中的run方法没有被执行?
2023-09-11 14:19:27 时间
今天学的时候随手测了一下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中的方法只来得及执行第一句。
相关文章
- 接口方法执行时的查找顺序
- i和j的值交换的方法
- CASE函数 sql server——分组查询(方法和思想) ref和out 一般处理程序结合反射技术统一执行客户端请求 遍历查询结果集,update数据 HBuilder设置APP状态栏
- Sqlite中Sql_server四种执行ExecuteReader、ExecuteNonQuery、ExecuteScalar、DataSet.docx的方法实例
- 类的专有方法(__getitem__和__setitem__)
- 《贝叶斯方法:概率编程与贝叶斯推断》一1.4 使用计算机执行贝叶斯推断
- [转]java如何保证一个方法只能执行一次
- Linux执行shell脚本的方法
- 转 Windows 7设置定时自动执行任务方法
- 《计算机网络:自顶向下方法(原书第6版)》一1.1 什么是因特网
- 简记:自定义tabBar的组件,pageLifeTimes中的show方法无法执行?
- python OS库常用方法总结
- JQuery的show方法执行调用以后,会秒退,并且返回上一个状态
- 手把手教你搭建一个深度网络模型:从输入层-激活函数-损失函数-优化方法-输出层-执行训练
- 前端性能优化:高频执行事件/方法的防抖
- Activity 中的Toast在Activity销毁后报错,解决方法,把context改成应用的
- 执行游戏时出现0xc000007b错误的解决方法
- 【图像处理】——遍历图像所有像素的方法(for 循环和np.where遍历)应用在模板制作
- angularjs如何在视图渲染结束之后,或者render之后执行指令中的link方法呢?
- 封装异步API-js先执行完一个函数后再执行下面的逻辑或者方法