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