Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
byit-myth-job
Overview
Overview
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
liyuan
byit-myth-job
Commits
7118602c
Commit
7118602c
authored
Oct 27, 2020
by
huangfusuper
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
命令参数替换
parent
d0cc6f7d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
108 additions
and
32 deletions
+108
-32
ScriptExecutorJobTask.java
...re/src/main/java/com/byit/task/ScriptExecutorJobTask.java
+12
-0
PlaceholderUtils.java
...on/src/main/java/com/byit/job/utils/PlaceholderUtils.java
+96
-32
No files found.
byit-myth-core/myth-admin-core/src/main/java/com/byit/task/ScriptExecutorJobTask.java
View file @
7118602c
...
@@ -48,6 +48,7 @@ public class ScriptExecutorJobTask implements TimerTask {
...
@@ -48,6 +48,7 @@ public class ScriptExecutorJobTask implements TimerTask {
private
static
final
String
HTTP_SUFFIX
=
"/myth-job-admin/job/callbackRes"
;
private
static
final
String
HTTP_SUFFIX
=
"/myth-job-admin/job/callbackRes"
;
public
static
final
String
LINE
=
"\n"
;
public
static
final
String
LINE
=
"\n"
;
private
static
final
Integer
INIT_SLEEP_TIME
=
100
;
private
static
final
Integer
INIT_SLEEP_TIME
=
100
;
public
static
final
String
JAVA_TYPE
=
"java"
;
private
JobTaskSchedule
mythJobTaskSchedule
;
private
JobTaskSchedule
mythJobTaskSchedule
;
...
@@ -135,11 +136,22 @@ public class ScriptExecutorJobTask implements TimerTask {
...
@@ -135,11 +136,22 @@ public class ScriptExecutorJobTask implements TimerTask {
if
(
CollectionUtil
.
isNotEmpty
(
param
))
{
if
(
CollectionUtil
.
isNotEmpty
(
param
))
{
command
=
PlaceholderUtils
.
commandReplace
(
command
,
param
);
command
=
PlaceholderUtils
.
commandReplace
(
command
,
param
);
}
}
if
(
ScheduleTypeEnum
.
REPAIR
.
getCode
().
equals
(
mythJobTaskSchedule
.
getScheduleType
()))
{
if
(
command
.
startsWith
(
JAVA_TYPE
))
{
if
(
ScheduleTypeEnum
.
REPAIR
.
getCode
().
equals
(
mythJobTaskSchedule
.
getScheduleType
())){
command
=
PlaceholderUtils
.
commandDateReplace
(
param
,
command
,
param
.
get
(
PlaceholderEnum
.
DATE_PLACEHOLDER
.
getName
()),
param
.
get
(
PlaceholderEnum
.
NOW_DATE_PLACEHOLDER
.
getName
()));
}
else
{
command
=
PlaceholderUtils
.
commandDateReplace
(
param
,
command
);
}
}
}
}
}
//公共参数不为空的时候
//公共参数不为空的时候
if
(
CollectionUtil
.
isNotEmpty
(
publicParam
))
{
if
(
CollectionUtil
.
isNotEmpty
(
publicParam
))
{
command
=
PlaceholderUtils
.
commandReplace
(
command
,
publicParam
);
command
=
PlaceholderUtils
.
commandReplace
(
command
,
publicParam
);
}
}
mythJobTaskSchedule
.
setRunCommand
(
command
);
mythJobTaskSchedule
.
setRunCommand
(
command
);
}
}
}
}
...
...
byit-myth-core/myth-core-common/src/main/java/com/byit/job/utils/PlaceholderUtils.java
View file @
7118602c
...
@@ -9,11 +9,11 @@ import org.apache.commons.lang3.StringUtils;
...
@@ -9,11 +9,11 @@ import org.apache.commons.lang3.StringUtils;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
/**
/**
* 占位符替换工具类
* 占位符替换工具类
*
* @author huangfu
* @author huangfu
*/
*/
@Slf4j
@Slf4j
...
@@ -27,96 +27,160 @@ public class PlaceholderUtils {
...
@@ -27,96 +27,160 @@ public class PlaceholderUtils {
* 占位符后缀
* 占位符后缀
*/
*/
private
static
final
String
PLACEHOLDER_SUFFIX
=
"}"
;
private
static
final
String
PLACEHOLDER_SUFFIX
=
"}"
;
public
static
final
String
JAVA_TYPE
=
"java"
;
/**
/**
* 占位符数据替换
* 占位符数据替换
*
* @param scriptData 脚本数据
* @param scriptData 脚本数据
* @param parameter 脚本参数
* @param parameter
脚本参数
* @return 替换后的脚本参数
* @return 替换后的脚本参数
*/
*/
public
static
byte
[]
resolvePlaceholders
(
byte
[]
scriptData
,
Map
<
String
,
String
>
parameter
)
{
public
static
byte
[]
resolvePlaceholders
(
byte
[]
scriptData
,
Map
<
String
,
String
>
parameter
)
{
//替换为null 不做操作
//替换为null 不做操作
if
(
parameter
==
null
||
parameter
.
isEmpty
())
{
if
(
parameter
==
null
||
parameter
.
isEmpty
())
{
log
.
warn
(
"------------占位符数据为空--------"
);
log
.
warn
(
"------------占位符数据为空--------"
);
return
scriptData
;
return
scriptData
;
}
}
//读取脚本文件
//读取脚本文件
String
scriptContent
=
new
String
(
scriptData
,
StandardCharsets
.
UTF_8
);
String
scriptContent
=
new
String
(
scriptData
,
StandardCharsets
.
UTF_8
);
for
(
Map
.
Entry
<
String
,
String
>
entity
:
parameter
.
entrySet
())
{
for
(
Map
.
Entry
<
String
,
String
>
entity
:
parameter
.
entrySet
())
{
String
newKey
=
String
.
format
(
"%s%s%s"
,
PLACEHOLDER_PREFIX
,
entity
.
getKey
(),
PLACEHOLDER_SUFFIX
);
String
newKey
=
String
.
format
(
"%s%s%s"
,
PLACEHOLDER_PREFIX
,
entity
.
getKey
(),
PLACEHOLDER_SUFFIX
);
scriptContent
=
scriptContent
.
replace
(
newKey
,
entity
.
getValue
());
scriptContent
=
scriptContent
.
replace
(
newKey
,
entity
.
getValue
());
}
}
return
scriptContent
.
getBytes
(
StandardCharsets
.
UTF_8
);
return
scriptContent
.
getBytes
(
StandardCharsets
.
UTF_8
);
}
}
/**
/**
* 命令占位符替换
* 命令占位符替换
*
* @param command 命令
* @param command 命令
* @param param 参数
* @param param
参数
* @return 替换后的参数
* @return 替换后的参数
*/
*/
public
static
String
commandReplace
(
String
command
,
Map
<
String
,
String
>
param
)
{
public
static
String
commandReplace
(
String
command
,
Map
<
String
,
String
>
param
)
{
if
(
StringUtils
.
isBlank
(
command
)
||
CollectionUtil
.
isEmpty
(
param
))
{
if
(
StringUtils
.
isBlank
(
command
)
||
CollectionUtil
.
isEmpty
(
param
))
{
log
.
debug
(
"-----------命令{},不需要替换-----------"
,
command
);
log
.
debug
(
"-----------命令{},不需要替换-----------"
,
command
);
return
command
;
return
command
;
}
}
StringBuilder
sb
=
new
StringBuilder
(
command
);
if
(!
command
.
startsWith
(
JAVA_TYPE
))
{
for
(
Map
.
Entry
<
String
,
String
>
stringStringEntry
:
param
.
entrySet
())
{
StringBuilder
sb
=
new
StringBuilder
(
command
);
sb
.
append
(
" -"
).
append
(
stringStringEntry
.
getKey
()).
append
(
" "
).
append
(
stringStringEntry
.
getValue
());
for
(
Map
.
Entry
<
String
,
String
>
stringStringEntry
:
param
.
entrySet
())
{
sb
.
append
(
" -"
).
append
(
stringStringEntry
.
getKey
()).
append
(
" "
).
append
(
stringStringEntry
.
getValue
());
}
return
sb
.
toString
();
}
else
{
command
=
commandDateReplace
(
param
,
command
);
}
return
command
;
}
/**
* 命令日期替换
*
* @param commandParameter 参数对象
* @param command 原始命令
* @return 替换完成后的命令
*/
public
static
String
commandDateReplace
(
Map
<
String
,
String
>
commandParameter
,
String
command
)
{
return
commandDateReplace
(
commandParameter
,
command
,
null
,
null
);
}
/**
* 命令时间参数替换
* @param commandParameter 命令参数
* @param command 命令
* @param bizDate 时间
* @param nowDate 时间
* @return 替换后的命令
*/
public
static
String
commandDateReplace
(
Map
<
String
,
String
>
commandParameter
,
String
command
,
String
bizDate
,
String
nowDate
)
{
String
bizDateTypeName
=
"yyyyMMdd"
;
String
nowDateTypeName
=
"yyyyMMdd"
;
if
(
StringUtils
.
isBlank
(
command
))
{
return
command
;
}
if
(
CollectionUtil
.
isNotEmpty
(
commandParameter
))
{
String
bizDateParamTypeName
=
commandParameter
.
get
(
PlaceholderEnum
.
DATE_PLACEHOLDER
.
getName
());
String
nowDateParamTypeName
=
commandParameter
.
get
(
PlaceholderEnum
.
NOW_DATE_PLACEHOLDER
.
getName
());
if
(
StringUtils
.
isNoneBlank
(
bizDateParamTypeName
))
{
bizDateTypeName
=
bizDateParamTypeName
;
}
if
(
StringUtils
.
isNoneBlank
(
nowDateParamTypeName
))
{
nowDateTypeName
=
nowDateParamTypeName
;
}
}
if
(
StringUtils
.
isBlank
(
bizDate
)){
bizDate
=
DateUtil
.
dateLessDayStr
(
new
Date
(),
bizDateTypeName
,
1
);
}
if
(
StringUtils
.
isBlank
(
nowDate
))
{
nowDate
=
DateUtil
.
dateFormat
(
new
Date
(),
nowDateTypeName
);
}
}
return
sb
.
toString
();
command
=
command
.
replace
(
PLACEHOLDER_PREFIX
+
PlaceholderEnum
.
DATE_PLACEHOLDER
.
getName
()
+
PLACEHOLDER_SUFFIX
,
bizDate
);
command
=
command
.
replace
(
PLACEHOLDER_PREFIX
+
PlaceholderEnum
.
NOW_DATE_PLACEHOLDER
.
getName
()
+
PLACEHOLDER_SUFFIX
,
nowDate
);
return
command
;
}
}
/**
/**
* 初始化脚本信息
* 初始化脚本信息
* @param command 基础命令
*
* @param command 基础命令
* @param scriptPath 脚本路径
* @param scriptPath 脚本路径
* @return 基础命令
* @return 基础命令
*/
*/
public
static
String
initCommand
(
String
command
,
String
scriptPath
)
{
public
static
String
initCommand
(
String
command
,
String
scriptPath
)
{
log
.
info
(
"----------替换路径{}------"
,
scriptPath
);
log
.
info
(
"----------替换路径{}------"
,
scriptPath
);
return
command
.
replace
(
PLACEHOLDER_PREFIX
+
PlaceholderEnum
.
BIZ_SCRIPT_FILE
.
getName
()+
PLACEHOLDER_SUFFIX
,
scriptPath
);
return
command
.
replace
(
PLACEHOLDER_PREFIX
+
PlaceholderEnum
.
BIZ_SCRIPT_FILE
.
getName
()
+
PLACEHOLDER_SUFFIX
,
scriptPath
);
}
}
/**
/**
* 格式化运行参数
* 格式化运行参数
* 如果参数内存在时间参数 就将时间参数改为
* 如果参数内存在时间参数 就将时间参数改为
*
* @param runParam 运行参数
* @param runParam 运行参数
* @return 参数字符串
* @return 参数字符串
*/
*/
public
static
String
formatBizDateParam
(
String
runParam
,
String
key
,
Integer
cutBackDay
)
{
public
static
String
formatBizDateParam
(
String
runParam
,
String
key
,
Integer
cutBackDay
)
{
return
formatBizDateParam
(
runParam
,
key
,
null
,
cutBackDay
);
return
formatBizDateParam
(
runParam
,
key
,
null
,
cutBackDay
);
}
}
public
static
String
formatBizDateParam
(
String
runParam
,
String
key
,
String
value
,
Integer
cutBackDay
)
{
public
static
String
formatBizDateParam
(
String
runParam
,
String
key
,
String
value
,
Integer
cutBackDay
)
{
if
(
StringUtils
.
isBlank
(
runParam
))
{
if
(
StringUtils
.
isBlank
(
runParam
))
{
return
null
;
return
null
;
}
}
ScriptParamAndPlaceholderDto
scriptParamAndPlaceholderDto
=
JSON
.
parseObject
(
runParam
,
ScriptParamAndPlaceholderDto
.
class
);
ScriptParamAndPlaceholderDto
scriptParamAndPlaceholderDto
=
JSON
.
parseObject
(
runParam
,
ScriptParamAndPlaceholderDto
.
class
);
//获取命令参数
//获取命令参数
Map
<
String
,
String
>
param
=
scriptParamAndPlaceholderDto
.
getParam
();
Map
<
String
,
String
>
param
=
scriptParamAndPlaceholderDto
.
getParam
();
//获取替换成参数
Map
<
String
,
String
>
placeholder
=
scriptParamAndPlaceholderDto
.
getPlaceholder
();
Map
<
String
,
String
>
placeholder
=
scriptParamAndPlaceholderDto
.
getPlaceholder
();
if
(
CollectionUtil
.
isNotEmpty
(
param
)
&&
param
.
containsKey
(
key
)){
//替换命令参数
if
(
StringUtils
.
isNoneBlank
(
value
))
{
if
(
CollectionUtil
.
isNotEmpty
(
param
)
&&
param
.
containsKey
(
key
))
{
placeholder
.
put
(
key
,
value
);
if
(
StringUtils
.
isNoneBlank
(
value
))
{
}
else
{
param
.
put
(
key
,
value
);
}
else
{
String
dateFormat
=
param
.
get
(
key
);
String
dateFormat
=
param
.
get
(
key
);
String
calculationDate
=
DateUtil
.
dateLessDayStr
(
new
Date
(),
dateFormat
,
cutBackDay
);
String
calculationDate
=
DateUtil
.
dateLessDayStr
(
new
Date
(),
dateFormat
,
cutBackDay
);
param
.
put
(
key
,
calculationDate
);
param
.
put
(
key
,
calculationDate
);
scriptParamAndPlaceholderDto
.
setParam
(
param
);
scriptParamAndPlaceholderDto
.
setParam
(
param
);
}
}
}
}
//获取脚本占位符
//获取脚本占位符
if
(
CollectionUtil
.
isNotEmpty
(
placeholder
)
&&
placeholder
.
containsKey
(
key
))
{
if
(
CollectionUtil
.
isNotEmpty
(
placeholder
)
&&
placeholder
.
containsKey
(
key
))
{
if
(
StringUtils
.
isNoneBlank
(
value
))
{
if
(
StringUtils
.
isNoneBlank
(
value
))
{
placeholder
.
put
(
key
,
value
);
placeholder
.
put
(
key
,
value
);
}
else
{
}
else
{
String
dateFormat
=
p
aram
.
get
(
key
);
String
dateFormat
=
p
laceholder
.
get
(
key
);
String
calculationDate
=
DateUtil
.
dateLessDayStr
(
new
Date
(),
dateFormat
,
cutBackDay
);
String
calculationDate
=
DateUtil
.
dateLessDayStr
(
new
Date
(),
dateFormat
,
cutBackDay
);
placeholder
.
put
(
key
,
calculationDate
);
placeholder
.
put
(
key
,
calculationDate
);
scriptParamAndPlaceholderDto
.
setPlaceholder
(
placeholder
);
scriptParamAndPlaceholderDto
.
setPlaceholder
(
placeholder
);
}
}
...
...
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