1. Frontier
After tossing for a day, I finally released the jar of xsequence to maven. After that, I have the motivation to start, maintain some documents, and continue to upgrade and improve this component. Each time a version is upgraded in the future, a document will be maintained, explaining some new features and usage methods.
2. Source address
https://gitee.com/xuan698400/xsequence
3. Maven support
<dependency>
<groupId>com.xuanner</groupId>
<artifactId>xsequence-core</artifactId>
<version>1.0</version>
</dependency>
4. Instructions for using DB mode
(1) API usage
public class SequenceTest_Api {
private com.xuanner.seq.sequence.Sequence userSeq;
@Before
public void setup() {
//数据源
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://121.196.218.206:3306/admin?characterEncoding=UTF-8");
dataSource.setUsername("admin");
dataSource.setPassword("xxx");
dataSource.setMaxActive(300);
dataSource.setMinIdle(50);
dataSource.setInitialSize(2);
dataSource.setMaxWait(500);
//利用DB获取区间管理器
DbSeqRangeMgr dbSeqRangeMgr = new DbSeqRangeMgr();
dbSeqRangeMgr.setDataSource(dataSource);
dbSeqRangeMgr.setTableName("sequence");
dbSeqRangeMgr.setRetryTimes(100);
dbSeqRangeMgr.setStep(100);
dbSeqRangeMgr.setStepStart(0);
dbSeqRangeMgr.init();
//构建序列号生成器
DefaultRangeSequence defaultRangeSequence = new DefaultRangeSequence();
defaultRangeSequence.setName("user");
defaultRangeSequence.setSeqRangeMgr(dbSeqRangeMgr);
userSeq = defaultRangeSequence;
}
@Test
public void test() {
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
System.out.println("++++++++++id:" + userSeq.nextValue());
}
System.out.println("interval time:" + (System.currentTimeMillis() - start));
}
}
(2) Spring configuration
Step 1: Configure spring's xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd" default-autowire="byName"
default-lazy-init="false">
<!-- 数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="jdbc:mysql://121.196.218.206:3306/admin?characterEncoding=UTF-8"/>
<property name="username" value="admin"/>
<property name="password" value="xxx"/>
<property name="maxActive" value="300"/>
<property name="minIdle" value="50"/>
<property name="initialSize" value="2"/>
<property name="maxWait" value="500"/>
</bean>
<!-- 序列号步长管理器 -->
<bean id="dbSeqRangeMgr" class="com.xuanner.seq.range.impl.db.DbSeqRangeMgr" init-method="init">
<!-- 数据源[必选] -->
<property name="dataSource" ref="dataSource"/>
<!-- 表名[可选] 默认:sequence-->
<property name="tableName" value="sequence"/>
<!-- 更新失败重试次数[可选] 默认:100-->
<property name="retryTimes" value="100"/>
<!-- 每次取数步长[可选] 默认:1000-->
<property name="step" value="1000"/>
<!-- 起始数,注意真实开始可用数是stepStart+1,例如stepStart=0表示从1开始[可选] 默认:0-->
<property name="stepStart" value="0"/>
</bean>
<!-- 具体使用demo -->
<bean id="userSeq" class="com.xuanner.seq.sequence.impl.DefaultRangeSequence">
<property name="seqRangeMgr" ref="dbSeqRangeMgr"/>
<property name="name" value="user"/>
</bean>
</beans>
Step 2: You can use it directly in the code
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:seq-test.xml" })
public class SequenceTest_Spring {
@Autowired
private Sequence userSeq;
@Test
public void test() {
for (int i = 0; i < 1000; i++) {
System.out.println("++++++++++id:" + userSeq.nextValue());
}
}
}