JAVA流控及超流控后的延遲處理實(shí)例
本文實(shí)例講述了JAVA流控及超流控后的延遲處理方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
流控檢查(每半秒累計(jì),因此最小留空閥值只能做到每秒2條):
import java.util.Date;
import java.lang.Thread;
/**
* 流量控制
*
* @author chenx
*/
public class OverflowController {
private int maxSendCountPerSecend; // 該條鏈路上流控閥值
private Date sendTime = new Date();
private int sendCount = 0; // 該條鏈路上發(fā)送的數(shù)量
public OverflowController(int maxSendCountPerSecend) {
if (maxSendCountPerSecend < 2) {
maxSendCountPerSecend = 2;
}
this.maxSendCountPerSecend = maxSendCountPerSecend;
}
public int getMaxSendCountPerSecend() {
if (getMilliseconds(new Date()) >= 500) {
return maxSendCountPerSecend / 2;
}
return maxSendCountPerSecend - (maxSendCountPerSecend / 2);
}
/**
* 是否超流控
*/
public boolean isOverflow(int sendNum) {
synchronized (this) {
Date now = new Date();
if (now.getTime() - sendTime.getTime() >= 500) {
sendTime = now;
sendCount = sendNum;
} else {
if (sendCount + sendNum > getMaxSendCountPerSecend()) {
return true;
} else {
sendCount += sendNum;
}
}
return false;
}
}
/**
* 獲取指定時(shí)間的毫秒數(shù)
*/
private int getMilliseconds(Date date) {
SimpleDateFormat df = new SimpleDateFormat("SSS");
return Integer.valueOf(df.format(date));
}
public static void main(String[] args) throws InterruptedException {
OverflowController oc = new OverflowController(50);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
for (int i = 0; i <= 100; i++) {
if (oc.isOverflow(1)) {
System.out.println(i + "-isOverflow-" + df.format(new Date()));
} else {
System.out.println(i + "-sendOk-" + df.format(new Date()));
}
Thread.sleep(10);
}
}
}
超流控后的延遲處理,由于java中沒有.net的“延遲委托”一說:
WaitHandle waitObject,
WaitOrTimerCallback callBack,
Object state,
int millisecondsTimeOutInterval,
bool executeOnlyOnce
)
Java下需實(shí)現(xiàn)一個(gè)簡(jiǎn)單的延遲隊(duì)列:
import java.util.concurrent.TimeUnit;
public class DelayEntry implements Delayed {
private int count;
private long dequeuedTimeMillis; // 出隊(duì)列時(shí)間
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public long getDequeuedTimeMillis() {
return dequeuedTimeMillis;
}
public DelayEntry(long delayMillis) {
dequeuedTimeMillis = System.currentTimeMillis() + delayMillis;
}
@Override
public int compareTo(Delayed o) {
DelayEntry de = (DelayEntry) o;
long timeout = dequeuedTimeMillis - de.dequeuedTimeMillis;
return timeout > 0 ? 1 : timeout < 0 ? -1 : 0;
}
@Override
public long getDelay(TimeUnit unit) {
return dequeuedTimeMillis - System.currentTimeMillis();
}
}
public class DelayService {
public void run() {
DelayQueue<DelayEntry> queue = new DelayQueue<DelayEntry>();
DelayConsumer delayConsumer = new DelayConsumer(queue);
delayConsumer.start();
for (int i = 0; i < 100; i++) {
DelayEntry de = new DelayEntry(5000);
de.setCount(i);
System.out.println(System.currentTimeMillis() + "--------" + de.getCount());
queue.add(de);
}
}
class DelayConsumer extends Thread {
DelayQueue<DelayEntry> queue;
public DelayConsumer(DelayQueue<DelayEntry> queue) {
this.queue = queue;
}
public void run() {
while (true) {
try {
DelayEntry de = queue.take();
System.out.println("queue size=" + queue.size());
System.out.println(de.getCount());
System.out.println(System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
DelayService ds = new DelayService();
ds.run();
}
}
希望本文所述對(duì)大家的Java程序設(shè)計(jì)有所幫助。
相關(guān)文章
15道非常經(jīng)典的Java面試題 附詳細(xì)答案
這篇文章主要為大家推薦了15道非常經(jīng)典的Java面試題,附詳細(xì)答案,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Java字符串轉(zhuǎn)時(shí)間簡(jiǎn)單示例代碼
這篇文章主要給大家介紹了關(guān)于Java字符串轉(zhuǎn)時(shí)間的相關(guān)資料,在Java中字符和字符串常常需要相互轉(zhuǎn)化,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
SpringBoot集成ActiveMQ的實(shí)戰(zhàn)全過程
消息隊(duì)列中間件是分布式系統(tǒng)中重要的組件,主要解決應(yīng)用耦合、異步消息、流量削鋒等問題,實(shí)現(xiàn)高性能、高可用、可伸縮和最終一致性架構(gòu),是大型分布式系統(tǒng)不可缺少的中間件,這篇文章主要給大家介紹了關(guān)于SpringBoot集成ActiveMQ的相關(guān)資料,需要的朋友可以參考下2021-11-11
Java實(shí)戰(zhàn)項(xiàng)目之校園跑腿管理系統(tǒng)的實(shí)現(xiàn)
只有理論是不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+vue+maven+elementui+mysql實(shí)現(xiàn)一個(gè)校園跑腿管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01
Springboot的maven間接依賴的實(shí)現(xiàn)
這篇文章主要介紹了Springboot的maven間接依賴的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
淺析如何將多個(gè)SpringBoot項(xiàng)目打包到一個(gè)Docker容器中
在現(xiàn)代軟件開發(fā)中,Docker已成為一種流行的容器化技術(shù),能夠簡(jiǎn)化應(yīng)用的部署和管理,本文將詳細(xì)介紹如何將多個(gè)Spring Boot項(xiàng)目打包到一個(gè)Docker容器中,希望對(duì)大家有所幫助2024-10-10
Java數(shù)據(jù)結(jié)構(gòu)之List的使用總結(jié)
List是Java中比較常用的集合類,指一系列存儲(chǔ)數(shù)據(jù)的接口和類,可以解決復(fù)雜的數(shù)據(jù)存儲(chǔ)問題,本文就來拿實(shí)際案例總結(jié)介紹一下List的使用方法,感興趣的朋友快來看看吧2021-11-11
JAVA中通過Hibernate-Validation進(jìn)行參數(shù)驗(yàn)證
這篇文章主要介紹了JAVA中通過Hibernate-Validation進(jìn)行參數(shù)驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

