orderNo生成增加redis锁
parent
ea8ec0ed18
commit
faf60dd3e5
@ -0,0 +1,67 @@
|
||||
package test.cn.estsh.i3plus.core.apiservice.serviceimpl.busi;
|
||||
|
||||
import cn.estsh.i3plus.core.api.iservice.base.ISynchronizedService;
|
||||
import cn.estsh.i3plus.core.apiservice.serviceimpl.base.SynchronizedService;
|
||||
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
|
||||
import cn.estsh.i3plus.pojo.platform.bean.SysOrderNoRule;
|
||||
import cn.estsh.impp.framework.boot.util.ImppRedis;
|
||||
import cn.estsh.impp.framework.run.ImppGo;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.testng.TestRunner;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* @author ns
|
||||
* 测试生成orderNo是否重复
|
||||
* @create 2021/9/15 0015 下午 13:46
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ImppGo.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class TestSynchronizedService extends AbstractTestNGSpringContextTests {
|
||||
|
||||
@Autowired
|
||||
private ISynchronizedService synchronizedService;
|
||||
|
||||
@Resource(name = CommonConstWords.IMPP_REDIS_RES)
|
||||
private ImppRedis resRedis;
|
||||
|
||||
public static String orderNoKey = CommonConstWords.REDIS_PREFIX_LOCK_GET_ORDER_NO + ":" + "LOCK" + ":" + "PO_ORDER_NO" + ":" + "ORDERNO";
|
||||
public static String serialNoKey = CommonConstWords.REDIS_PREFIX_LOCK_GET_ORDER_NO + ":" + "LOCK" + ":" + "PO_ORDER_NO" + ":" + "SERIALNO";
|
||||
|
||||
|
||||
@Test(threadPoolSize = 5)
|
||||
public void getOrderNo() {
|
||||
while (true) {
|
||||
try {
|
||||
SysOrderNoRule sysOrderNoRule = synchronizedService.nextOrderNo("PO_ORDER_NO");
|
||||
System.out.println(sysOrderNoRule);
|
||||
System.out.println(sysOrderNoRule.getOrderNo());
|
||||
System.out.println(sysOrderNoRule.getSerialNo());
|
||||
if (!ObjectUtils.isEmpty(resRedis.getHash(orderNoKey + ":ORDERNO" ,sysOrderNoRule.getOrderNo()))){
|
||||
throw new Exception("订单号重复");
|
||||
}else{
|
||||
resRedis.putHash(orderNoKey + ":ORDERNO",sysOrderNoRule.getOrderNo(), System.currentTimeMillis());
|
||||
}
|
||||
if (!ObjectUtils.isEmpty(resRedis.getHash(orderNoKey + ":SERIALNO", String.valueOf(sysOrderNoRule.getSerialNo())))){
|
||||
throw new Exception("订单号重复");
|
||||
}else {
|
||||
resRedis.putHash(orderNoKey + ":SERIALNO", String.valueOf(sysOrderNoRule.getSerialNo()), System.currentTimeMillis());
|
||||
}
|
||||
Thread.sleep(new Random().nextInt(20));
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue