JAVA流控及超流控后的延迟处理实例

 更新时间:2014年12月31日 09:42:42   投稿:shichen2014  
这篇文章主要介绍了JAVA流控及超流控后的延迟处理,以实例形式较为详细的分析了Java进行流量控制的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JAVA流控及超流控后的延迟处理方法。分享给大家供大家参考。具体实现方法如下:

流控检查(每半秒累计,因此最小留空阀值只能做到每秒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; // 该条链路上发送的数量
 
    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;
        }
    }
 
    /**
     * 获取指定时间的毫秒数
     */
    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
)

Java下需实现一个简单的延迟队列:

复制代码 代码如下:
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.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();
    }
}

希望本文所述对大家的Java程序设计有所帮助。

相关文章

  • 15道非常经典的Java面试题 附详细答案

    15道非常经典的Java面试题 附详细答案

    这篇文章主要为大家推荐了15道非常经典的Java面试题,附详细答案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • java实现文件的断点续传

    java实现文件的断点续传

    这篇文章主要为大家详细介绍了java实现文件的断点续传,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Java字符串转时间简单示例代码

    Java字符串转时间简单示例代码

    这篇文章主要给大家介绍了关于Java字符串转时间的相关资料,在Java中字符和字符串常常需要相互转化,文中通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-08-08
  • SpringBoot集成ActiveMQ的实战全过程

    SpringBoot集成ActiveMQ的实战全过程

    消息队列中间件是分布式系统中重要的组件,主要解决应用耦合、异步消息、流量削锋等问题,实现高性能、高可用、可伸缩和最终一致性架构,是大型分布式系统不可缺少的中间件,这篇文章主要给大家介绍了关于SpringBoot集成ActiveMQ的相关资料,需要的朋友可以参考下
    2021-11-11
  • Java实战项目之校园跑腿管理系统的实现

    Java实战项目之校园跑腿管理系统的实现

    只有理论是不够的,只有在实战中才能获得能力的提升,本篇文章手把手带你用java+Springboot+vue+maven+elementui+mysql实现一个校园跑腿管理系统,大家可以在过程中查缺补漏,提升水平
    2022-01-01
  • Springboot的maven间接依赖的实现

    Springboot的maven间接依赖的实现

    这篇文章主要介绍了Springboot的maven间接依赖的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • 浅谈将JNI库打包入jar文件

    浅谈将JNI库打包入jar文件

    这篇文章主要介绍了浅谈将JNI库打包入jar文件,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • 浅析如何将多个SpringBoot项目打包到一个Docker容器中

    浅析如何将多个SpringBoot项目打包到一个Docker容器中

    在现代软件开发中,Docker已成为一种流行的容器化技术,能够简化应用的部署和管理,本文将详细介绍如何将多个Spring Boot项目打包到一个Docker容器中,希望对大家有所帮助
    2024-10-10
  • Java数据结构之List的使用总结

    Java数据结构之List的使用总结

    List是Java中比较常用的集合类,指一系列存储数据的接口和类,可以解决复杂的数据存储问题,本文就来拿实际案例总结介绍一下List的使用方法,感兴趣的朋友快来看看吧
    2021-11-11
  • JAVA中通过Hibernate-Validation进行参数验证

    JAVA中通过Hibernate-Validation进行参数验证

    这篇文章主要介绍了JAVA中通过Hibernate-Validation进行参数验证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04

最新评论