JAVA流控及超流控后的延遲處理實例
更新時間:2014年12月31日 09:42:42 投稿:shichen2014
這篇文章主要介紹了JAVA流控及超流控后的延遲處理,以實例形式較為詳細的分析了Java進行流量控制的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JAVA流控及超流控后的延遲處理方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
流控檢查(每半秒累計,因此最小留空閥值只能做到每秒2條):
復制代碼 代碼如下:
import java.text.SimpleDateFormat;
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ù)
*/
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);
}
}
}
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ù)
*/
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的“延遲委托”一說:
復制代碼 代碼如下:
ThreadPool.RegisterWaitForSingleObject(
WaitHandle waitObject,
WaitOrTimerCallback callBack,
Object state,
int millisecondsTimeOutInterval,
bool executeOnlyOnce
)
WaitHandle waitObject,
WaitOrTimerCallback callBack,
Object state,
int millisecondsTimeOutInterval,
bool executeOnlyOnce
)
Java下需實現(xiàn)一個簡單的延遲隊列:
復制代碼 代碼如下:
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class DelayEntry implements Delayed {
private int count;
private long dequeuedTimeMillis; // 出隊列時間
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();
}
}
import java.util.concurrent.TimeUnit;
public class DelayEntry implements Delayed {
private int count;
private long dequeuedTimeMillis; // 出隊列時間
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();
}
}
復制代碼 代碼如下:
import java.util.concurrent.DelayQueue;
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();
}
}
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();
}
}
希望本文所述對大家的Java程序設(shè)計有所幫助。
相關(guān)文章
SpringBoot集成ActiveMQ的實戰(zhàn)全過程
消息隊列中間件是分布式系統(tǒng)中重要的組件,主要解決應用耦合、異步消息、流量削鋒等問題,實現(xiàn)高性能、高可用、可伸縮和最終一致性架構(gòu),是大型分布式系統(tǒng)不可缺少的中間件,這篇文章主要給大家介紹了關(guān)于SpringBoot集成ActiveMQ的相關(guān)資料,需要的朋友可以參考下2021-11-11
Java實戰(zhàn)項目之校園跑腿管理系統(tǒng)的實現(xiàn)
只有理論是不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+vue+maven+elementui+mysql實現(xiàn)一個校園跑腿管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01
淺析如何將多個SpringBoot項目打包到一個Docker容器中
在現(xiàn)代軟件開發(fā)中,Docker已成為一種流行的容器化技術(shù),能夠簡化應用的部署和管理,本文將詳細介紹如何將多個Spring Boot項目打包到一個Docker容器中,希望對大家有所幫助2024-10-10
Java數(shù)據(jù)結(jié)構(gòu)之List的使用總結(jié)
List是Java中比較常用的集合類,指一系列存儲數(shù)據(jù)的接口和類,可以解決復雜的數(shù)據(jù)存儲問題,本文就來拿實際案例總結(jié)介紹一下List的使用方法,感興趣的朋友快來看看吧2021-11-11
JAVA中通過Hibernate-Validation進行參數(shù)驗證
這篇文章主要介紹了JAVA中通過Hibernate-Validation進行參數(shù)驗證,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

