spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor
官方文档:
/**
* Support class for throttling concurrent access to a specific resource.
*
* <p>Designed for use as a base class, with the subclass invoking
* the {@link #beforeAccess()} and {@link #afterAccess()} methods at
* appropriate points of its workflow. Note that {@code afterAccess}
* should usually be called in a finally block!
*
* <p>The default concurrency limit of this support class is -1
* ("unbounded concurrency"). Subclasses may override this default;
* check the javadoc of the concrete class that you're using.
*
* @author Juergen Hoeller
* @since 1.2.5
* @see #setConcurrencyLimit
* @see #beforeAccess()
* @see #afterAccess()
* @see org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor
* @see java.io.Serializable
*/
beforeAccess()实现
/**
* To be invoked before the main execution logic of concrete subclasses.
* <p>This implementation applies the concurrency throttle.
* @see #afterAccess()
*/
protected void beforeAccess() {
if (this.concurrencyLimit == NO_CONCURRENCY) {
throw new IllegalStateException(
"Currently no invocations allowed - concurrency limit set to NO_CONCURRENCY");
}
if (this.concurrencyLimit > 0) {
boolean debug = logger.isDebugEnabled();
synchronized (this.monitor) {
boolean interrupted = false;
while (this.concurrencyCount >= this.concurrencyLimit) {
if (interrupted) {
throw new IllegalStateException("Thread was interrupted while waiting for invocation access, " +
"but concurrency limit still does not allow for entering");
}
if (debug) {
logger.debug("Concurrency count " + this.concurrencyCount +
" has reached limit " + this.concurrencyLimit + " - blocking");
}
try {
this.monitor.wait();
}
catch (InterruptedException ex) {
// Re-interrupt current thread, to allow other threads to react.
Thread.currentThread().interrupt();
interrupted = true;
}
}
if (debug) {
logger.debug("Entering throttle at concurrency count " + this.concurrencyCount);
}
this.concurrencyCount++;
}
}
}
afterAccess()实现
/**
* To be invoked after the main execution logic of concrete subclasses.
* @see #beforeAccess()
*/
protected void afterAccess() {
if (this.concurrencyLimit >= 0) {
synchronized (this.monitor) {
this.concurrencyCount--;
if (logger.isDebugEnabled()) {
logger.debug("Returning from throttle at concurrency count " + this.concurrencyCount);
}
this.monitor.notify();
}
}
}
ConcurrencyThrottleSupport是个抽象类,其具体的实现类ConcurrencyThrottleInterceptor
/**
* Interceptor that throttles concurrent access, blocking invocations
* if a specified concurrency limit is reached.
*
* <p>Can be applied to methods of local services that involve heavy use
* of system resources, in a scenario where it is more efficient to
* throttle concurrency for a specific service rather than restricting
* the entire thread pool (e.g. the web container's thread pool).
*
* <p>The default concurrency limit of this interceptor is 1.
* Specify the "concurrencyLimit" bean property to change this value.
*
* @author Juergen Hoeller
* @since 11.02.2004
* @see #setConcurrencyLimit
*/
@SuppressWarnings("serial")
public class ConcurrencyThrottleInterceptor extends ConcurrencyThrottleSupport
implements MethodInterceptor, Serializable { public ConcurrencyThrottleInterceptor() {
setConcurrencyLimit(1);
} @Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
beforeAccess();
try {
return methodInvocation.proceed();
}
finally {
afterAccess();
}
} }
spring控制并发数的工具类ConcurrencyThrottleSupport和ConcurrencyThrottleInterceptor的更多相关文章
- spring boot 结合Redis 实现工具类
		
自己整理了 spring boot 结合 Redis 的工具类引入依赖 <dependency> <groupId>org.springframework.boot</g ...
 - 高可用的Spring FTP上传下载工具类(已解决上传过程常见问题)
		
前言 最近在项目中需要和ftp服务器进行交互,在网上找了一下关于ftp上传下载的工具类,大致有两种. 第一种是单例模式的类. 第二种是另外定义一个Service,直接通过Service来实现ftp的上 ...
 - java代码行数统计工具类
		
