您的位置:首页 > 编程语言 > Java开发

java中生成流水号的一个例子(使用BerkeleyDB)

2014-07-09 14:35 381 查看
package com.jiaoyiping.berkeleydb;

import com.sleepycat.je.*;
import com.sleepycat.utilint.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
* Created with IntelliJ IDEA.
* User: 焦一平
* Date: 14-7-9
* Time: 下午1:27
* To change this template use File | Settings | File Templates.
* 1.直接存key和value
* 2.存储key和value组合成的对象(未使用)
*/
public class SerialNumberGenerator {
Logger logger = LoggerFactory.getLogger(SerialNumberGenerator.class);
private static int MAX_VALUE = 9999;
private static int MIN_VALUE = 1;

/**
* 对于任何的请求,总是先取出当前的值返回,然后再将数据库里的值加一
* @param key 业务主键
* @return 当前值
*/
public String getNextValue(String key){
String currentValue = this.readCurrentValueFromDatabase(key);
String result;
if (currentValue == null || "".equals(currentValue)){
result = this.formatString(MIN_VALUE);
this.instrtIntoDataBase(key,(MIN_VALUE+1)+"");
BerkeleyDBUtil.getDatabase().getEnvironment().sync();

}
else{
int intCurrentValue = Integer.parseInt(currentValue);
result = this.formatString(intCurrentValue);

//存储下一个值
String nextValue = "";
if (intCurrentValue == MAX_VALUE){
nextValue = MIN_VALUE+"";
}else {
nextValue = (intCurrentValue+1) +"";
}
this.instrtIntoDataBase(key,nextValue);
BerkeleyDBUtil.getDatabase().getEnvironment().sync();

}

return result;
}
public String readCurrentValueFromDatabase(String key){
DatabaseEntry theKey = new DatabaseEntry(StringUtils.toUTF8(key));
DatabaseEntry theValue = new DatabaseEntry();
TransactionConfig txnConfig = new TransactionConfig();
txnConfig.setSerializableIsolationVoid(true);
Database database = BerkeleyDBUtil.getDatabase();
//Transaction txn = database.getEnvironment().beginTransaction(null,txnConfig);
OperationStatus status = database.get(null,theKey,theValue,LockMode.DEFAULT);
//对应的键不存在
if (status == OperationStatus.KEYEMPTY){
return null;
}
else if (status == OperationStatus.SUCCESS){
byte[] data = theValue.getData();
String result;
try {
result = new String(data,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
result = "";
}
return result;
}

return null;
}

public void instrtIntoDataBase(String key,String value){
DatabaseEntry theKey = new DatabaseEntry(StringUtils.toUTF8(key));
DatabaseEntry theValue = new DatabaseEntry(StringUtils.toUTF8(value));
TransactionConfig txnConfig = new TransactionConfig();
txnConfig.setSerializableIsolationVoid(true);
Database database = BerkeleyDBUtil.getDatabase();
//Transaction txn = database.getEnvironment().beginTransaction(null,txnConfig);
OperationStatus status = database.put(null,theKey,theValue);
if (status == OperationStatus.SUCCESS){
logger.info("保存成功");
}
//        else if (status == OperationStatus.KEYEXIST){
//            logger.info("");
//        }

}

public String formatString(int input){
String result = "";
if(input > 1000){
result = input +"";
}else{
int length = (input+"").length();
if (length == 1){
result = "000"+input;
}
else if (length == 2){
result = "00"+input;
}else {
result = "0"+input;
}
}
return result;
}

public static void main(String[] args) {
SerialNumberGenerator generator = new SerialNumberGenerator();
String result = generator.getNextValue("jiao");
System.out.println("======"+result+"======");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: