Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
QA
holmes
Commits
22297607
Commit
22297607
authored
Dec 16, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化关键字替换
parent
30a1a7f1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
10 deletions
+17
-10
DbQueryTask.java
src/main/java/cn/qg/holmes/quartz/DbQueryTask.java
+17
-10
No files found.
src/main/java/cn/qg/holmes/quartz/DbQueryTask.java
View file @
22297607
...
...
@@ -19,6 +19,10 @@ import java.util.*;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* 数据库查询类
* @author libo
*/
@Slf4j
public
class
DbQueryTask
extends
QuartzJobBean
{
...
...
@@ -45,36 +49,39 @@ public class DbQueryTask extends QuartzJobBean {
monitorDataSource
.
getUsername
(),
monitorDataSource
.
getPassword
(),
null
,
monitorTask
.
getSql
());
String
dingUrl
=
monitorTask
.
getDingUrl
();
String
dingText
=
monitorTask
.
getDingText
();
// dingText = dingText.replaceAll("\\{count}", String.valueOf(resultList.size()));
dingText
=
replaceDingText
(
dingText
,
resultList
);
String
markdownMsg
=
DingdingUtils
.
buildMarkdownMsg
(
"监控测试"
,
dingText
+
"\n"
+
JSON
.
toJSONString
(
resultList
),
false
);
DingdingUtils
.
sendToDingding
(
markdownMsg
,
dingUrl
);
}
public
static
String
replaceDingText
(
String
dingText
,
List
<
Map
<
String
,
Object
>>
queryList
)
{
// 替换{count}
if
(
dingText
.
contains
(
"{count}"
))
{
dingText
=
dingText
.
replace
(
"{count}"
,
String
.
valueOf
(
queryList
.
size
()));
}
Pattern
numPattern
=
Pattern
.
compile
(
".*\\(\\{\\d+}\\).*"
);
// 正则表达式替换{数字},当前仅替换第一行的数据,{0}为第一行第一列,{1}第一行第二列,以此类推...
String
regex
=
"\\{([^}]*)\\}"
;
Pattern
numPattern
=
Pattern
.
compile
(
regex
);
Matcher
matcher
=
numPattern
.
matcher
(
dingText
);
List
<
String
>
firstLineDataList
=
null
;
if
(
queryList
.
size
()
>
0
)
{
firstLineDataList
=
new
ArrayList
(
queryList
.
get
(
0
).
values
());
}
if
(
matcher
.
find
())
{
int
count
=
matcher
.
groupCount
();
System
.
out
.
println
(
count
);
System
.
out
.
println
(
matcher
.
group
(
0
));
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
dingText
.
replace
(
matcher
.
group
(
i
+
1
),
firstLineDataList
.
get
(
i
));
while
(
matcher
.
find
())
{
String
matchString
=
matcher
.
group
();
int
index
=
Integer
.
parseInt
(
matchString
.
substring
(
1
,
matchString
.
length
()
-
1
));
if
(
firstLineDataList
!=
null
&&
firstLineDataList
.
size
()
>
index
)
{
dingText
=
dingText
.
replace
(
matchString
,
firstLineDataList
.
get
(
index
));
}
else
{
dingText
=
dingText
.
replace
(
matchString
,
"NULL"
);
}
}
return
dingText
;
}
public
static
void
main
(
String
[]
args
)
{
String
dingText
=
"监控定时任务测试,本次共查出{count}条数据,第一行第一列数据是{0}啦啦啦,
{1
}"
;
String
dingText
=
"监控定时任务测试,本次共查出{count}条数据,第一行第一列数据是{0}啦啦啦,
第一行第二列数据是{1},第一行第三列数据是{2
}"
;
List
<
Map
<
String
,
Object
>>
mapList
=
new
ArrayList
<>();
Map
<
String
,
Object
>
map1
=
new
LinkedHashMap
<>();
Map
<
String
,
Object
>
map2
=
new
LinkedHashMap
<>();
...
...
@@ -84,8 +91,8 @@ public class DbQueryTask extends QuartzJobBean {
map2
.
put
(
"sku_name"
,
"商品二"
);
mapList
.
add
(
map1
);
mapList
.
add
(
map2
);
System
.
out
.
println
(
mapList
);
dingText
=
replaceDingText
(
dingText
,
mapList
);
System
.
out
.
println
(
dingText
);
}
}
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