package com.syl.demo.test; import java.io.*; /** * java代码行数统计工具类 * Created by 孙义朗 on 2017/11/17 0017 ...
 - 并发编程常用工具类(二) SymaPhore实现线程池
		
1.symaPhore简介 symaphore(信号量)用来控制同时访问某个资源的线程数量,一般用在并发流量控制.个人对它的理解相当于是接待室每次只能接待固定数量的人,当达到最高接待数的时候,其他人就 ...
 - spring -mvc service层调用工具类配置
		
在service层时调用工具类时服务返回工具类对象为空 在此工具类上加上@Component注解就可以了 @Component:把普通pojo实例化到spring容器中,相当于配置文件中的 <b ...
 - 《java并发编程实战》读书笔记4--基础构建模块,java中的同步容器类&并发容器类&同步工具类,消费者模式
		
上一章说道委托是创建线程安全类的一个最有效策略,只需让现有的线程安全的类管理所有的状态即可.那么这章便说的是怎么利用java平台类库的并发基础构建模块呢? 5.1 同步容器类 包括Vector和Has ...
 - dubbo是如何控制并发数和限流的?
		
ExecuteLimitFilter ExecuteLimitFilter ,在服务提供者,通过 的 "executes" 统一配置项开启: 表示每服务的每方法最大可并行执行请求数 ...
 - SSM(Spring)中,在工具类中调用服务层的方法
		
因为平时在调用service层时都是在controller中,有配置扫描注入,spring会根据配置自动注入所依赖的服务层. 但因我们写的工具类不属于controller层,所以当所写接口需要调用服务 ...
 - 项目ITP(四) javaweb http json 交互 in action  (服务端 spring  手机端 提供各种工具类)勿喷!
		
前言 系列文章:[传送门] 洗了个澡,准备写篇博客.然后看书了.时间 3 7 分.我慢慢规律生活,向目标靠近. 很喜欢珍惜时间像叮当猫一样 正文 慢慢地,二维码实现签到将要落幕了.下篇文章出二维码实 ...
 
随机推荐
- AmazeUI 框架知识点-布局和样式整理
			
1.Amaze UI 将所有元素的盒模型设置为 border-box.这下好了,妈妈再也不用担心没计算好 padding.border 而使布局破相了. 2.Amaze UI 将浏览器的基准字号设置为 ...
 - Hello Bugs
			
2014-01-09 [Maven]Not Authorized ReasonPhame: Failed to decrypt password... 描述:eclipse中使用maven进行mvn ...
 - 基于webdriver的jmeter性能测试-Selenium IDE
			
前言: 由于某些项目使用了WebGL技术,需要高版本的Firefox和Chrome浏览器才能支持浏览,兼容性很弱,导致Loadrunner和jmeter(badboy)无法正常进行录制脚本.因此我们采 ...
 - 初识Windows程序
			
首先,我们创建第一个Windows程序,一共分为4个步骤: 1.打开Visual Studio开发工具 2.选择"文件"→"新建"→"项目" ...
 - weblogic 12c web部署注意的问题
			
废话不多说下面讲介绍他的具体部署web应用,应该注意哪些问题. 准备工作: Java JDK 安装及环境配置 http://jingyan.baidu.com/article/ff41162596a7 ...
 - js测试题
			
(function(){ return typeof arguments;})();"object" var f = function g(){ return 23; };type ...
 - 分分钟用上C#中的委托和事件
			
每一个初学C#的程序猿,在刚刚碰到委托和事件的概念时,估计都是望而却步,茫然摸不到头脑的.百度一搜,关于概念介绍的文章大把大把的,当然也不乏深入浅出的好文章.可看完这些文章,大多数新手,估计也只是信心 ...
 - 一小时包教会 —— webpack 入门指南
			
什么是 webpack? webpack是近期最火的一款模块加载器兼打包工具,它能把各种资源,例如JS(含JSX).coffee.样式(含less/sass).图片等都作为模块来使用和处理. 我们可以 ...
 - .net中事件引起的内存泄漏分析
			
系列主题:基于消息的软件架构模型演变 在Winform和Asp.net时代,事件被大量的应用在UI和后台交互的代码中.看下面的代码: private void BindEvent() { var bt ...
 - 如何使用Worktile进行敏捷项目开发管理
			
Worktile在任务管理上采用了看板视图,非常适合进行敏捷项目开发管理.事实上,在开发Worktile的过程中,我们也是自产自销,使用Worktile管理Worktile本身的开发过程,在本文中跟大 ...