Guava使用RateLimiter做接口限流
2018-02-09 16:10
465 查看
google开源工具包guava提供了限流工具类RateLimiter,该类基于“令牌桶算法”,非常方便使用。
ListenableFuture与RateLimiter示例
ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强。我们知道Future表示一个异步计算任务,当任务完成时可以得到计算结果。如果我们希望一旦计算完成就拿到结果展示给用户或者做另外的计算,就必须使用另一个线程不断的查询计算状态。这样做,代码复杂,而且效率低下。使用ListenableFuture Guava帮我们检测Future是否完成了,如果完成就自动调用回调函数,这样可以减少并发程序的复杂度。
推荐使用第二种方法,因为第二种方法可以直接得到Future的返回值,或者处理错误情况。本质上第二种方法是通过调动第一种方法实现的,做了进一步的封装。
另外ListenableFuture还有其他几种内置实现:
1.SettableFuture:不需要实现一个方法来计算返回值,而只需要返回一个固定值来做为返回值,可以通过程序设置此Future的返回值或者异常信息
2.CheckedFuture: 这是一个继承自ListenableFuture接口,他提供了checkedGet()方法,此方法在Future执行发生异常时,可以抛出指定类型的异常。
http://blog.csdn.net/JIESA/article/details/50412027
/** * @author wzx * @time 2018/2/9 */ public class RateLimiterDemo { public static void main(String[] args) { Long start = System.currentTimeMillis(); RateLimiter limiter = RateLimiter.create(1); for (int i = 0; i < 10; i++) { limiter.acquire(); System.out.println("call execute.." + i); } Long end = System.currentTimeMillis(); System.out.println(end - start); } }
ListenableFuture与RateLimiter示例
ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强。我们知道Future表示一个异步计算任务,当任务完成时可以得到计算结果。如果我们希望一旦计算完成就拿到结果展示给用户或者做另外的计算,就必须使用另一个线程不断的查询计算状态。这样做,代码复杂,而且效率低下。使用ListenableFuture Guava帮我们检测Future是否完成了,如果完成就自动调用回调函数,这样可以减少并发程序的复杂度。
推荐使用第二种方法,因为第二种方法可以直接得到Future的返回值,或者处理错误情况。本质上第二种方法是通过调动第一种方法实现的,做了进一步的封装。
另外ListenableFuture还有其他几种内置实现:
1.SettableFuture:不需要实现一个方法来计算返回值,而只需要返回一个固定值来做为返回值,可以通过程序设置此Future的返回值或者异常信息
2.CheckedFuture: 这是一个继承自ListenableFuture接口,他提供了checkedGet()方法,此方法在Future执行发生异常时,可以抛出指定类型的异常。
/** * @author wzx * @time 2018/2/9 */ public class ListenableFutureDemo { public static void main(String[] args) { // testRateLimiter(); testListenableFuture(); } public static void testRateLimiter() { ListeningExecutorService listeningDecorator = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); RateLimiter limiter = RateLimiter.create(1); for (int i = 0; i < 10; i++) { limiter.acquire(); ListenableFuture<Integer> listenableFuture = listeningDecorator.submit(new Task("is" + i)); } } public static void testListenableFuture() { ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool()); ListenableFuture<Integer> listenableFuture = service.submit(new Task("testListenableFuture")); try { //同步获取调用结果 listenableFuture.get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } //第一种方式 listenableFuture.addListener(new Runnable() { @Override public void run() { try { System.out.println("get listenable future's result " + listenableFuture.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }, service); //第二种方式 Futures.addCallback(listenableFuture, new FutureCallback<Integer>() { @Override public void onSuccess(@Nullable Integer result) { System.out.println("get listenable future's result with callback " + result); } @Override public void onFailure(Throwable t) { t.printStackTrace(); } }); } }
/** * @author wzx * @time 2018/2/9 */ public class Task implements Callable<Integer> { String str; public Task(String str) { this.str = str; } @Override public Integer call() throws Exception { System.out.println("call execute.." + str); TimeUnit.SECONDS.sleep(1); return 1; } }
http://blog.csdn.net/JIESA/article/details/50412027
相关文章推荐
- RateLimit--使用guava来做接口限流
- RateLimit--使用guava来做接口限流-转载
- RateLimit-使用guava来做接口限流代码示例
- RateLimit--使用guava来做接口限流
- RateLimit--使用guava来做接口限流
- 实战限流(guava的RateLimiter)
- 使用guava-retry优雅的实现接口重试
- RateLimit--使用guava来做接口限流
- 限流模式-Guava的RateLimiter
- 限流模式-Guava的RateLimiter
- Guava Multiset接口使用与分析
- 使用guava来做接口限流
- Google Guava 缓存实现接口的限流
- Guava来做接口限流RateLimit
- 使用Guava retryer优雅的实现接口重调机制
- RateLimit--使用guava来做接口限流
- 使用RateLimiter完成简单的大流量限流,抢购秒杀限流
- 【Guava】使用Guava的RateLimiter做限流
- RateLimit--使用guava来做接口限流
- 共享内存使用的基本思路和接口