基于数组阻塞队列 ArrayBlockingQueue 的一个队列工具类
java语言基于ArrayBlockingQueue 开发的一个根据特定前缀和后缀的队列。每天自动循环生成。
1.定义队列基类 Cookie
package com.bytter.util.queue; import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue; public class Cookie {
private ArrayBlockingQueue<String> queue;
private int cursor = 1;
private Date date = new Date();
private int maxPoolQueue = 500;
private int mixPoolQueue = 100; public Cookie(int maxPoolQueue,int mixPoolQueue,Date date){
this.maxPoolQueue=maxPoolQueue;
this.mixPoolQueue=mixPoolQueue;
this.date=date;
this.queue = new ArrayBlockingQueue<String>(this.maxPoolQueue,true);
} public Cookie(int maxPoolQueue,int mixPoolQueue,Date date, int cursor){
this.maxPoolQueue = maxPoolQueue;
this.mixPoolQueue = mixPoolQueue;
this.date=date;
this.cursor=cursor;
this.queue = new ArrayBlockingQueue<String>(this.maxPoolQueue,true);
} public ArrayBlockingQueue<String> getQueue() {
return queue;
} public Cookie setQueue(ArrayBlockingQueue<String> queue) {
this.queue = queue;
return this;
} public int getCursor() {
return cursor;
} public Cookie setCursor(int cursor) {
this.cursor = cursor;
return this;
} public Date getDate() {
return date;
} public Cookie setDate(Date date) {
this.date = date;
return this;
} public int getMaxPoolQueue() {
return maxPoolQueue;
} public Cookie setMaxPoolQueue(int maxPoolQueue) {
this.maxPoolQueue = maxPoolQueue;
return this;
} public int getMixPoolQueue() {
return mixPoolQueue;
} public Cookie setMixPoolQueue(int mixPoolQueue) {
this.mixPoolQueue = mixPoolQueue;
return this;
} }
2.QueueUtil 队列工具类,用于获取队列中的值(主要是获取付款的单号==)
package com.bytter.util.queue; import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import com.bytter.framework.persistence.dao.IBaseDAO;
import com.bytter.util.AppContextUtil;
import com.bytter.util.DateUtil;
/**
*
* @author
* May 6, 2019 10:35:39 AM
* @version
*/
public class QueueUtil{ private static Logger log = LogManager.getLogger(QueueUtil.class);
protected static IBaseDAO baseDao = null;
private static ConcurrentHashMap<String, Cookie> queueMap = new ConcurrentHashMap<String, Cookie>(); private static class SingletonClassInstance{
private static final QueueUtil instance=new QueueUtil();
} private QueueUtil(){} public static QueueUtil getInstance(){
return SingletonClassInstance.instance;
} /**
* 调用事例: QueueUtil.getInstance().getSequence(SequenceEnum.BIS_EXC)
* @param sequenceEnum
* @return
* @throws InterruptedException
*/
@SuppressWarnings("unchecked")
public String getSequence(SequenceEnum sequenceEnum) throws InterruptedException{
String value = "";
String key = sequenceEnum.getTableName()+"_"+sequenceEnum.getFieldName();
Cookie cookie = queueMap.get(key);
value = cookie.getQueue().take();
String returnValue = sequenceEnum.getPrefix()+DateUtil.dateToStr(sequenceEnum.getDateFormat(), new Date());
for (int i = 0; i < sequenceEnum.getSequenceLength()-value.length(); i++) {
returnValue += "0";
}
returnValue += value;
log.info("取出的单号:"+returnValue);
return returnValue;
} public void initQueue() {
putAllMap();
Thread thread = new Produce(queueMap);
thread.setName("QueueUtil_Produce");
thread.start();
} private static void putAllMap() {
queueMap.clear();
int curor = 1;
log.info("-----开始加载队列 -------");
for (SequenceEnum sequenceEnum : SequenceEnum.values()){
String key = sequenceEnum.getTableName()+"_"+sequenceEnum.getFieldName();
//同一天但是系统重启或者宕机的情况时,游标重置为1.此时需要查询表中最大的值并且置游标值+1
String sql="SELECT MAX("+sequenceEnum.getFieldName()+") FROM "+sequenceEnum.getTableName()+" WHERE "+sequenceEnum.getFieldName()+" LIKE '"+sequenceEnum.getPrefix()+"%'";
List list = baseDao.search_sql(null, sql, "", "", null);
if(list != null && list.size() >0 && list.get(0) != null){
String no = list.get(0).toString();
//单号,自增。
String count = no.substring(no.length()-sequenceEnum.getSequenceLength());
String date = no.replaceAll(count, "").replaceAll(sequenceEnum.getPrefix(), "");
/*if(date.equals(DateUtil.dateToStr(sequenceEnum.getDateFormat(), new Date()))){
curor = Integer.parseInt(count) + 1;
}*/
}
log.info("加载队列 :table=="+sequenceEnum.getTableName()+" field=="+sequenceEnum.getFieldName()+" 游标=="+curor);
queueMap.put(key, new Cookie(sequenceEnum.getMaxSeqSize(),sequenceEnum.getMinSeqSize(),new Date(),curor));
}
log.info("-----队列加载完毕 -------");
} static{
baseDao = AppContextUtil.getBean("baseDao");
//第一次调用时开启队列
//initQueue();
} }
3.生产者线程类
package com.bytter.util.queue; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap; import com.bytter.util.DateUtil; public class Produce extends Thread{
private ConcurrentHashMap<String, Cookie> queueMap;
public Produce(ConcurrentHashMap<String, Cookie> queueMap){
this.queueMap=queueMap;
}
public void run(){
ArrayBlockingQueue<String> queue;
int mixSize;
int curor;
Cookie cookie;
Date date;
Map.Entry<String,Cookie> entry;
Iterator<Map.Entry<String,Cookie>> iteratorMap;
while (true) {
iteratorMap = queueMap.entrySet().iterator();
while (iteratorMap.hasNext()){
entry = iteratorMap.next();
cookie = entry.getValue();
queue = cookie.getQueue();
mixSize = cookie.getMixPoolQueue();
curor = cookie.getCursor();
date = cookie.getDate();
//新的一天,游标开始重置
if(!DateUtil.dateToStr("yyyy-MM-dd", new Date()).equals(DateUtil.dateToStr("yyyy-MM-dd", date))){
curor = 1;
queue.clear();
date = new Date();
System.out.println("重置游标时间===="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
} //offer 在填满后返回false
if(queue.size() < mixSize){
while (queue.offer(curor + "")) {
curor++;
}
}
cookie.setQueue(queue).setCursor(curor).setDate(date);
queueMap.put(entry.getKey(),cookie);
}
}
}
}
4.自定义的生成单号的规则枚举类
package com.bytter.util.queue;
public enum SequenceEnum{
/**
* 付款指令编码 yyMMdd000001
* @return
*/
BIS_EXC("",6, "BIS_EXC", "VOUCHER_NO", "yyMMdd",500,100),
/**
* 付款单号
* @return
*/
PAYMENT_BILL_NO("P",7, "T_BIS_PAYMENT_INFO", "BILL_CODE", "yyMMddHHmmss",500,100), ; /**
* 流水号前缀
*/
private String prefix; /**
* 流水号数字部分长度
*/
private Integer sequenceLength;
/**
* 业务表名
*/
private String tableName;
/**
* 业务表中的字段名
*/
private String fieldName;
/**
* 日期格式化模板
*/
private String dateFormat; /**
* 队列最大的长度
*/
private int maxSeqSize;
/**
* 队列最小长度
*/
private int minSeqSize; /**
* @param prefix 流水号前缀
* @param sequenceLength 流水号自增长长度
* @param tableName 表名
* @param fieldName 字段名
* @param dateFormat 日期格式化模板
*/
SequenceEnum(String prefix, Integer sequenceLength, String tableName, String fieldName,String dateFormat,int maxSeqSize,int minSeqSize) {
this.prefix = prefix;
this.sequenceLength = sequenceLength;
this.tableName = tableName;
this.fieldName = fieldName;
this.dateFormat = dateFormat;
this.maxSeqSize = maxSeqSize;
this.minSeqSize = minSeqSize;
} /**
* @param prefix 流水号前缀
* @param sequenceLength 流水号自增长长度
* @param tableName 表名
* @param fieldName 字段名
* @param dateFormat 日期格式化模板
*/
SequenceEnum(Integer sequenceLength, String tableName, String fieldName,String dateFormat) {
this.sequenceLength = sequenceLength;
this.tableName = tableName;
this.fieldName = fieldName;
this.dateFormat = dateFormat;
} public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public Integer getSequenceLength() {
return sequenceLength;
}
public void setSequenceLength(Integer sequenceLength) {
this.sequenceLength = sequenceLength;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getFieldName() {
return fieldName;
}
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
} public String getDateFormat() {
return dateFormat;
} public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
} public int getMaxSeqSize() {
return maxSeqSize;
} public void setMaxSeqSize(int maxSeqSize) {
this.maxSeqSize = maxSeqSize;
} public int getMinSeqSize() {
return minSeqSize;
} public void setMinSeqSize(int minSeqSize) {
this.minSeqSize = minSeqSize;
} }
5.测试相关类
1> 消费者线程
package com.bytter.util.queue;
public class 	 implements Runnable{
    public void run(){
		try {
			for (int i = 0; i < 200; i++) {
				Thread.sleep(Long.valueOf((long) (Math.random()*10*300)));
//				System.out.println(Thread.currentThread().getName());
				System.out.println(Thread.currentThread().getName()+"==="+SequenceUtilTest.getInstance().getSequence(SequenceEnum.BIS_EXC));
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
    }
}
2>测试main 方法
package com.bytter.util.queue;
public class tests {
	public static void main(String[] args) throws InterruptedException {
		new Thread(new Consume()).start();
		new Thread(new Consume()).start();
		new Thread(new Consume()).start();
		new Thread(new Consume()).start();
		new Thread(new Consume()).start();
//		new Thread(new Consume()).start();
//		new Thread(new Consume()).start();
//		new Thread(new Consume()).start();
//		new Thread(new Consume()).start();
//		new Thread(new Consume()).start();
	}
}
基于数组阻塞队列 ArrayBlockingQueue 的一个队列工具类的更多相关文章
- Go/Python/Erlang编程语言对比分析及示例          基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池)   封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil   分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
		
Go/Python/Erlang编程语言对比分析及示例 本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...
 - 编写Java程序,创建一个数学工具类,将该类设计为final类,Final 修饰符的使用。
		
返回本章节 返回作业目录 需求说明: 创建一个数学工具类. 将该类设计为final类. 将该类的构造方法的访问权限定义为私有,以防止外界实例化该类. 在该类定义静态double类型常量π,其值为3.1 ...
 - 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
		
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; usi ...
 - 分享一个Snackbar工具类 SnackbarUtils;
		
分享一个Snackbar工具类,源代码也是在Github上面找的,自己做了一下修改: 功能如下: 1:设置Snackbar显示时间长短 1.1:Snackbar.LEN ...
 - java中定义一个CloneUtil 工具类
		
其实所有的java对象都可以具备克隆能力,只是因为在基础类Object中被设定成了一个保留方法(protected),要想真正拥有克隆的能力, 就需要实现Cloneable接口,重写clone方法.通 ...
 - 手写一个LRU工具类
		
LRU概述 LRU算法,即最近最少使用算法.其使用场景非常广泛,像我们日常用的手机的后台应用展示,软件的复制粘贴板等. 本文将基于算法思想手写一个具有LRU算法功能的Java工具类. 结构设计 在插入 ...
 - [分享]一个String工具类,也许你的项目中会用得到
		
每次做项目都会遇到字符串的处理,每次都会去写一个StringUtil,完成一些功能. 但其实每次要的功能都差不多: 1.判断类(包括NULL和空串.是否是空白字符串等) 2.默认值 3.去空白(tri ...
 - 调用CMD命令的一个.NET工具类(MyWindowsCmd)
		
功能大概描述一下如果直接StandardOutput.ReadToEnd()这种方法,有很多限制 这类方式必须把命令全部执行一次写入并标记为exit,而且返回内容的获取会一直等待,如果在主线程里使用会 ...
 - 分享一个FileUtil工具类,基本满足web开发中的文件上传,单个文件下载,多个文件下载的需求
		
获取该FileUtil工具类具体演示,公众号内回复fileutil20200501即可. package com.example.demo.util; import javax.servlet.htt ...
 
随机推荐
- 修改python pip3镜像源
			
方法一: pip3 install 包名 -i 镜像源url 主要的镜像源: pip3 install tornado -i https://pypi.douban.com/simple/ pip ...
 - homebrew学习(四)之取消homebrew自动更新
			
homebrew自动更新 使用brew install /brew cask install安装软件总是先updating HomeBrew…,速度很慢 取消homebrew自动更新 方法一:使用命令 ...
 - ubuntu中apache的ssl证书配置及url重写
			
一.https原理 借用网上的图(图片来源: https://www.cnblogs.com/xiohao/p/9054355.html ),用到了对称加密和非对称加密. 二.ubuntu的ap ...
 - Assets.xcassets的详细使用方法
			
开始之前,首先回顾一下iOS7初体验(1)——第一个应用程序HelloWorld中的一张图,如下所示: 本文分享一下Images.xcassets的体验~_~ 1. 打开此前使用过的HelloWorl ...
 - MathType 7.4.2.480
			
目录 1. 相关推荐 2. 按 3. 软件介绍 4. 安装步骤 5. 使用说明 6. 下载地址 1. 相关推荐 推荐使用:AxMath(AxMath可以与LaTeX进行交互):https://blog ...
 - 微软宣布全新命令行+脚本工具:PowerShell 7
			
DOS 逐渐退出历史舞台后,Windows 一直内置着 CMD 命令行工具,并在 Windows 7 时代升级为更强悍的 PowerShell,不仅可以执行命令行,更可以执行各种高级脚本,还能跨平台. ...
 - VS编译器问题总结
			
error C2236: 意外的“class”“CTsgBaseTask”.是否忘记了“;”? 出现这个问题的原因是在引用的一个头文件中定义的一个类最后没有加分号";".
 - 三、Signalr外部链接
			
一.本地外部引用 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
 - 解决deepin没有ll等命令的办法
			
编辑~/.bashrc, 添加alias 如下 vim ~/.bashrc 设置别名. 添加如下行 alias ll='ls -alF' alias la='ls -A' alias vi='vim' ...
 - pycharm 的一个小问题
			
版本:PyCharm 2018.3.7 (Professional Edition) 这段时间用pycharm写python代码,运行网上copy的代码.报错了也就是少个模块或者Python2的语法在 ...