Java serial number generator
Ask a question
How to use jAVA to generate serial numbers while supporting configurability and high concurrency???
Solve the problem
Assume that your project has integrated caching technology
If you have a certain Java foundation
If...
The following code implements a serial number generator that supports high concurrency, configurability, and high efficiency. For use by multiple modules of a project at the same time, serial numbers support caching, that is, a certain number of serial numbers will be pre-generated and stored in the cache each time. When needed, they will be sent to the cache first. After the serial numbers in the cache are used, Regenerate a certain number of serial numbers and put them in the cache, and cycle like this to improve efficiency...
At the same time, the serial number generator is thread-safe and protected by thread locks. It has been truly invested in the project. Use...
Database table design
[code]CREATE TABLE sys_serial_number2 ( "id" varchar(32) COLLATE "default" NOT NULL, "module_name" varchar(50) COLLATE "default", "module_code" varchar(50) COLLATE "default", "config_templet" varchar(50) COLLATE "default", "max_serial" varchar(32) COLLATE "default", "pre_max_num" varchar(32) COLLATE "default", "is_auto_increment" char(1) COLLATE "default" )
Instructions:
[code]module_name:模块名称 module_code:模块编码 config_templet:当前模块 使用的序列号模板 max_serial:存放当前序列号的值 pre_max_num:预生成序列号存放到缓存的个数 is_auto_increment:是否自动增长模式,0:否 1:是
Note: Currently the serial number template only supports letters, dynamic numbers (0000 represents 1-9999), and dates Use the combination form of ${DATE}
is_auto_increment is configured as 1, then the serial number generated by the configuration template for CX000000 is: CX1, CX2, CX3...
is configured as 0, this The serial numbers generated when the configuration template is CX0000000 are: CX00000001, CX00000002, CX00000003
Database configuration instructions: If you need the project number of the project module, you need to configure a record in the database table sys_serial_number:
[code]| id | module_name | module_code | config_templet | max_serial | pre_max_num | is_auto_increment |-------|--------------|--------------|-----------------|-------------|-------------|--------------------/ | xxxx | 项目 | PJ |CX00000000${DATE}| 2650 | 100 | 1
CX00000000${DATE} The generated serial number is similar to: CX0000000120160522, CX0000000220160522, CX0000000320160522...
Serial number model entity design:
[code]package com.evada.de.serialnum.model; import com.evada.de.common.model.BaseModel; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; /** * 功能描述:序列号表模型 * * @author :Ay 2015/11/23 */ @Entity @Table(name="sys_serial_number") public class SystemSerialNumber extends BaseModel { /** * 模块名称 */ @Column(name = "module_name", columnDefinition = "VARCHAR") private String moduleName; /** * 模块编码 */ @Column(name = "module_code", columnDefinition = "VARCHAR") private String moduleCode; /** * 流水号配置模板 */ @Column(name = "config_templet", columnDefinition = "VARCHAR") private String configTemplet; /** * 序列号最大值 */ @Column(name = "max_serial", columnDefinition = "VARCHAR") private String maxSerial; /** * 是否自动增长标示 */ @Column(name = "is_auto_increment", columnDefinition = "VARCHAR") private String isAutoIncrement; public String getIsAutoIncrement() { return isAutoIncrement; } public void setIsAutoIncrement(String isAutoIncrement) { this.isAutoIncrement = isAutoIncrement; } /** * 预生成流水号数量 */ @Column(name = "pre_max_num", columnDefinition = "VARCHAR") private String preMaxNum; public String getPreMaxNum() { return preMaxNum; } public void setPreMaxNum(String preMaxNum) { this.preMaxNum = preMaxNum; } public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } public String getModuleCode() { return moduleCode; } public void setModuleCode(String moduleCode) { this.moduleCode = moduleCode; } public String getConfigTemplet() { return configTemplet; } public void setConfigTemplet(String configTemplet) { this.configTemplet = configTemplet; } public String getMaxSerial() { return maxSerial; } public void setMaxSerial(String maxSerial) { this.maxSerial = maxSerial; } public SystemSerialNumber(String id){ this.id = id; } public SystemSerialNumber(String id,String moduleCode){ this.id = id; this.moduleCode = moduleCode; } public SystemSerialNumber(){} }
Service interface design:
[code]package com.evada.de.serialnum.service; import com.evada.de.serialnum.dto.SystemSerialNumberDTO; /** * 序列号service接口 * Created by huangwy on 2015/11/24. */ public interface ISerialNumService { public SystemSerialNumberDTO find(SystemSerialNumberDTO systemSerialNumberDTO); public String generateSerialNumberByModelCode(String moduleCode); /** * 设置最小值 * @param value 最小值,要求:大于等于零 * @return 流水号生成器实例 */ ISerialNumService setMin(int value); /** * 设置最大值 * @param value 最大值,要求:小于等于Long.MAX_VALUE ( 9223372036854775807 ) * @return 流水号生成器实例 */ ISerialNumService setMax(long value); /** * 设置预生成流水号数量 * @param count 预生成数量 * @return 流水号生成器实例 */ ISerialNumService setPrepare(int count); }
Service implementation:
[code]package com.evada.de.serialnum.service.impl; import com.evada.de.common.constants.SerialNumConstants; import com.evada.de.serialnum.dto.SystemSerialNumberDTO; import com.evada.de.serialnum.model.SystemSerialNumber; import com.evada.de.serialnum.repository.SerialNumberRepository; import com.evada.de.serialnum.repository.mybatis.SerialNumberDAO; import com.evada.de.serialnum.service.ISerialNumService; import com.evada.inno.common.util.BeanUtils; import com.evada.inno.common.util.DateUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CachePut; import org.springframework.stereotype.Service; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.locks.ReentrantLock; /** * Created by Ay on 2015/11/24. */ @Service("serialNumberService") public class SerialNumberServiceImpl implements ISerialNumService { private static final Logger LOGGER = LoggerFactory.getLogger(SerialNumberServiceImpl.class); @Autowired private SerialNumberDAO serialNumberDAO; @Autowired private SerialNumberRepository serialNumberRepository; /** 格式 */ private String pattern = ""; /** 生成器锁 */ private final ReentrantLock lock = new ReentrantLock(); /** 流水号格式化器 */ private DecimalFormat format = null; /** 预生成锁 */ private final ReentrantLock prepareLock = new ReentrantLock(); /** 最小值 */ private int min = 0; /** 最大值 */ private long max = 0; /** 已生成流水号(种子) */ private long seed = min; /** 预生成数量 */ private int prepare = 0; /** 数据库存储的当前最大序列号 **/ long maxSerialInt = 0; /** 当前序列号是否为个位数自增的模式 **/ private String isAutoIncrement = "0"; SystemSerialNumberDTO systemSerialNumberDTO = new SystemSerialNumberDTO(); /** 预生成流水号 */ HashMap<String,List<String>> prepareSerialNumberMap = new HashMap<>(); /** * 查询单条序列号配置信息 * @param systemSerialNumberDTO * @return */ @Override public SystemSerialNumberDTO find(SystemSerialNumberDTO systemSerialNumberDTO) { return serialNumberDAO.find(systemSerialNumberDTO); } /** * 根据模块code生成预数量的序列号存放到Map中 * @param moduleCode 模块code * @return */ @CachePut(value = "serialNumber",key="#moduleCode") public List<String> generatePrepareSerialNumbers(String moduleCode){ //临时List变量 List<String> resultList = new ArrayList<String>(prepare); lock.lock(); try{ for(int i=0;i<prepare;i++){ maxSerialInt = maxSerialInt + 1; if(maxSerialInt > min && (maxSerialInt + "").length() < max ){ seed = maxSerialInt ; }else{ //如果动态数字长度大于模板中的长度 例:模板CF000 maxSerialInt 1000 seed = maxSerialInt = 0; //更新数据,重置maxSerialInt为0 systemSerialNumberDTO.setMaxSerial("0"); SystemSerialNumber systemSerialNumber = new SystemSerialNumber(); BeanUtils.copyProperties(systemSerialNumber,systemSerialNumberDTO); serialNumberRepository.save(systemSerialNumber); } //动态数字生成 String formatSerialNum = format.format(seed); //动态日期的生成 if(pattern.contains(SerialNumConstants.DATE_SYMBOL)){ String currentDate = DateUtils.format(new Date(),"yyyyMMdd"); formatSerialNum = formatSerialNum.replace(SerialNumConstants.DATE_SYMBOL,currentDate); } resultList.add(formatSerialNum); } //更新数据 systemSerialNumberDTO.setMaxSerial(maxSerialInt + ""); SystemSerialNumber systemSerialNumber = new SystemSerialNumber(); BeanUtils.copyProperties(systemSerialNumber,systemSerialNumberDTO); serialNumberRepository.save(systemSerialNumber); }finally{ lock.unlock(); } return resultList; } /** * 根据模块code生成序列号 * @param moduleCode 模块code * @return 序列号 */ public String generateSerialNumberByModelCode(String moduleCode){ //预序列号加锁 prepareLock.lock(); try{ //判断内存中是否还有序列号 if(null != prepareSerialNumberMap.get(moduleCode) && prepareSerialNumberMap.get(moduleCode).size() > 0){ //若有,返回第一个,并删除 return prepareSerialNumberMap.get(moduleCode).remove(0); } }finally { //预序列号解锁 prepareLock.unlock(); } systemSerialNumberDTO = new SystemSerialNumberDTO(); systemSerialNumberDTO.setModuleCode(moduleCode); systemSerialNumberDTO = serialNumberDAO.find(systemSerialNumberDTO); prepare = Integer.parseInt(systemSerialNumberDTO.getPreMaxNum().trim());//预生成流水号数量 pattern = systemSerialNumberDTO.getConfigTemplet().trim();//配置模板 String maxSerial = systemSerialNumberDTO.getMaxSerial().trim(); //存储当前最大值 isAutoIncrement = systemSerialNumberDTO.getIsAutoIncrement().trim(); maxSerialInt = Long.parseLong(maxSerial.trim());//数据库存储的最大序列号 max = this.counter(pattern,'0') + 1;//根据模板判断当前序列号数字的最大值 if(isAutoIncrement.equals("1")){ pattern = pattern.replace("0","#"); } format = new DecimalFormat(pattern); //生成预序列号,存到缓存中 List<String> resultList = generatePrepareSerialNumbers(moduleCode); prepareLock.lock(); try { prepareSerialNumberMap.put(moduleCode, resultList); return prepareSerialNumberMap.get(moduleCode).remove(0); } finally { prepareLock.unlock(); } } /** * 设置最小值 * * @param value 最小值,要求:大于等于零 * @return 流水号生成器实例 */ public ISerialNumService setMin(int value) { lock.lock(); try { this.min = value; }finally { lock.unlock(); } return this; } /** * 最大值 * * @param value 最大值,要求:小于等于Long.MAX_VALUE ( 9223372036854775807 ) * @return 流水号生成器实例 */ public ISerialNumService setMax(long value) { lock.lock(); try { this.max = value; }finally { lock.unlock(); } return this; } /** * 设置预生成流水号数量 * @param count 预生成数量 * @return 流水号生成器实例 */ public ISerialNumService setPrepare(int count) { lock.lock(); try { this.prepare = count; }finally { lock.unlock(); } return this; } /** * 统计某一个字符出现的次数 * @param str 查找的字符 * @param c * @return */ private int counter(String str,char c){ int count=0; for(int i = 0;i < str.length();i++){ if(str.charAt(i)==c){ count++; } } return count; } }
The above is the content of the Java serial number generator. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Spring Boot simplifies the creation of robust, scalable, and production-ready Java applications, revolutionizing Java development. Its "convention over configuration" approach, inherent to the Spring ecosystem, minimizes manual setup, allo
