参考原文:https://blog.csdn.net/timchen525/article/details/82903110
原理
利用Executors的Future的限时get方法,Google的SimpleTimeLimiter本质上是对Executors的Future的包装。
1、基于类级别的调用
(1)编写接口类UserInfoService.java
public interface UserInfoService {
String getUserName() throws InterruptedException;
}
(2)编写对应的实现类UserInfoServiceImpl.java
public class UserInfoServiceImpl implements UserInfoService {
@Override
public String getUserName() throws InterruptedException {
System.out.println("123");
Thread.sleep(500);
System.out.println("456");
return "tim";
}
}
(3) 测试代码
public static void main(String[] args) {
TimeLimiter timeLimiter = new SimpleTimeLimiter();
UserInfoService userInfoService = new UserInfoServiceImpl();
UserInfoService userInfoService1 = timeLimiter.newProxy(userInfoService, UserInfoService.class, 20, TimeUnit.MILLISECONDS);
try {
String userName = userInfoService1.getUserName();
System.out.println("userName:" + userName);
} catch (Exception e) {
e.printStackTrace();
}
}
(4)综合上述可以写成更通用的
/**
* @author chenjintian
* @email chenjintian@meituan.com
* @date 2018/9/29
* Description:
*/
public class UserInfoServiceImpl implements UserInfoService {
private static final TimeLimiter TIME_LIMITER = new SimpleTimeLimiter();
// 设置为可配置
private static final long TIME_OUT_DURATION = 200;
private static UserInfoService userInfoServiceProxy;
static {
UserInfoService userInfoService = new UserInfoServiceImpl();
userInfoServiceProxy = TIME_LIMITER.newProxy(userInfoService, UserInfoService.class, TIME_OUT_DURATION, TimeUnit.MILLISECONDS);
}
@Override
public String getUserName(String info) throws InterruptedException {
System.out.println("123");
Thread.sleep(500);
System.out.println("456");
return info;
}
@Override
public String getUserNameWithTimeout(String info) throws InterruptedException {
return userInfoServiceProxy.getUserName(info);
}
@Override
public String getSex(String txt) {
return "";
}
@Override
public String getSexWithTimeout(String txt) {
return userInfoServiceProxy.getSex(txt);
}
public static void main(String[] args) throws InterruptedException {
UserInfoService userInfoService = new UserInfoServiceImpl();
String result = userInfoService.getUserNameWithTimeout("tim");
System.out.println("result is:" + result);
}
}
————————————————
2、基于方法级别的调用
public class SimpleTimeLimiterDemo {
public static void main(String[] args) {
SimpleTimeLimiter simpleTimeLimiter = new SimpleTimeLimiter();
final AtomicBoolean isInterrupted = new AtomicBoolean();
Callable<String> task = new Callable<String>() {
@Override
public String call() throws Exception {
isInterrupted.set(true);
Thread.sleep(200);
isInterrupted.set(false);
return "success";
}
};
try {
String result = simpleTimeLimiter.callWithTimeout(task, 20, TimeUnit.MILLISECONDS, true);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
————————————————
示例三:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.facebook.presto.jdbc.internal.guava.util.concurrent.SimpleTimeLimiter;
public void execute(JobExecutionContext context) throws JobExecutionException {
ExecutorService executor = Executors.newFixedThreadPool(1);
SimpleTimeLimiter stl = new SimpleTimeLimiter(executor);
//接受返回的结果
String result = null ;
long startTime = System.currentTimeMillis();
try {
result = stl.callWithTimeout(new Callable<String>() {
@Override
public String call() throws Exception {
//调用具体业务方法
return "success";
}
}, 20, TimeUnit.MINUTES, true);
}catch(Exception e) {
long elapsed = System.currentTimeMillis() - startTime;
//花费多少分钟
double costtime = elapsed / 1000 / 60;
logger.error(String.format("%s,异常:%s",costtime,e.getLocalizedMessage() ));
}finally {
executor.shutdown();
}
}