Commit 767e04f9 authored by qinxunjia's avatar qinxunjia

修改ID生成方式

parent a57bd496
...@@ -5,6 +5,7 @@ import com.bgy.sms.channel.dmHub.config.DmHubConfig; ...@@ -5,6 +5,7 @@ import com.bgy.sms.channel.dmHub.config.DmHubConfig;
import com.bgy.sms.channel.dto.*; import com.bgy.sms.channel.dto.*;
import com.bgy.sms.config.ResponseCode; import com.bgy.sms.config.ResponseCode;
import com.bgy.sms.service.MessageService; import com.bgy.sms.service.MessageService;
import com.bgy.sms.service.XiaoShuService;
import com.bgy.util.Md5Util; import com.bgy.util.Md5Util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -20,6 +21,11 @@ public class DmHubApi { ...@@ -20,6 +21,11 @@ public class DmHubApi {
@Autowired @Autowired
private MessageService messageService; private MessageService messageService;
@Autowired
private XiaoShuService xiaoShuService;
private static final String REJECTED = "rejected"; // 创建模板响应数据
private static final String APPROVED = "approved"; // 创建模板响应数据
@GetMapping("/ping") @GetMapping("/ping")
public String ping() { public String ping() {
...@@ -34,22 +40,20 @@ public class DmHubApi { ...@@ -34,22 +40,20 @@ public class DmHubApi {
* @return * @return
*/ */
@PostMapping("/sms/template") @PostMapping("/sms/template")
public DmHubResponse template(@RequestBody DmHubTemplateRequest params, String appId, String timestamp, String signature) { public void template(@RequestBody DmHubTemplateRequest params, String appId, String timestamp, String signature) {
log.info("**********创建模板接口入参*******:{},\r\n appId:{},timestamp:{},signature:{}", JSONObject.toJSONString(params), appId, timestamp, signature); log.info("**********创建模板接口入参*******:{},\r\n appId:{},timestamp:{},signature:{}", JSONObject.toJSONString(params), appId, timestamp, signature);
DmHubResponse response; DmHubResponse response;
try { try {
boolean checkResult = this.checkSignature(timestamp, signature); boolean checkResult = this.checkSignature(timestamp, signature);
if (!checkResult) { if (!checkResult) {
response = new DmHubResponse("555", "接口请求签名校验不通过"); xiaoShuService.updateDmhubSmsTempStatus(REJECTED, params.getTemplateId(), "接口请求签名校验不通过");
log.info("**********创建模板接口出参*******:{}", JSONObject.toJSONString(response)); log.info("**********创建模板接口出参*******:{}", "接口请求签名校验不通过");
return response;
} }
messageService.createTemplate(params); messageService.createTemplate(params);
} catch (Exception e) { } catch (Exception e) {
log.error("创建模板短信异常", e); log.error("创建模板短信异常", e);
new DmHubResponse("999", "创建模板短信异常"); new DmHubResponse("999", "创建模板短信异常");
} }
return null;
} }
/** /**
......
...@@ -160,7 +160,7 @@ public class BgySmsServiceImpl implements BgySmsService { ...@@ -160,7 +160,7 @@ public class BgySmsServiceImpl implements BgySmsService {
updateInfo.setUpRejectMsg(auditReason); updateInfo.setUpRejectMsg(auditReason);
updateInfo.setLastUpdated(new Date()); updateInfo.setLastUpdated(new Date());
smsTemplateService.updateById(updateInfo); smsTemplateService.updateById(updateInfo);
xiaoShuService.updateDmhubSmsTempStatus(dmhubStatue,smsTemplateInfo.getDmTemplateId()); xiaoShuService.updateDmhubSmsTempStatus(dmhubStatue,smsTemplateInfo.getDmTemplateId(),auditReason);
return new BGYResponse(); return new BGYResponse();
} }
......
...@@ -9,7 +9,7 @@ import java.util.Date; ...@@ -9,7 +9,7 @@ import java.util.Date;
public class DmBatchInfo { public class DmBatchInfo {
private Long id; private String id;
private String dmBatchId; private String dmBatchId;
...@@ -26,11 +26,11 @@ public class DmBatchInfo { ...@@ -26,11 +26,11 @@ public class DmBatchInfo {
private Date lastUpdated; private Date lastUpdated;
public Long getId() { public String getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(String id) {
this.id = id; this.id = id;
} }
......
...@@ -9,7 +9,7 @@ import java.util.Date; ...@@ -9,7 +9,7 @@ import java.util.Date;
public class DmCodeInfo { public class DmCodeInfo {
private Long id; private String id;
private String mobile; private String mobile;
...@@ -19,11 +19,11 @@ public class DmCodeInfo { ...@@ -19,11 +19,11 @@ public class DmCodeInfo {
private Date dateCreated; private Date dateCreated;
public Long getId() { public String getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(String id) {
this.id = id; this.id = id;
} }
......
...@@ -10,7 +10,7 @@ import java.util.Date; ...@@ -10,7 +10,7 @@ import java.util.Date;
public class SmsTemplateInfo { public class SmsTemplateInfo {
private Long id; private String id;
private String dmTemplateId; private String dmTemplateId;
...@@ -91,11 +91,11 @@ public class SmsTemplateInfo { ...@@ -91,11 +91,11 @@ public class SmsTemplateInfo {
this.tenantId = tenantId; this.tenantId = tenantId;
} }
public Long getId() { public String getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(String id) {
this.id = id; this.id = id;
} }
......
...@@ -7,13 +7,13 @@ import java.util.Date; ...@@ -7,13 +7,13 @@ import java.util.Date;
@TableName("sys_batch") @TableName("sys_batch")
public class SysBatchInfo { public class SysBatchInfo {
private Long id; private String id;
private String dmBatchId; private String dmBatchId;
private String dmTemplateId; private String dmTemplateId;
private Long batchId; private String batchId;
private String report; private String report;
...@@ -31,11 +31,11 @@ public class SysBatchInfo { ...@@ -31,11 +31,11 @@ public class SysBatchInfo {
private Date lastUpdated; private Date lastUpdated;
public Long getId() { public String getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(String id) {
this.id = id; this.id = id;
} }
...@@ -55,11 +55,11 @@ public class SysBatchInfo { ...@@ -55,11 +55,11 @@ public class SysBatchInfo {
this.dmTemplateId = dmTemplateId; this.dmTemplateId = dmTemplateId;
} }
public Long getBatchId() { public String getBatchId() {
return batchId; return batchId;
} }
public void setBatchId(Long batchId) { public void setBatchId(String batchId) {
this.batchId = batchId; this.batchId = batchId;
} }
......
...@@ -7,9 +7,9 @@ import java.util.Date; ...@@ -7,9 +7,9 @@ import java.util.Date;
@TableName("sms_record") @TableName("sms_record")
public class SysRecordInfo { public class SysRecordInfo {
private Long id; private String id;
private Long sysBatchId; private String sysBatchId;
private String mobile; private String mobile;
...@@ -39,19 +39,19 @@ public class SysRecordInfo { ...@@ -39,19 +39,19 @@ public class SysRecordInfo {
this.areaId = areaId; this.areaId = areaId;
} }
public Long getId() { public String getId() {
return id; return id;
} }
public void setId(Long id) { public void setId(String id) {
this.id = id; this.id = id;
} }
public Long getSysBatchId() { public String getSysBatchId() {
return sysBatchId; return sysBatchId;
} }
public void setSysBatchId(Long sysBatchId) { public void setSysBatchId(String sysBatchId) {
this.sysBatchId = sysBatchId; this.sysBatchId = sysBatchId;
} }
......
...@@ -5,5 +5,5 @@ import org.apache.ibatis.annotations.Param; ...@@ -5,5 +5,5 @@ import org.apache.ibatis.annotations.Param;
public interface XiaoShuMapper { public interface XiaoShuMapper {
String getLoginNameByTempId(@Param("tempId") String templateId); String getLoginNameByTempId(@Param("tempId") String templateId);
void updateDmhubTemp(@Param("status")String status, @Param("tempId")String tempId); void updateDmhubTemp(@Param("status")String status, @Param("tempId")String tempId,@Param("desc") String desc);
} }
...@@ -3,5 +3,5 @@ package com.bgy.sms.service; ...@@ -3,5 +3,5 @@ package com.bgy.sms.service;
public interface XiaoShuService { public interface XiaoShuService {
String selectUserIdByTemplate(String templateId); String selectUserIdByTemplate(String templateId);
void updateDmhubSmsTempStatus(String dmhubStatue, String dmTemplateId); void updateDmhubSmsTempStatus(String dmhubStatue, String dmTemplateId,String desc);
} }
...@@ -11,7 +11,7 @@ import com.bgy.sms.config.ResponseCode; ...@@ -11,7 +11,7 @@ import com.bgy.sms.config.ResponseCode;
import com.bgy.sms.repository.domain.*; import com.bgy.sms.repository.domain.*;
import com.bgy.sms.service.*; import com.bgy.sms.service.*;
import com.bgy.sms.service.bean.TemplateChangeBean; import com.bgy.sms.service.bean.TemplateChangeBean;
import com.bgy.util.id.IdHandler; import com.bgy.util.id.UUIDUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -47,6 +47,9 @@ public class MessageServiceImpl implements MessageService { ...@@ -47,6 +47,9 @@ public class MessageServiceImpl implements MessageService {
@Autowired @Autowired
private XiaoShuService xiaoShuService; private XiaoShuService xiaoShuService;
private static final String REJECTED = "rejected"; // 创建模板响应数据
private static final String APPROVED = "approved"; // 创建模板响应数据
/** /**
* 短信模板创建 * 短信模板创建
* *
...@@ -58,14 +61,14 @@ public class MessageServiceImpl implements MessageService { ...@@ -58,14 +61,14 @@ public class MessageServiceImpl implements MessageService {
log.info("进入创建短信模板业务逻辑,入参:{}", requestDTO); log.info("进入创建短信模板业务逻辑,入参:{}", requestDTO);
DmHubResponse responseDTO = new DmHubResponse(ResponseCode.SUCCESS); DmHubResponse responseDTO = new DmHubResponse(ResponseCode.SUCCESS);
String templateId = null;
try { try {
String templateContent = requestDTO.getTemplateContent(); String templateContent = requestDTO.getTemplateContent();
String signature = requestDTO.getSignature(); String signature = requestDTO.getSignature();
String smsType = requestDTO.getSmsType(); String smsType = requestDTO.getSmsType();
String tenantId = requestDTO.getTenantId(); String tenantId = requestDTO.getTenantId();
String templateId = requestDTO.getTemplateId(); templateId = requestDTO.getTemplateId();
String templateName = requestDTO.getTemplateName(); String templateName = requestDTO.getTemplateName();
// 替换模板格式,保存适合碧桂园发送短信的模板,避免发送时修改模板格式 // 替换模板格式,保存适合碧桂园发送短信的模板,避免发送时修改模板格式
TemplateChangeBean bean = dmHub2BgyTemplateSend(templateContent); TemplateChangeBean bean = dmHub2BgyTemplateSend(templateContent);
String bgySendStr = bean.getUpSendStr(); String bgySendStr = bean.getUpSendStr();
...@@ -73,6 +76,7 @@ public class MessageServiceImpl implements MessageService { ...@@ -73,6 +76,7 @@ public class MessageServiceImpl implements MessageService {
SmsTemplateInfo dbInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", templateId)); SmsTemplateInfo dbInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", templateId));
if (dbInfo != null) { if (dbInfo != null) {
log.error("Dm Hub请求创建模板,模板id已存在,模板id:【{}】", templateId); log.error("Dm Hub请求创建模板,模板id已存在,模板id:【{}】", templateId);
xiaoShuService.updateDmhubSmsTempStatus(REJECTED, templateId, "模板id已存在,请重新创建");
return new DmHubResponse(ResponseCode.TEMPLATE_ALREADY_EXISTED); return new DmHubResponse(ResponseCode.TEMPLATE_ALREADY_EXISTED);
} }
SmsTemplateInfo info = new SmsTemplateInfo(); SmsTemplateInfo info = new SmsTemplateInfo();
...@@ -80,7 +84,7 @@ public class MessageServiceImpl implements MessageService { ...@@ -80,7 +84,7 @@ public class MessageServiceImpl implements MessageService {
info.setType(smsType); info.setType(smsType);
info.setDmTemplateId(templateId); info.setDmTemplateId(templateId);
info.setTenantId(tenantId); info.setTenantId(tenantId);
info.setId(IdHandler.nextId()); info.setId(UUIDUtils.getId());
info.setSignature(signature); info.setSignature(signature);
info.setTemplateName(templateName); info.setTemplateName(templateName);
info.setDateCreated(new Date()); info.setDateCreated(new Date());
...@@ -91,9 +95,10 @@ public class MessageServiceImpl implements MessageService { ...@@ -91,9 +95,10 @@ public class MessageServiceImpl implements MessageService {
boolean insert = smsTemplateService.insert(info); boolean insert = smsTemplateService.insert(info);
if (!insert) { if (!insert) {
log.error("模板插入DB异常:【{}】", JSONObject.toJSONString(info)); log.error("模板插入DB异常:【{}】", JSONObject.toJSONString(info));
xiaoShuService.updateDmhubSmsTempStatus(REJECTED, templateId, "插件系统异常");
return new DmHubResponse(ResponseCode.SYSTEM_ERROR); return new DmHubResponse(ResponseCode.SYSTEM_ERROR);
} else { } else {
String pkID = info.getId() + ""; String pkID = info.getId();
String TEMPLATETYPE = ""; String TEMPLATETYPE = "";
if ("marketing".equals(smsType)) { if ("marketing".equals(smsType)) {
TEMPLATETYPE = "52"; TEMPLATETYPE = "52";
...@@ -103,6 +108,7 @@ public class MessageServiceImpl implements MessageService { ...@@ -103,6 +108,7 @@ public class MessageServiceImpl implements MessageService {
CLBizResponse response = new CLBizResponse(); CLBizResponse response = new CLBizResponse();
String account = xiaoShuService.selectUserIdByTemplate(templateId); String account = xiaoShuService.selectUserIdByTemplate(templateId);
if (StringUtils.isBlank(account)) { if (StringUtils.isBlank(account)) {
xiaoShuService.updateDmhubSmsTempStatus(REJECTED, templateId, "未找到当前登录用户的售楼系统ID");
return new DmHubResponse(ResponseCode.NO_USER_ID); return new DmHubResponse(ResponseCode.NO_USER_ID);
} }
response = bgySmsService.createTemplate(bgySendStr, account, TEMPLATETYPE, pkID); response = bgySmsService.createTemplate(bgySendStr, account, TEMPLATETYPE, pkID);
...@@ -111,15 +117,19 @@ public class MessageServiceImpl implements MessageService { ...@@ -111,15 +117,19 @@ public class MessageServiceImpl implements MessageService {
if (code.equals(ResponseCode.SUCCESS.getCode())) { if (code.equals(ResponseCode.SUCCESS.getCode())) {
SmsTemplateInfo updateInfo = new SmsTemplateInfo(); SmsTemplateInfo updateInfo = new SmsTemplateInfo();
updateInfo.setTemplateRecordId(msg); updateInfo.setTemplateRecordId(msg);
updateInfo.setId(Long.parseLong(pkID)); updateInfo.setId(pkID);
smsTemplateService.updateById(updateInfo); smsTemplateService.updateById(updateInfo);
return new DmHubResponse(ResponseCode.SUCCESS); return new DmHubResponse(ResponseCode.SUCCESS);
} else { } else {
xiaoShuService.updateDmhubSmsTempStatus(REJECTED, templateId, msg);
return new DmHubResponse(code, msg); return new DmHubResponse(code, msg);
} }
} }
} catch (Exception exception) { } catch (Exception exception) {
log.error("创建模板业务逻辑异常,错误信息", exception); log.error("创建模板业务逻辑异常,错误信息", exception);
if (!StringUtils.isBlank(templateId)) {
xiaoShuService.updateDmhubSmsTempStatus(REJECTED, templateId, "插件系统异常");
}
responseDTO = new DmHubResponse(ResponseCode.SYSTEM_ERROR); responseDTO = new DmHubResponse(ResponseCode.SYSTEM_ERROR);
} }
return responseDTO; return responseDTO;
...@@ -252,10 +262,10 @@ public class MessageServiceImpl implements MessageService { ...@@ -252,10 +262,10 @@ public class MessageServiceImpl implements MessageService {
String type = templateInfo.getType(); String type = templateInfo.getType();
// 3、记录批次信息 // 3、记录批次信息
SysBatchInfo sysBatchInfo = new SysBatchInfo(); SysBatchInfo sysBatchInfo = new SysBatchInfo();
long sysBatchId = IdHandler.nextId(); String sysBatchId = UUIDUtils.getId();
sysBatchInfo.setBatchId(sysBatchId); sysBatchInfo.setBatchId(sysBatchId);
sysBatchInfo.setDmBatchId(dmHubBatchId); sysBatchInfo.setDmBatchId(dmHubBatchId);
sysBatchInfo.setId(IdHandler.nextId()); sysBatchInfo.setId(UUIDUtils.getId());
sysBatchInfo.setDmTemplateId(templateId); sysBatchInfo.setDmTemplateId(templateId);
sysBatchInfo.setReport("1"); sysBatchInfo.setReport("1");
sysBatchInfo.setSmsNum(1); sysBatchInfo.setSmsNum(1);
...@@ -280,13 +290,11 @@ public class MessageServiceImpl implements MessageService { ...@@ -280,13 +290,11 @@ public class MessageServiceImpl implements MessageService {
} else { } else {
api = "SendNotifySMS"; api = "SendNotifySMS";
} }
response = bgySmsService.sendSms(mobile, content, channelAccount, api); response = bgySmsService.sendSms(mobile, content, channelAccount, api);
//4.短信发送记录 //4.短信发送记录
SysRecordInfo sysRecordInfo = new SysRecordInfo(); SysRecordInfo sysRecordInfo = new SysRecordInfo();
sysRecordInfo.setId(IdHandler.nextId()); sysRecordInfo.setId(UUIDUtils.getId());
long sysRecordInfoBatchId = IdHandler.nextId(); String sysRecordInfoBatchId = UUIDUtils.getId();
sysRecordInfo.setSysBatchId(sysRecordInfoBatchId); sysRecordInfo.setSysBatchId(sysRecordInfoBatchId);
sysRecordInfo.setMobile(mobile); sysRecordInfo.setMobile(mobile);
sysRecordInfo.setChargeNum(1); sysRecordInfo.setChargeNum(1);
...@@ -324,7 +332,7 @@ public class MessageServiceImpl implements MessageService { ...@@ -324,7 +332,7 @@ public class MessageServiceImpl implements MessageService {
String content = "您的验证码是:" + code; String content = "您的验证码是:" + code;
try { try {
DmCodeInfo codeInfo = new DmCodeInfo(); DmCodeInfo codeInfo = new DmCodeInfo();
codeInfo.setId(IdHandler.nextId()); codeInfo.setId(UUIDUtils.getId());
codeInfo.setAreaId(BgySMSConfig.areaId); codeInfo.setAreaId(BgySMSConfig.areaId);
codeInfo.setCode(code); codeInfo.setCode(code);
codeInfo.setMobile(mobile); codeInfo.setMobile(mobile);
...@@ -428,17 +436,17 @@ public class MessageServiceImpl implements MessageService { ...@@ -428,17 +436,17 @@ public class MessageServiceImpl implements MessageService {
dmInfo.setDmBatchId(batchId); dmInfo.setDmBatchId(batchId);
dmInfo.setDmTemplateId(templateId); dmInfo.setDmTemplateId(templateId);
dmInfo.setSmsNum(data.size()); dmInfo.setSmsNum(data.size());
dmInfo.setId(IdHandler.nextId()); dmInfo.setId(UUIDUtils.getId());
dmInfo.setDateCreated(new Date()); dmInfo.setDateCreated(new Date());
dmInfo.setLastUpdated(new Date()); dmInfo.setLastUpdated(new Date());
dmBatchService.insert(dmInfo); dmBatchService.insert(dmInfo);
SysBatchInfo info = new SysBatchInfo(); SysBatchInfo info = new SysBatchInfo();
info.setId(info.getBatchId()); String sysBatchId = UUIDUtils.getId();
info.setId(sysBatchId);
info.setDmBatchId(batchId); info.setDmBatchId(batchId);
info.setSmsNum(data.size()); info.setSmsNum(data.size());
info.setDmTemplateId(templateId); info.setDmTemplateId(templateId);
Long sysBatchId = IdHandler.nextId();
info.setBatchId(sysBatchId); info.setBatchId(sysBatchId);
info.setDateCreated(new Date()); info.setDateCreated(new Date());
info.setLastUpdated(new Date()); info.setLastUpdated(new Date());
...@@ -447,8 +455,8 @@ public class MessageServiceImpl implements MessageService { ...@@ -447,8 +455,8 @@ public class MessageServiceImpl implements MessageService {
//4.短信发送记录 //4.短信发送记录
SysRecordInfo sysRecordInfo = new SysRecordInfo(); SysRecordInfo sysRecordInfo = new SysRecordInfo();
sysRecordInfo.setId(IdHandler.nextId()); sysRecordInfo.setId(UUIDUtils.getId());
long sysRecordInfoBatchId = IdHandler.nextId(); String sysRecordInfoBatchId = UUIDUtils.getId();
sysRecordInfo.setSysBatchId(sysRecordInfoBatchId); sysRecordInfo.setSysBatchId(sysRecordInfoBatchId);
Date date = new Date(); Date date = new Date();
sysRecordInfo.setDateCreated(date); sysRecordInfo.setDateCreated(date);
...@@ -494,8 +502,7 @@ public class MessageServiceImpl implements MessageService { ...@@ -494,8 +502,7 @@ public class MessageServiceImpl implements MessageService {
return new DmHubResponse("999", "插件服务系统异常"); return new DmHubResponse("999", "插件服务系统异常");
} }
} } else {
else {
// 变量短信 // 变量短信
Set<Map.Entry<String, List<String>>> entries = paramsMap.entrySet(); Set<Map.Entry<String, List<String>>> entries = paramsMap.entrySet();
List<JSONObject> list = new ArrayList<>(); List<JSONObject> list = new ArrayList<>();
......
...@@ -25,7 +25,7 @@ public class XiaoShuServiceImpl implements XiaoShuService { ...@@ -25,7 +25,7 @@ public class XiaoShuServiceImpl implements XiaoShuService {
@Override @Override
@DB(value = DataSourceType.DB2) @DB(value = DataSourceType.DB2)
public void updateDmhubSmsTempStatus(String dmhubStatue, String dmTemplateId) { public void updateDmhubSmsTempStatus(String dmhubStatue, String dmTemplateId,String desc) {
xiaoShuMapper.updateDmhubTemp( dmhubStatue, dmTemplateId); xiaoShuMapper.updateDmhubTemp( dmhubStatue, dmTemplateId,desc);
} }
} }
package com.bgy.util.id; //package com.bgy.util.id;
//
//
import java.text.ParseException; //import java.text.ParseException;
import java.text.SimpleDateFormat; //import java.text.SimpleDateFormat;
import java.util.Date; //import java.util.Date;
//
/** ///**
* 基于Twitter的Snowflake算法实现分布式高效有序ID生产黑科技(sequence) // * 基于Twitter的Snowflake算法实现分布式高效有序ID生产黑科技(sequence)
* // *
* <br> // * <br>
* SnowFlake的结构如下(每部分用-分开):<br> // * SnowFlake的结构如下(每部分用-分开):<br>
* <br> // * <br>
* 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> // * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br>
* <br> // * <br>
* 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> // * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br>
* <br> // * <br>
* 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) // * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截)
* 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> // * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br>
* <br> // * <br>
* 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> // * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br>
* <br> // * <br>
* 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> // * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br>
* <br> // * <br>
* <br> // * <br>
* 加起来刚好64位,为一个Long型。<br> // * 加起来刚好64位,为一个Long型。<br>
* SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 // * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。
* // *
* @author lry // * @author lry
* @modify shuliangxing // * @modify shuliangxing
* @see <a href="https://tech.meituan.com/MT_Leaf.html">常见id策略对比分析</a> // * @see <a href="https://tech.meituan.com/MT_Leaf.html">常见id策略对比分析</a>
* @see <a href="https://github.com/twitter/snowflake">twitter snowflake</a> // * @see <a href="https://github.com/twitter/snowflake">twitter snowflake</a>
* @see <a href="https://gitee.com/yu120/sequence">sequence</a> // * @see <a href="https://gitee.com/yu120/sequence">sequence</a>
*/ // */
public class Id { //public class Id {
//
/** // /**
* 起始时间戳,用于用当前时间戳减去这个时间戳,算出偏移量 // * 起始时间戳,用于用当前时间戳减去这个时间戳,算出偏移量
**/ // **/
// 某一时刻时间戳, 2018-05-01 00:00:00 1525104000000 // // 某一时刻时间戳, 2018-05-01 00:00:00 1525104000000
private final long startTime = 1525104000000L; // private final long startTime = 1525104000000L;
//
/** // /**
* workerId占用的位数5(表示只允许workId的范围为:0-1023) // * workerId占用的位数5(表示只允许workId的范围为:0-1023)
**/ // **/
private final long workerIdBits = 5L; // private final long workerIdBits = 5L;
/** // /**
* dataCenterId占用的位数:5 // * dataCenterId占用的位数:5
**/ // **/
private final long dataCenterIdBits = 5L; // private final long dataCenterIdBits = 5L;
/** // /**
* 序列号占用的位数:12(表示只允许workId的范围为:0-4095) // * 序列号占用的位数:12(表示只允许workId的范围为:0-4095)
**/ // **/
private final long sequenceBits = 12L; // private final long sequenceBits = 12L;
//
/** // /**
* workerId可以使用的最大数值:31 // * workerId可以使用的最大数值:31
**/ // **/
private final long maxWorkerId = -1L ^ (-1L << workerIdBits); // private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
/** // /**
* dataCenterId可以使用的最大数值:31 // * dataCenterId可以使用的最大数值:31
**/ // **/
private final long maxDataCenterId = -1L ^ (-1L << dataCenterIdBits); // private final long maxDataCenterId = -1L ^ (-1L << dataCenterIdBits);
//
private final long workerIdShift = sequenceBits; // private final long workerIdShift = sequenceBits;
private final long dataCenterIdShift = sequenceBits + workerIdBits; // private final long dataCenterIdShift = sequenceBits + workerIdBits;
private final long timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits; // private final long timestampLeftShift = sequenceBits + workerIdBits + dataCenterIdBits;
//
/** // /**
* 用mask防止溢出:位与运算保证计算的结果范围始终是 0-4095 // * 用mask防止溢出:位与运算保证计算的结果范围始终是 0-4095
**/ // **/
private final long sequenceMask = -1L ^ (-1L << sequenceBits); // private final long sequenceMask = -1L ^ (-1L << sequenceBits);
//
private long workerId; // private long workerId;
private long dataCenterId; // private long dataCenterId;
private long sequence = 0L; // private long sequence = 0L;
private long lastTimestamp = -1L; // private long lastTimestamp = -1L;
private boolean isClock = false; // private boolean isClock = false;
//
/** // /**
* 基于Snowflake创建分布式ID生成器 // * 基于Snowflake创建分布式ID生成器
* <p> // * <p>
* 注:sequence // * 注:sequence
* // *
* @param workerId 工作机器ID,数据范围为0~31 // * @param workerId 工作机器ID,数据范围为0~31
* @param dataCenterId 数据中心ID,数据范围为0~31 // * @param dataCenterId 数据中心ID,数据范围为0~31
*/ // */
public Id(long workerId, long dataCenterId) { // public Id(long workerId, long dataCenterId) {
if (workerId > maxWorkerId || workerId < 0) { // if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); // throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
} // }
if (dataCenterId > maxDataCenterId || dataCenterId < 0) { // if (dataCenterId > maxDataCenterId || dataCenterId < 0) {
throw new IllegalArgumentException(String.format("dataCenter Id can't be greater than %d or less than 0", maxDataCenterId)); // throw new IllegalArgumentException(String.format("dataCenter Id can't be greater than %d or less than 0", maxDataCenterId));
} // }
//
this.workerId = workerId; // this.workerId = workerId;
this.dataCenterId = dataCenterId; // this.dataCenterId = dataCenterId;
} // }
//
public void setClock(boolean clock) { // public void setClock(boolean clock) {
isClock = clock; // isClock = clock;
} // }
//
/** // /**
* 获取ID // * 获取ID
* // *
* @return // * @return
*/ // */
public synchronized Long nextId() { // public synchronized Long nextId() {
long timestamp = this.timeGen(); // long timestamp = this.timeGen();
//
// 闰秒:如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 // // 闰秒:如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
if (timestamp < lastTimestamp) { // if (timestamp < lastTimestamp) {
long offset = lastTimestamp - timestamp; // long offset = lastTimestamp - timestamp;
if (offset <= 5) { // if (offset <= 5) {
try { // try {
this.wait(offset << 1); // this.wait(offset << 1);
timestamp = this.timeGen(); // timestamp = this.timeGen();
if (timestamp < lastTimestamp) { // if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset)); // throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset));
} // }
} catch (Exception e) { // } catch (Exception e) {
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
} else { // } else {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset)); // throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset));
} // }
} // }
//
// 解决跨毫秒生成ID序列号始终为偶数的缺陷:如果是同一时间生成的,则进行毫秒内序列 // // 解决跨毫秒生成ID序列号始终为偶数的缺陷:如果是同一时间生成的,则进行毫秒内序列
if (lastTimestamp == timestamp) { // if (lastTimestamp == timestamp) {
// 通过位与运算保证计算的结果范围始终是 0-4095 // // 通过位与运算保证计算的结果范围始终是 0-4095
sequence = (sequence + 1) & sequenceMask; // sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) { // if (sequence == 0) {
timestamp = this.tilNextMillis(lastTimestamp); // timestamp = this.tilNextMillis(lastTimestamp);
} // }
} else { // } else {
// 时间戳改变,毫秒内序列重置 // // 时间戳改变,毫秒内序列重置
sequence = 0L; // sequence = 0L;
} // }
//
lastTimestamp = timestamp; // lastTimestamp = timestamp;
//
/* // /*
* 1.左移运算是为了将数值移动到对应的段(41、5、5,12那段因为本来就在最右,因此不用左移) // * 1.左移运算是为了将数值移动到对应的段(41、5、5,12那段因为本来就在最右,因此不用左移)
* 2.然后对每个左移后的值(la、lb、lc、sequence)做位或运算,是为了把各个短的数据合并起来,合并成一个二进制数 // * 2.然后对每个左移后的值(la、lb、lc、sequence)做位或运算,是为了把各个短的数据合并起来,合并成一个二进制数
* 3.最后转换成10进制,就是最终生成的id // * 3.最后转换成10进制,就是最终生成的id
*/ // */
return ((timestamp - startTime) << timestampLeftShift) | // return ((timestamp - startTime) << timestampLeftShift) |
(dataCenterId << dataCenterIdShift) | // (dataCenterId << dataCenterIdShift) |
(workerId << workerIdShift) | // (workerId << workerIdShift) |
sequence; // sequence;
} // }
//
/** // /**
* 保证返回的毫秒数在参数之后(阻塞到下一个毫秒,直到获得新的时间戳) // * 保证返回的毫秒数在参数之后(阻塞到下一个毫秒,直到获得新的时间戳)
* // *
* @param lastTimestamp // * @param lastTimestamp
* @return // * @return
*/ // */
private long tilNextMillis(long lastTimestamp) { // private long tilNextMillis(long lastTimestamp) {
long timestamp = this.timeGen(); // long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) { // while (timestamp <= lastTimestamp) {
timestamp = this.timeGen(); // timestamp = this.timeGen();
} // }
//
return timestamp; // return timestamp;
} // }
//
/** // /**
* 获得系统当前毫秒数 // * 获得系统当前毫秒数
* // *
* @return timestamp // * @return timestamp
*/ // */
private long timeGen() { // private long timeGen() {
if (isClock) { // if (isClock) {
// 解决高并发下获取时间戳的性能问题 // // 解决高并发下获取时间戳的性能问题
return SystemClock.now(); // return SystemClock.now();
} else { // } else {
return System.currentTimeMillis(); // return System.currentTimeMillis();
} // }
} // }
//
public static void main(String[] args) throws ParseException { // public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2018-05-01 00:00:00"); //1525104000000 // Date date = sdf.parse("2018-05-01 00:00:00"); //1525104000000
System.out.println("\r\n----------Id.main()----------sdf.format(date)=" + sdf.format(date)); // System.out.println("\r\n----------Id.main()----------sdf.format(date)=" + sdf.format(date));
//
System.out.println("\r\n----------Id.main()----------date.getTime()=" + date.getTime()); // System.out.println("\r\n----------Id.main()----------date.getTime()=" + date.getTime());
//
} // }
//
} //}
\ No newline at end of file \ No newline at end of file
package com.bgy.util.id; //package com.bgy.util.id;
//
//
public class IdHandler { //public class IdHandler {
private IdHandler() { // private IdHandler() {
super(); // super();
} // }
//
private static final Id id = new Id(0, 0); // private static final Id id = new Id(0, 0);
//
public static Id getInstance() { // public static Id getInstance() {
return id; // return id;
} // }
//
public static long nextId() { // public static long nextId() {
return id.nextId(); // return id.nextId();
} // }
//
public static String nextIdStr() { // public static String nextIdStr() {
return String.valueOf(nextId()); // return String.valueOf(nextId());
} // }
//
public static void main(String[] args) { // public static void main(String[] args) {
//
} // }
//
} //}
package com.bgy.util.id; //package com.bgy.util.id;
//
import java.sql.Timestamp; //import java.sql.Timestamp;
import java.util.concurrent.Executors; //import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; //import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory; //import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; //import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; //import java.util.concurrent.atomic.AtomicLong;
//
/** ///**
* 高并发场景下System.currentTimeMillis()的性能问题的优化 // * 高并发场景下System.currentTimeMillis()的性能问题的优化
* <p><p> // * <p><p>
* System.currentTimeMillis()的调用比new一个普通对象要耗时的多(具体耗时高出多少我还没测试过,有人说是100倍左右)<p> // * System.currentTimeMillis()的调用比new一个普通对象要耗时的多(具体耗时高出多少我还没测试过,有人说是100倍左右)<p>
* System.currentTimeMillis()之所以慢是因为去跟系统打了一次交道<p> // * System.currentTimeMillis()之所以慢是因为去跟系统打了一次交道<p>
* 后台定时更新时钟,JVM退出时,线程自动回收<p> // * 后台定时更新时钟,JVM退出时,线程自动回收<p>
* 10亿:43410,206,210.72815533980582%<p> // * 10亿:43410,206,210.72815533980582%<p>
* 1亿:4699,29,162.0344827586207%<p> // * 1亿:4699,29,162.0344827586207%<p>
* 1000万:480,12,40.0%<p> // * 1000万:480,12,40.0%<p>
* 100万:50,10,5.0%<p> // * 100万:50,10,5.0%<p>
* // *
* @author lry // * @author lry
*/ // */
public class SystemClock { //public class SystemClock {
//
private final long period; // private final long period;
private final AtomicLong now; // private final AtomicLong now;
//
private SystemClock(long period) { // private SystemClock(long period) {
this.period = period; // this.period = period;
this.now = new AtomicLong(System.currentTimeMillis()); // this.now = new AtomicLong(System.currentTimeMillis());
scheduleClockUpdating(); // scheduleClockUpdating();
} // }
//
private static class InstanceHolder { // private static class InstanceHolder {
public static final SystemClock INSTANCE = new SystemClock(1); // public static final SystemClock INSTANCE = new SystemClock(1);
} // }
//
private static SystemClock instance() { // private static SystemClock instance() {
return InstanceHolder.INSTANCE; // return InstanceHolder.INSTANCE;
} // }
//
private void scheduleClockUpdating() { // private void scheduleClockUpdating() {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { // ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
public Thread newThread(Runnable runnable) { // public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, "System Clock"); // Thread thread = new Thread(runnable, "System Clock");
thread.setDaemon(true); // thread.setDaemon(true);
return thread; // return thread;
} // }
}); // });
scheduler.scheduleAtFixedRate(new Runnable() { // scheduler.scheduleAtFixedRate(new Runnable() {
public void run() { // public void run() {
now.set(System.currentTimeMillis()); // now.set(System.currentTimeMillis());
} // }
}, period, period, TimeUnit.MILLISECONDS); // }, period, period, TimeUnit.MILLISECONDS);
} // }
//
private long currentTimeMillis() { // private long currentTimeMillis() {
return now.get(); // return now.get();
} // }
//
public static long now() { // public static long now() {
return instance().currentTimeMillis(); // return instance().currentTimeMillis();
} // }
//
public static String nowDate() { // public static String nowDate() {
return new Timestamp(instance().currentTimeMillis()).toString(); // return new Timestamp(instance().currentTimeMillis()).toString();
} // }
//
} //}
package com.bgy.util.id;
import java.util.UUID;
public class UUIDUtils {
public static String getId() {
String id = UUID.randomUUID().toString().replaceAll("-", "");
return id;
}
}
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
</select> </select>
<update id="updateDmhubTemp" parameterType="string"> <update id="updateDmhubTemp" parameterType="string">
update sms_template set status = #{status} where id = #{tempId} update sms_template set status = #{status},reject_msg = #{desc} where id = #{tempId}
</update> </update>
</mapper> </mapper>
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