Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
Project
Project
Details
Activity
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
项目统一框架
amos-boot-biz
Commits
52fa25b4
Commit
52fa25b4
authored
Nov 30, 2022
by
tangwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改消息
parent
f83a4db5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
1 deletion
+101
-1
MessageAction.java
...ejoin/amos/boot/biz/common/rule/action/MessageAction.java
+7
-1
SnowFlake.java
...ava/com/yeejoin/amos/boot/biz/common/utils/SnowFlake.java
+94
-0
No files found.
amos-boot-biz-common/src/main/java/com/yeejoin/amos/boot/biz/common/rule/action/MessageAction.java
View file @
52fa25b4
package
com
.
yeejoin
.
amos
.
boot
.
biz
.
common
.
rule
.
action
;
//package com.yeejoin.amos.latentdanger.business.rule.action;
import
com.alibaba.fastjson.JSON
;
import
com.yeejoin.amos.boot.biz.common.utils.RedisUtils
;
import
com.yeejoin.amos.boot.biz.common.utils.RuleUtils
;
import
com.yeejoin.amos.boot.biz.common.utils.SnowFlake
;
import
com.yeejoin.amos.feign.systemctl.Systemctl
;
import
com.yeejoin.amos.feign.systemctl.model.MessageModel
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.typroject.tyboot.core.foundation.utils.ValidationUtil
;
...
...
@@ -18,11 +21,14 @@ import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
public
class
MessageAction
{
public
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
MessageAction
.
class
);
@Autowired
private
SnowFlake
snowFlake
;
public
void
sendMessage
(
Object
msgObj
,
String
title
,
String
content
)
{
MessageModel
messageModel
=
JSON
.
parseObject
(
JSON
.
toJSONString
(
msgObj
),
MessageModel
.
class
);
messageModel
.
setTitle
(
title
);
messageModel
.
setBody
(
RuleUtils
.
instedParams
(
content
,
msgObj
));
messageModel
.
setSequenceNbr
(
snowFlake
.
nextId
());
log
.
info
(
String
.
format
(
"接收规则返回数据: %s"
,
msgObj
));
if
(!
ValidationUtil
.
isEmpty
(
messageModel
))
{
try
{
...
...
amos-boot-biz-common/src/main/java/com/yeejoin/amos/boot/biz/common/utils/SnowFlake.java
0 → 100644
View file @
52fa25b4
package
com
.
yeejoin
.
amos
.
boot
.
biz
.
common
.
utils
;
/**
* @description:
* @author: tw
* @createDate: 2022/11/30
*/
import
org.springframework.stereotype.Component
;
import
java.util.HashSet
;
import
java.util.concurrent.atomic.AtomicLong
;
@Component
public
class
SnowFlake
{
//时间 41位
private
static
long
lastTime
=
System
.
currentTimeMillis
();
//数据中心ID 5位(默认0-31)
private
long
datacenterId
=
0
;
private
long
datacenterIdShift
=
5
;
//机房机器ID 5位(默认0-31)
private
long
workerId
=
0
;
private
long
workerIdShift
=
5
;
//随机数 12位(默认0~4095)
private
AtomicLong
random
=
new
AtomicLong
();
private
long
randomShift
=
12
;
//随机数的最大值
private
long
maxRandom
=
(
long
)
Math
.
pow
(
2
,
randomShift
);
public
SnowFlake
()
{
}
public
SnowFlake
(
long
workerIdShift
,
long
datacenterIdShift
){
if
(
workerIdShift
<
0
||
datacenterIdShift
<
0
||
workerIdShift
+
datacenterIdShift
>
22
)
{
throw
new
IllegalArgumentException
(
"参数不匹配"
);
}
this
.
workerIdShift
=
workerIdShift
;
this
.
datacenterIdShift
=
datacenterIdShift
;
this
.
randomShift
=
22
-
datacenterIdShift
-
workerIdShift
;
this
.
maxRandom
=
(
long
)
Math
.
pow
(
2
,
randomShift
);
}
//获取雪花的ID
private
long
getId
()
{
return
lastTime
<<
(
workerIdShift
+
datacenterIdShift
+
randomShift
)
|
workerId
<<
(
datacenterIdShift
+
randomShift
)
|
datacenterId
<<
randomShift
|
random
.
get
();
}
//生成一个新的ID
public
synchronized
long
nextId
()
{
long
now
=
System
.
currentTimeMillis
();
//如果当前时间和上一次时间不在同一毫秒内,直接返回
if
(
now
>
lastTime
)
{
lastTime
=
now
;
random
.
set
(
0
);
return
getId
();
}
//将最后的随机数,进行+1操作
if
(
random
.
incrementAndGet
()
<
maxRandom
)
{
return
getId
();
}
//自选等待下一毫秒
while
(
now
<=
lastTime
)
{
now
=
System
.
currentTimeMillis
();
}
lastTime
=
now
;
random
.
set
(
0
);
return
getId
();
}
//测试
// public static void main(String[] args) {
// SnowFlake snowFlake = new SnowFlake();
// HashSet<Long> set = new HashSet<>();
// for (int i = 0; i < 10000; i++) {
// set.add(snowFlake.nextId());
// System.out.println(snowFlake.nextId());
//
// }
// System.out.println();
// }
}
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