add
yj
2024-12-05 b9900893177c78fc559223521fe839aa21000017
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.dobbinsoft.fw.support.component;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.TimeUnit;
 
@Component
public class MachineComponent {
 
    @Autowired
    private StringRedisTemplate lockRedisTemplate;
 
    private Integer machineNo;
 
    private static final String MACHINE_PREFIX = "MACHINE_PREFIX_";
 
    public boolean isInit() {
        return this.machineNo != null;
    }
 
    /**
     * 从集群中获取标号,优先使用空缺编号,若不存在,则向后延申,从1开始编号
     *
     * @return
     */
    public Integer getMachineNo() {
        if (this.machineNo == null) {
            synchronized (MachineComponent.class) {
                if (this.machineNo == null) {
                    for (int i = 1; i < Integer.MAX_VALUE; i++) {
                        Boolean suc = lockRedisTemplate.opsForValue().setIfAbsent(MACHINE_PREFIX + i, "" + i);
                        if (suc) {
                            lockRedisTemplate.expire(MACHINE_PREFIX + i, 15, TimeUnit.MINUTES);
                            this.machineNo = i;
                            break;
                        }
                    }
                }
            }
        }
        return this.machineNo;
    }
 
    /**
     * 续约
     */
    public void renew() {
        if (this.machineNo == null) {
            throw new RuntimeException("续约失败,请先获取机器号");
        }
        lockRedisTemplate.expire(MACHINE_PREFIX + this.machineNo, 15, TimeUnit.MINUTES);
    }
 
    /**
     * 主动释放机器号
     */
    public void release() {
        lockRedisTemplate.delete(MACHINE_PREFIX + this.machineNo);
    }
 
 
}