java限流


public class ConcurrencyControl {

    private static ConcurrencyControl instance = new ConcurrencyControl();  

public static ConcurrencyControl getInstance(){
return instance;
}

private String preConfStr = "uws.concurrency.control.";

//并发对象
private Map<String, Object> synObjMap = new HashMap<String, Object>();
//并发量
private Map<String, Object> countMap = new HashMap<String, Object>();


private String ccfStrType = "CallCCF";
private int countForCCF = 0;
private Object ccfObj = new Object();

private String ilogStrType = "CallILOG";
private int countForILOG = 0;
private Object ilogObj = new Object();


private String taobaoRequest = "taobaoRequest";
private int taobaoCounts = 0;
private Object requestObj = new Object();


private String ccfStrTypeITS = "CallCCFForITS";
private int countForCCFITS = 0;
private Object ccfObjITS = new Object();


private String freyUndwRequest = "freyUndwRequest";
private int freyUndwCounts = 0;
private Object freyUndw = new Object();

/**
* 线程控制 未达到最大并发量,则计数器增1
* @param proType
* @param o
* @return
*/
public boolean isOverLimit(String proType, Object o) {
if (StringUtils.isEmpty(proType)) {
return false;
}
int concuLimit = getLimitCount(proType);
if (concuLimit >= 0) {
synchronized (o) {
int currentCount = getConcurrencyCount(proType);
if (currentCount >= concuLimit) {
//System.out.println(proType + "debug 最大线程数:"+concuLimit +" 当前线程数:"+currentCount);
DevLog.debug(proType + "debug 最大线程数:"+concuLimit +" 当前线程数:"+currentCount);
ErrorLogger.log(
"isOverLimit",
Level.SEVERE,
"ConcurrencyControl",
"isOverLimit",
"",
new Exception(proType + " 调用并发数已到最大值"),
ErrorLogger.saveParameters(new Object[] {
proType, concuLimit, currentCount }));
return true;
}
addConcurrencyControl(proType);
}
}
return false;
}

/**
* 释放一个线程
* @param proType
*/
public void releaseThread(String proType, Object o){
int concuLimit=0;
if(StringUtils.isEmpty(proType)){
concuLimit = -1;
}else{
concuLimit = getLimitCount(proType);
}

if (concuLimit >= 0){
synchronized (o) {
if(getConcurrencyCount(proType)>0){
decreaseConcurrencyControl(proType);
}
}
}
}

/**
* 设置已有线程数 增
* @param proType
*/
public void addConcurrencyControl(String proType) {
if(StringUtils.equals(ccfStrType, proType)){
this.countForCCF++;
}
if(StringUtils.equals(ilogStrType, proType)){
this.countForILOG++;
}
if(StringUtils.equals(taobaoRequest, proType)){
this.taobaoCounts++;
}

if(StringUtils.equals(ccfStrTypeITS, proType)){
this.countForCCFITS++;
}
if(StringUtils.equals(freyUndwRequest, proType)){
this.freyUndwCounts++;
}
}

/**
* 设置已有线程数 减
* @param proType
*/
public void decreaseConcurrencyControl(String proType) {
if(StringUtils.equals(ccfStrType, proType)){
this.countForCCF--;
}
if(StringUtils.equals(ilogStrType, proType)){
this.countForILOG--;
}
if(StringUtils.equals(taobaoRequest, proType)){
this.taobaoCounts--;
}
if(StringUtils.equals(ccfStrTypeITS, proType)){
this.countForCCFITS--;
}
if(StringUtils.equals(freyUndwRequest, proType)){
this.freyUndwCounts--;
}
}

/**
* 构造并发控制对象
*/
public ConcurrencyControl() {
synObjMap.put(ccfStrTypeITS, ccfObjITS);
synObjMap.put(ccfStrType, ccfObj);
synObjMap.put(ilogStrType, ilogObj);
synObjMap.put(taobaoRequest, requestObj);
synObjMap.put(freyUndwRequest, freyUndw);}

/**
* 获取指定的对象
* @param proType
* @return
*/
public Object getSynObjectByProType(String proType){
if(StringUtils.isNotEmpty(proType)){
return this.getSynObjMap().get(proType);
}
return new Object();
}

/**
* 获取当前线程数
* @param proType
* @return
*/
public int getConcurrencyCount(String proType) {
if(StringUtils.isNotEmpty(proType)){
return (Integer) this.getCountMap().get(proType);
}
return 0;
}

/**
* 获取最大线程数
* @param proType
* @return
*/
public int getLimitCount(String proType){
String threadNum = UWSPropertiesUtils.getPropertyValues(this.preConfStr+proType);
int limitCount = StringUtils.isBlank(threadNum) ? 5 : Integer.parseInt(threadNum);
return limitCount;
}

public Map<String, Object> getSynObjMap() {
return synObjMap;
}

public void setSynObjMap(Map<String, Object> synObjMap) {
this.synObjMap = synObjMap;
}

/**
* 获取线程控制map
* @return
*/
public Map<String, Object> getCountMap() {
countMap.put(ccfStrTypeITS, countForCCFITS);
countMap.put(ccfStrType, countForCCF);
countMap.put(ilogStrType, countForILOG);
countMap.put(taobaoRequest, taobaoCounts);
countMap.put(freyUndwRequest, freyUndwCounts);
return countMap;
}

public void setCountMap(Map<String, Object> countMap) {
this.countMap = countMap;
}

public int getCountForCCF() {
return countForCCF;
}

public void setCountForCCF(int countForCCF) {
this.countForCCF = countForCCF;
}

public Object getCcfObj() {
return ccfObj;
}

public void setCcfObj(Object ccfObj) {
this.ccfObj = ccfObj;
}

public String getCcfStrType() {
return ccfStrType;
}

public String getIlogStrType() {
return ilogStrType;
}

public void setIlogStrType(String ilogStrType) {
this.ilogStrType = ilogStrType;
}

public int getCountForILOG() {
return countForILOG;
}

public void setCountForILOG(int countForILOG) {
this.countForILOG = countForILOG;
}

public Object getIlogObj() {
return ilogObj;
}

public void setIlogObj(Object ilogObj) {
this.ilogObj = ilogObj;
}

public String getTaobaoRequest() {
return taobaoRequest;
}

public void setTaobaoRequest(String taobaoRequest) {
this.taobaoRequest = taobaoRequest;
}

public int getTaobaoCounts() {
return taobaoCounts;
}

public void setTaobaoCounts(int taobaoCounts) {
this.taobaoCounts = taobaoCounts;
}

public Object getRequestObj() {
return requestObj;
}

public void setRequestObj(Object requestObj) {
this.requestObj = requestObj;
}

public int getCountForCCFITS() {
return countForCCFITS;
}

public void setCountForCCFITS(int countForCCFITS) {
this.countForCCFITS = countForCCFITS;
}

public Object getCcfObjITS() {
return ccfObjITS;
}

public void setCcfObjITS(Object ccfObjITS) {
this.ccfObjITS = ccfObjITS;
}

public String getCcfStrTypeITS() {
return ccfStrTypeITS;
}
public String getFreyUndwRequest() {
return freyUndwRequest;
}

public void setFreyUndwRequest(String freyUndwRequest) {
this.freyUndwRequest = freyUndwRequest;
}

public int getFreyUndwCounts() {
return freyUndwCounts;
}

public void setFreyUndwCounts(int freyUndwCounts) {
this.freyUndwCounts = freyUndwCounts;
}

public Object getFreyUndw() {
return freyUndw;
}

public void setFreyUndw(Object freyUndw) {
this.freyUndw = freyUndw;
}
}

猜你喜欢

转载自javagongchengshi.iteye.com/blog/2401150