Commit 820edb78 authored by Quxl's avatar Quxl

增加公共工具CommonService, 使用乐观锁,用于生成全局唯一ID

parent 1f539600
package com.egolm.sso.config;
public class XRException extends RuntimeException {
private static final long serialVersionUID = 1L;
public XRException(String msg) {
super(msg);
}
public XRException(String msg, Throwable e) {
super(msg, e);
}
public XRException(String msg, int code) {
super(msg);
}
public XRException(String msg, int code, Throwable e) {
super(msg, e);
}
public static void assertNotBlank(String message, Object... objs) {
for(Object obj : objs) {
if(obj == null || obj.toString().trim().equals("")) {
throw new XRException((message == null || message.trim().equals("")) ? "对象不能为空" : message);
}
}
}
}
package com.egolm.sso.service;
public interface CommonService {
public Long getNextval(String sName);
}
package com.egolm.sso.service.impl;
import java.text.MessageFormat;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.egolm.sso.config.XRException;
import com.egolm.sso.service.CommonService;
@Service
public class CommonServiceImpl implements CommonService {
@Autowired
private JdbcTemplate jdbcTemplate;
public Long getNextval(String sName) {
try {
Map<String, Object> seqMap = jdbcTemplate.queryForMap("select * from sequence where name = ?", sName);
Integer id = (Integer)seqMap.get("id");
Long step = (Long)seqMap.get("step");
Long max = (Long)seqMap.get("max");
Long min = (Long)seqMap.get("min");
Long value = (Long)seqMap.get("value");
Long nextValue = value + step;
if(value > max || value < min) {
throw new XRException(MessageFormat.format("序列已超出许可范围[{0}]", sName));
} else {
String sql = "update sequence set value = ? where id = ? and value < ?";
int count = jdbcTemplate.update(sql, nextValue, id, nextValue);
if(count == 1) {
return value;
} else if(count == 0) {
return this.getNextval(sName);
} else {
throw new XRException(MessageFormat.format("序列错误[{0}]", sName));
}
}
} catch (EmptyResultDataAccessException e) {
String sql = "insert into sequence (name, max, min, step, value) values (?, ?, ?, ?, ?)";
jdbcTemplate.update(sql, sName, Long.MAX_VALUE, 1L, 1L, 1L);
return this.getNextval(sName);
}
}
}
...@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.egolm.sso.service.CommonService;
import com.egolm.sso.service.MaterialMasterService; import com.egolm.sso.service.MaterialMasterService;
@Component @Component
...@@ -15,8 +16,12 @@ public class MaterialMasterServiceImpl implements MaterialMasterService { ...@@ -15,8 +16,12 @@ public class MaterialMasterServiceImpl implements MaterialMasterService {
@Autowired @Autowired
JdbcTemplate jdbcTemplate; JdbcTemplate jdbcTemplate;
@Autowired
CommonService common;
@Override @Override
public void execute(String xml) { public void execute(String xml) {
System.out.println(common.getNextval("Test"));
System.out.println(xml); System.out.println(xml);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment