com.facebook.presto.jdbc.internal.guava.util.concurrent.SimpleTimeLimiter 设置超时

参考原文: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();
        }
        
    }

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值