Skip to content

Commit 2782efb

Browse files
committed
fix properties
1 parent b4eada0 commit 2782efb

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/AutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public IDGen idGen(IDAllocDao allocDao){
3030
}
3131

3232
@Bean(initMethod = "init")
33-
public Leaf leafClient(IDGen idGen, IDAllocDao allocDao){
34-
return new Leaf(idGen,allocDao);
33+
public Leaf leafClient(IDGen idGen, IDAllocDao allocDao,LeafProperties leafProperties){
34+
return new Leaf(idGen,allocDao,leafProperties);
3535
}
3636

3737
}

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/Leaf.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.codingapi.springboot.leaf.exception.LeafServerException;
55
import com.codingapi.springboot.leaf.exception.NoKeyException;
6+
import com.codingapi.springboot.leaf.properties.LeafProperties;
67
import com.sankuai.inf.leaf.IDGen;
78
import com.sankuai.inf.leaf.common.Result;
89
import com.sankuai.inf.leaf.common.Status;
@@ -23,7 +24,7 @@ class Leaf {
2324

2425
private final IDGen idGen;
2526
private final IDAllocDao idAllocDao;
26-
27+
private final LeafProperties leafProperties;
2728

2829
long segmentGetId(String key){
2930
Result result = idGen.get(key);
@@ -58,7 +59,7 @@ void initIdGen(){
5859
}
5960

6061
void init(){
61-
LeafContext.getInstance().setLeaf(this);
62+
LeafContext.getInstance().setLeaf(this,leafProperties);
6263
}
6364

6465
}

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/LeafContext.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.codingapi.springboot.leaf;
22

33
import com.codingapi.springboot.leaf.exception.LeafServerException;
4+
import com.codingapi.springboot.leaf.properties.LeafProperties;
45
import lombok.Setter;
56
import lombok.extern.slf4j.Slf4j;
67

@@ -14,6 +15,9 @@
1415
class LeafContext {
1516

1617
private Leaf leaf;
18+
private int defaultStep;
19+
private int defaultMaxId;
20+
1721
@Setter
1822
private Set<Class<? extends LeafIdGenerate>> classes;
1923

@@ -33,8 +37,10 @@ public static LeafContext getInstance() {
3337
return instance;
3438
}
3539

36-
protected void setLeaf(Leaf leaf){
40+
protected void setLeaf(Leaf leaf, LeafProperties leafProperties){
3741
this.leaf = leaf;
42+
this.defaultMaxId = leafProperties.getDefaultMaxId();
43+
this.defaultStep = leafProperties.getDefaultStep();
3844
this.initClass();
3945
}
4046

@@ -43,21 +49,21 @@ long generateId(Class<?> clazz){
4349
return segmentGetId(clazz);
4450
}
4551

46-
private long segmentGetId(Class<?> clazz){
52+
long segmentGetId(Class<?> clazz){
4753
return leaf.segmentGetId(clazz.getName());
4854
}
4955

5056

51-
boolean push(String key, int step, int maxId){
52-
return leaf.segmentPush(key,step,maxId);
57+
public void push(String key, int step, int maxId){
58+
leaf.segmentPush(key, step, maxId);
5359
}
5460

5561

5662
private void initClass(){
5763
if(classes!=null&&classes.size()>0) {
5864
for (Class<?> clazz : classes) {
5965
try {
60-
LeafContext.getInstance().push(clazz.getName(), 2000, 1);
66+
LeafContext.getInstance().push(clazz.getName(), defaultStep, defaultMaxId);
6167
} catch (Exception e) {
6268
throw new LeafServerException(e);
6369
}

springboot-starter-leaf/src/main/java/com/codingapi/springboot/leaf/properties/LeafProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ public class LeafProperties {
99

1010
private String jdbcUrl = "jdbc:h2:file:./leaf.db";
1111

12+
private int defaultStep = 100;
13+
14+
private int defaultMaxId = 1;
15+
1216
}

springboot-starter-leaf/src/main/java/com/sankuai/inf/leaf/segment/SegmentIDGenImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public class SegmentIDGenImpl implements IDGen {
3737
* 一个Segment维持时间为15分钟
3838
*/
3939
private static final long SEGMENT_DURATION = 15 * 60 * 1000L;
40-
private ExecutorService service = new ThreadPoolExecutor(5, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new UpdateThreadFactory());
40+
private final ExecutorService service = new ThreadPoolExecutor(5, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new UpdateThreadFactory());
4141
private volatile boolean initOK = false;
42-
private Map<String, SegmentBuffer> cache = new ConcurrentHashMap<String, SegmentBuffer>();
42+
private final Map<String, SegmentBuffer> cache = new ConcurrentHashMap<String, SegmentBuffer>();
4343
private IDAllocDao dao;
4444

4545
public SegmentIDGenImpl(IDAllocDao dao) {
@@ -96,7 +96,7 @@ private void updateCacheFromDb() {
9696
if (dbTags == null || dbTags.isEmpty()) {
9797
return;
9898
}
99-
List<String> cacheTags = new ArrayList<String>(cache.keySet());
99+
List<String> cacheTags = new ArrayList<>(cache.keySet());
100100
Set<String> insertTagsSet = new HashSet<>(dbTags);
101101
Set<String> removeTagsSet = new HashSet<>(cacheTags);
102102
//db中新加的tags灌进cache

0 commit comments

Comments
 (0)