update - 调整 ID 生成器, 简单测试,能正常使用,不确定是否全局替换了

This commit is contained in:
wangli 2023-10-31 15:38:18 +08:00
parent 834bd83bba
commit 2b6581f66a
3 changed files with 54 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package cn.axzo.workflow.core.conf;
import cn.axzo.workflow.core.engine.behavior.CustomActivityBehaviorFactory;
import cn.axzo.workflow.core.engine.id.TimeBasedIdGenerator;
import com.google.common.collect.Lists;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.form.spring.SpringFormEngineConfiguration;
@ -31,6 +32,7 @@ public class FlowableConfiguration {
configuration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_TRUE);
configuration.setEnableSafeBpmnXml(false);
// configuration.setCreateDiagramOnDeploy(false);
configuration.setIdGenerator(new TimeBasedIdGenerator());
};
}

View File

@ -1,19 +0,0 @@
package cn.axzo.workflow.core.engine.id;
import org.flowable.common.engine.impl.cfg.IdGenerator;
/**
* 基于时间的编号生成器
*
* @author wangli
* @since 2023/10/31 10:41
*/
public class BasedTimeIdGenerator implements IdGenerator {
@Override
public String getNextId() {
long timestamp = System.currentTimeMillis();
return "";
}
}

View File

@ -0,0 +1,52 @@
package cn.axzo.workflow.core.engine.id;
import org.flowable.common.engine.impl.cfg.IdGenerator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
/**
* 基于时间的编号生成器
*
* @author wangli
* @since 2023/10/31 10:41
*/
public class TimeBasedIdGenerator implements IdGenerator {
private static final long SEQUENCE_BITS = 9L;
private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BITS);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
private static final AtomicLong sequence = new AtomicLong(0);
private static volatile String lastTimestamp = "";
@Override
public synchronized String getNextId() {
String timestamp = dateFormat.format(new Date());
if (timestamp.equals(lastTimestamp)) {
long currentSequence = sequence.incrementAndGet() & MAX_SEQUENCE;
if (currentSequence == 0) {
// 如果递增数已达到最大值则等待下一毫秒
timestamp = tilNextTimestamp(timestamp);
}
} else {
sequence.set(0);
}
lastTimestamp = timestamp;
return timestamp + String.format("%09d", sequence.get());
}
private String tilNextTimestamp(String lastTimestamp) {
String timestamp = dateFormat.format(new Date());
while (timestamp.equals(lastTimestamp)) {
timestamp = dateFormat.format(new Date());
}
return timestamp;
}
}