Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
platform
/
platform-process-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit d7a5ea54
authored
Sep 20, 2023
by
tiger
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
执行过程参数处理。
1 parent
7d2a1ca3
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
250 additions
and
176 deletions
platform-process/src/main/java/cn/com/glsx/process/job/xxljob/impl/SopInstNodeExecuteJobHandler.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/SopHelper.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/RunResult.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/ExecuteStruct.java → platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/SnapshotStruct.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/SopNodeExecutor.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/EndExecutor.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/JavaExecutor.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/SqlExecutor.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/SqlQueryExecutor.java
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/StartExecutor.java
platform-process/src/main/java/cn/com/glsx/process/redis/DistributedLockUtils.java
platform-process/src/main/java/cn/com/glsx/process/job/xxljob/impl/SopInstNodeExecuteJobHandler.java
View file @
d7a5ea5
...
...
@@ -17,7 +17,8 @@ import cn.com.glsx.process.modules.entity.SopInstNodeBatch;
import
cn.com.glsx.process.modules.model.SopStruct
;
import
cn.com.glsx.process.modules.service.SopService
;
import
cn.com.glsx.process.modules.sop.SopHelper
;
import
cn.com.glsx.process.modules.sop.execute.ExecuteStruct
;
import
cn.com.glsx.process.modules.sop.execute.RunResult
;
import
cn.com.glsx.process.modules.sop.execute.SnapshotStruct
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutor
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutorFactory
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeType
;
...
...
@@ -108,10 +109,14 @@ public class SopInstNodeExecuteJobHandler {
// 重新加载sop
sop
=
reloadAndCreateBatch
(
sop
,
node
);
SnapshotStruct
snapshotStruct
=
instance
.
prepare
(
sop
,
node
);
// 执行sop节点
ExecuteStruct
result
=
instance
.
execute
(
sop
,
node
);
RunResult
result
=
instance
.
execute
(
sop
,
node
,
snapshotStruct
);
snapshotStruct
=
instance
.
handleResult
(
sop
,
node
,
snapshotStruct
,
result
);
updateBatch
(
sop
,
node
,
resul
t
);
updateBatch
(
sop
,
node
,
snapshotStruc
t
);
updateNextNodes
(
sop
,
node
);
...
...
@@ -131,7 +136,7 @@ public class SopInstNodeExecuteJobHandler {
SopXxlJobUtil
.
finishSopNodeJob
(
sop
,
node
);
}
private
void
updateBatch
(
SopStruct
sop
,
SopInstNode
node
,
Execute
Struct
result
)
{
private
void
updateBatch
(
SopStruct
sop
,
SopInstNode
node
,
Snapshot
Struct
result
)
{
SopInstNodeBatch
nodeBatch
=
sop
.
getNodeBatch
();
nodeBatch
.
setSnapshot
(
JSONUtil
.
toJsonStr
(
result
));
// TODO 这里需要考虑合并结果
nodeBatch
.
setUpdateTime
(
new
Date
());
...
...
platform-process/src/main/java/cn/com/glsx/process/modules/sop/SopHelper.java
View file @
d7a5ea5
package
cn
.
com
.
glsx
.
process
.
modules
.
sop
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
cn.com.glsx.process.redis.DistributedLockUtils
;
import
java.io.IOException
;
import
java.security.GeneralSecurityException
;
import
java.security.MessageDigest
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Map
;
import
org.apache.commons.lang.StringUtils
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.web.client.RestTemplate
;
import
org.springframework.web.util.DefaultUriBuilderFactory
;
import
org.springframework.web.util.UriBuilder
;
import
java.io.IOException
;
import
java.net.URI
;
import
java.security.GeneralSecurityException
;
import
java.security.MessageDigest
;
import
java.util.*
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
cn.com.glsx.process.redis.DistributedLockUtils
;
/**
* 封装部分操作,可以提供给Java执行器中的代码调用
...
...
@@ -31,7 +33,7 @@ public class SopHelper {
public
static
boolean
lockInst
(
SopInst
inst
)
{
String
key
=
SOP_INST_LOCK_NAMESPACE
+
inst
.
getInstId
();
return
DistributedLockUtils
.
lock
(
key
,
10
L
,
3
,
3
L
);
return
DistributedLockUtils
.
lock
(
key
,
10
*
1000L
,
3
,
3
*
1000
L
);
}
public
static
void
unlockInst
(
SopInst
inst
)
{
...
...
@@ -89,7 +91,7 @@ public class SopHelper {
while
(
var5
.
hasNext
())
{
paramName
=
(
String
)
var5
.
next
();
sb
.
append
(
paramName
).
append
(
(
String
)
paramValues
.
get
(
paramName
));
sb
.
append
(
paramName
).
append
(
paramValues
.
get
(
paramName
));
}
sb
.
append
(
secret
);
...
...
@@ -100,7 +102,6 @@ public class SopHelper {
}
}
private
static
byte
[]
getSHA1Digest
(
String
data
)
throws
IOException
{
Object
var1
=
null
;
...
...
@@ -113,7 +114,6 @@ public class SopHelper {
}
}
private
static
String
byte2hex
(
byte
[]
bytes
)
{
StringBuilder
sign
=
new
StringBuilder
();
...
...
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/RunResult.java
0 → 100644
View file @
d7a5ea5
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
cn
.
com
.
glsx
.
process
.
modules
.
sop
.
execute
;
import
java.io.Serializable
;
/**
* 单节点单次执行结果
*
* @author kome
* @date 2023年9月20日 下午6:02:22
*/
public
class
RunResult
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
1321328867544959522L
;
}
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/
Execute
Struct.java
→
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/
Snapshot
Struct.java
View file @
d7a5ea5
...
...
@@ -9,13 +9,13 @@ import com.google.common.collect.Maps;
import
lombok.Getter
;
/**
* node执行过程管理
* node执行过程
-快照
管理
*
* @author kome
* @date 2023年9月19日 下午5:09:42
*/
@Getter
public
class
Execute
Struct
implements
Serializable
,
Cloneable
{
public
class
Snapshot
Struct
implements
Serializable
,
Cloneable
{
private
static
final
long
serialVersionUID
=
-
1413400196758036960L
;
//
...
...
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/SopNodeExecutor.java
View file @
d7a5ea5
...
...
@@ -30,8 +30,8 @@ public interface SopNodeExecutor {
* @param node
* @return
*/
default
Execute
Struct
prepare
(
SopStruct
sop
,
SopInstNode
node
)
{
ExecuteStruct
es
=
new
Execute
Struct
();
default
Snapshot
Struct
prepare
(
SopStruct
sop
,
SopInstNode
node
)
{
SnapshotStruct
es
=
new
Snapshot
Struct
();
// 启动参数
if
(
StrUtil
.
isNotEmpty
(
sop
.
getInst
().
getInstParam
()))
{
...
...
@@ -56,5 +56,30 @@ public interface SopNodeExecutor {
* @return 输出参数
* @throws Exception
*/
ExecuteStruct
execute
(
SopStruct
sop
,
SopInstNode
node
)
throws
Exception
;
RunResult
execute
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
executeStruct
)
throws
Exception
;
/**
* 合并本节点执行结果到快照体中
*
* @param sop
* @param node
* @param snapshotStruct
* @param result
* @return
*/
default
SnapshotStruct
handleResult
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
es
,
RunResult
result
)
{
// 启动参数
if
(
StrUtil
.
isNotEmpty
(
sop
.
getInst
().
getInstParam
()))
{
JSONObject
paramObj
=
JSONUtil
.
parseObj
(
sop
.
getInst
().
getInstParam
());
es
.
getParamMap
().
putAll
(
paramObj
);
}
// 当前节点需要的参数
if
(
StrUtil
.
isNotEmpty
(
node
.
getNodeParam
()))
{
JSONObject
nodeParamObj
=
JSONUtil
.
parseObj
(
node
.
getNodeParam
());
es
.
getExecuteMap
().
putAll
(
nodeParamObj
);
}
return
es
;
}
}
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/EndExecutor.java
View file @
d7a5ea5
package
cn
.
com
.
glsx
.
process
.
modules
.
sop
.
execute
.
executor
;
import
cn.com.glsx.process.common.constant.Constants
;
import
java.util.Map
;
import
org.springframework.stereotype.Service
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopInstNode
;
import
cn.com.glsx.process.modules.model.SopStruct
;
import
cn.com.glsx.process.modules.sop.execute.ExecuteStruct
;
import
cn.com.glsx.process.modules.sop.execute.SnapshotStruct
;
import
cn.com.glsx.process.modules.sop.execute.RunResult
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutor
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.Map
;
@Slf4j
@Service
public
class
EndExecutor
implements
SopNodeExecutor
{
@Override
public
ExecuteStruct
execute
(
SopStruct
sop
,
SopInstNode
node
)
throws
Exception
{
ExecuteStruct
executeStruct
=
prepare
(
sop
,
node
);
Map
<
String
,
Object
>
resultMap
=
executeStruct
.
getResultMap
();
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_SUCCESS
);
@Override
public
RunResult
execute
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
executeStruct
)
throws
Exception
{
return
executeStruct
;
}
Map
<
String
,
Object
>
resultMap
=
executeStruct
.
getResultMap
()
;
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_SUCCESS
);
return
null
;
}
}
\ No newline at end of file
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/JavaExecutor.java
View file @
d7a5ea5
package
cn
.
com
.
glsx
.
process
.
modules
.
sop
.
execute
.
executor
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
java.util.Map
;
import
org.springframework.stereotype.Service
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
cn.com.glsx.process.modules.entity.SopInstNode
;
import
cn.com.glsx.process.modules.model.SopStruct
;
import
cn.com.glsx.process.modules.sop.execute.ExecuteStruct
;
import
cn.com.glsx.process.modules.sop.execute.SnapshotStruct
;
import
cn.com.glsx.process.modules.sop.execute.RunResult
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutor
;
import
cn.com.glsx.process.modules.sop.execute.plugin.java.DynamicEngine
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.Map
;
@Slf4j
@Service
public
class
JavaExecutor
implements
SopNodeExecutor
{
@Override
public
ExecuteStruct
execute
(
SopStruct
sop
,
SopInstNode
node
)
throws
Exception
{
ExecuteStruct
executeStruct
=
prepare
(
sop
,
node
);
Map
<
String
,
Object
>
resultMap
=
executeStruct
.
getResultMap
();
try
{
// 处理开始节点
String
nodeContent
=
node
.
getNodeContent
();
JSONObject
paramMap
=
JSON
.
parseObject
(
nodeContent
);
String
javacode
=
paramMap
.
get
(
"source"
)
+
""
;
@Override
public
RunResult
execute
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
executeStruct
)
throws
Exception
{
Map
<
String
,
Object
>
resultMap
=
executeStruct
.
getResultMap
();
try
{
// 处理开始节点
String
nodeContent
=
node
.
getNodeContent
();
JSONObject
paramMap
=
JSON
.
parseObject
(
nodeContent
);
String
javacode
=
paramMap
.
get
(
"source"
)
+
""
;
// java参数处理,执行java
DynamicEngine
de
=
DynamicEngine
.
getInstance
();
// java参数处理,执行java
DynamicEngine
de
=
DynamicEngine
.
getInstance
();
// 获取实例快照,传给处理器
SopInst
inst
=
sop
.
getInst
();
String
instSnapshot
=
inst
.
getInstSnapshot
();
JSONObject
jsonObject
=
new
JSONObject
();
// 获取实例快照,传给处理器
SopInst
inst
=
sop
.
getInst
();
String
instSnapshot
=
inst
.
getInstSnapshot
();
JSONObject
jsonObject
=
new
JSONObject
();
// 节点信息
jsonObject
.
put
(
"sopId"
,
inst
.
getInstId
());
jsonObject
.
put
(
"sopName"
,
inst
.
getInstName
());
jsonObject
.
put
(
"nodeId"
,
node
.
getNodeId
());
jsonObject
.
put
(
"nodeName"
,
node
.
getNodeName
());
// 节点信息
jsonObject
.
put
(
"sopId"
,
inst
.
getInstId
());
jsonObject
.
put
(
"sopName"
,
inst
.
getInstName
());
jsonObject
.
put
(
"nodeId"
,
node
.
getNodeId
());
jsonObject
.
put
(
"nodeName"
,
node
.
getNodeName
());
// 节点入参
jsonObject
.
put
(
"nodeData"
,
node
.
getNodeParam
());
// 节点入参
jsonObject
.
put
(
"nodeData"
,
node
.
getNodeParam
());
// 实例快照
jsonObject
.
put
(
"instSnapshot"
,
instSnapshot
);
// 实例快照
jsonObject
.
put
(
"instSnapshot"
,
instSnapshot
);
Object
instance
=
de
.
execute
(
"Controller"
,
javacode
,
"execute"
,
new
Class
[]{
JSON
.
class
},
new
Object
[]{
jsonObject
});
Object
instance
=
de
.
execute
(
"Controller"
,
javacode
,
"execute"
,
new
Class
[]
{
JSON
.
class
},
new
Object
[]
{
jsonObject
});
// 处理返回结果
resultMap
.
put
(
Constants
.
RES_DATE_KEY
,
instance
);
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_SUCCESS
);
}
catch
(
Exception
e
)
{
String
message
=
e
.
getMessage
();
resultMap
.
put
(
Constants
.
RES_FAIL_MESSAGE
,
message
);
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_FAIL
);
}
// 处理返回结果
resultMap
.
put
(
Constants
.
RES_DATE_KEY
,
instance
);
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_SUCCESS
);
}
catch
(
Exception
e
)
{
String
message
=
e
.
getMessage
();
resultMap
.
put
(
Constants
.
RES_FAIL_MESSAGE
,
message
);
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_FAIL
);
}
return
executeStruct
;
}
return
null
;
}
}
\ No newline at end of file
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/SqlExecutor.java
View file @
d7a5ea5
...
...
@@ -2,26 +2,27 @@
package
cn
.
com
.
glsx
.
process
.
modules
.
sop
.
execute
.
executor
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopDataSource
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
cn.com.glsx.process.modules.entity.SopInstNode
;
import
cn.com.glsx.process.modules.model.SopStruct
;
import
cn.com.glsx.process.modules.service.SopDataSourceService
;
import
cn.com.glsx.process.modules.sop.execute.ExecuteStruct
;
import
cn.com.glsx.process.modules.sop.execute.SnapshotStruct
;
import
cn.com.glsx.process.modules.sop.execute.RunResult
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutor
;
import
cn.com.glsx.process.modules.sop.execute.plugin.dbconn.FlowDataAdapterImpl
;
import
cn.com.glsx.process.modules.sop.execute.plugin.dbconn.SopBizAdapter
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.Map
;
@Slf4j
@Service
public
class
SqlExecutor
implements
SopNodeExecutor
{
...
...
@@ -29,9 +30,8 @@ public class SqlExecutor implements SopNodeExecutor {
@Autowired
private
SopDataSourceService
sopDataSourceService
;
@Override
public
ExecuteStruct
execute
(
SopStruct
sop
,
SopInstNode
node
)
throws
Exception
{
ExecuteStruct
executeStruct
=
prepare
(
sop
,
node
);
@Override
public
RunResult
execute
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
executeStruct
)
throws
Exception
{
Map
<
String
,
Object
>
resultMap
=
executeStruct
.
getResultMap
();
try
{
// 获取配置参数信息
...
...
@@ -58,7 +58,7 @@ public class SqlExecutor implements SopNodeExecutor {
resultMap
.
put
(
Constants
.
RES_FAIL_MESSAGE
,
message
);
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_FAIL
);
}
return
executeStruct
;
return
null
;
}
/**
...
...
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/SqlQueryExecutor.java
View file @
d7a5ea5
...
...
@@ -4,19 +4,20 @@ package cn.com.glsx.process.modules.sop.execute.executor;
import
java.util.List
;
import
java.util.Map
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
cn.com.glsx.process.common.constant.Constants
;
import
cn.com.glsx.process.modules.entity.SopDataSource
;
import
cn.com.glsx.process.modules.entity.SopInst
;
import
cn.com.glsx.process.modules.entity.SopInstNode
;
import
cn.com.glsx.process.modules.model.SopStruct
;
import
cn.com.glsx.process.modules.service.SopDataSourceService
;
import
cn.com.glsx.process.modules.sop.execute.ExecuteStruct
;
import
cn.com.glsx.process.modules.sop.execute.SnapshotStruct
;
import
cn.com.glsx.process.modules.sop.execute.RunResult
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutor
;
import
cn.com.glsx.process.modules.sop.execute.plugin.dbconn.FlowDataAdapterImpl
;
import
cn.com.glsx.process.modules.sop.execute.plugin.dbconn.SopBizAdapter
;
...
...
@@ -29,9 +30,8 @@ public class SqlQueryExecutor implements SopNodeExecutor {
@Autowired
private
SopDataSourceService
sopDataSourceService
;
@Override
public
ExecuteStruct
execute
(
SopStruct
sop
,
SopInstNode
node
)
throws
Exception
{
ExecuteStruct
executeStruct
=
prepare
(
sop
,
node
);
@Override
public
RunResult
execute
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
executeStruct
)
throws
Exception
{
Map
<
String
,
Object
>
resultMap
=
executeStruct
.
getResultMap
();
try
{
// 获取配置参数信息
...
...
@@ -54,7 +54,7 @@ public class SqlQueryExecutor implements SopNodeExecutor {
resultMap
.
put
(
Constants
.
RES_FAIL_MESSAGE
,
message
);
resultMap
.
put
(
Constants
.
RES_KEY
,
Constants
.
RES_FAIL
);
}
return
executeStruct
;
return
null
;
}
private
void
codeReplace
(
String
sqlcode
,
SopStruct
sop
,
SopInstNode
node
)
{
...
...
platform-process/src/main/java/cn/com/glsx/process/modules/sop/execute/executor/StartExecutor.java
View file @
d7a5ea5
...
...
@@ -8,7 +8,8 @@ import org.springframework.stereotype.Service;
import
cn.com.glsx.process.modules.entity.SopInstNode
;
import
cn.com.glsx.process.modules.model.SopStruct
;
import
cn.com.glsx.process.modules.service.SopService
;
import
cn.com.glsx.process.modules.sop.execute.ExecuteStruct
;
import
cn.com.glsx.process.modules.sop.execute.SnapshotStruct
;
import
cn.com.glsx.process.modules.sop.execute.RunResult
;
import
cn.com.glsx.process.modules.sop.execute.SopNodeExecutor
;
import
cn.hutool.json.JSONObject
;
import
cn.hutool.json.JSONUtil
;
...
...
@@ -25,21 +26,15 @@ import lombok.extern.slf4j.Slf4j;
*/
@Slf4j
@Service
public
class
StartExecutor
implements
SopNodeExecutor
{
public
class
StartExecutor
extends
JavaExecutor
implements
SopNodeExecutor
{
@Resource
private
SopService
sopService
;
@Override
public
ExecuteStruct
execute
(
SopStruct
sop
,
SopInstNode
node
)
throws
Exception
{
ExecuteStruct
executeStruct
=
prepare
(
sop
,
node
);
return
executeStruct
;
}
public
void
updateBatch
()
{
public
RunResult
execute
(
SopStruct
sop
,
SopInstNode
node
,
SnapshotStruct
executeStruct
)
throws
Exception
{
super
.
execute
(
sop
,
node
,
executeStruct
);
return
null
;
}
public
static
void
main
(
String
[]
args
)
{
...
...
platform-process/src/main/java/cn/com/glsx/process/redis/DistributedLockUtils.java
View file @
d7a5ea5
...
...
@@ -11,86 +11,104 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public
class
DistributedLockUtils
{
private
static
final
int
DEFAULT_RETRY_TIMES
=
3
;
private
static
final
long
DEFAULT_RETRY_INTERVAL
=
1
00
;
private
static
final
int
DEFAULT_RETRY_TIMES
=
3
*
1000
;
private
static
final
long
DEFAULT_RETRY_INTERVAL
=
100
*
10
00
;
/**
* 分布式锁不重试
*
* @param key 锁key
* @param expireTime 过期时间
* @return
*/
public
static
boolean
lock
(
String
key
,
long
expireTime
)
{
RedisTemplate
redisTemplate
=
SpringUtils
.
getBean
(
"restRedisTemplate"
);
log
.
info
(
"锁获取{}"
,
key
);
Boolean
locked
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
"lock"
,
expireTime
,
TimeUnit
.
MILLISECONDS
);
log
.
info
(
"锁获取结果{}"
,
locked
);
if
(
locked
!=
null
&&
locked
)
{
return
true
;
}
return
false
;
}
private
static
RedisTemplate
redisTemplate
;
/**
* 重试分布式锁
*
* @param key 锁key
* @param expireTime 过期时间
* @param retryCount 重试次数
* @param retryTime 重试时间
* @return
*/
public
static
boolean
lock
(
String
key
,
long
expireTime
,
Integer
retryCount
,
Long
retryTime
)
{
RedisTemplate
redisTemplate
=
SpringUtils
.
getBean
(
"restRedisTemplate"
);
log
.
info
(
"锁获取{}"
,
key
);
Boolean
locked
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
"lock"
,
expireTime
,
TimeUnit
.
MILLISECONDS
);
log
.
info
(
"锁获取结果{}"
,
locked
);
if
(
locked
!=
null
&&
locked
)
{
return
true
;
}
if
(
retryCount
!=
null
)
{
return
retryLock
(
key
,
expireTime
,
retryCount
,
retryTime
);
}
return
false
;
}
public
static
RedisTemplate
redisTemplate
()
{
if
(
redisTemplate
==
null
)
{
redisTemplate
=
SpringUtils
.
getBean
(
"restRedisTemplate"
);
}
return
redisTemplate
;
}
/**
* 重试
*
* @param key 锁key
* @param expireTime 过期时间
* @param retryCount 重试次数
* @param retryTime 重试时间
* @return
*/
private
static
boolean
retryLock
(
String
key
,
long
expireTime
,
Integer
retryCount
,
Long
retryTime
)
{
if
(
retryCount
==
null
)
{
retryCount
=
DEFAULT_RETRY_TIMES
;
}
if
(
retryTime
==
null
)
{
retryTime
=
DEFAULT_RETRY_INTERVAL
;
}
RedisTemplate
redisTemplate
=
SpringUtils
.
getBean
(
"restRedisTemplate"
);
for
(
int
i
=
0
;
i
<
retryCount
;
i
++)
{
try
{
Thread
.
sleep
(
retryTime
);
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
}
/**
* 分布式锁不重试
*
* @param key 锁key
* @param expireTime 过期时间
* @return
*/
public
static
boolean
lock
(
String
key
,
long
expireTime
)
{
RedisTemplate
redisTemplate
=
redisTemplate
();
log
.
info
(
"锁获取{}"
,
key
);
Boolean
locked
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
System
.
currentTimeMillis
(),
expireTime
,
TimeUnit
.
MILLISECONDS
);
log
.
info
(
"锁获取结果{}"
,
locked
);
if
(
locked
!=
null
&&
locked
)
{
return
true
;
}
return
false
;
}
Boolean
locked
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
"lock"
,
expireTime
,
TimeUnit
.
MILLISECONDS
);
if
(
locked
!=
null
&&
locked
)
{
return
true
;
}
}
/**
* 重试分布式锁
*
* @param key 锁key
* @param expireTime 过期时间
* @param retryCount 重试次数
* @param retryTime 重试时间
* @return
*/
public
static
boolean
lock
(
String
key
,
long
expireTime
,
Integer
retryCount
,
Long
retryTime
)
{
RedisTemplate
redisTemplate
=
redisTemplate
();
log
.
info
(
"锁获取{}"
,
key
);
Boolean
locked
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
System
.
currentTimeMillis
()
+
""
,
expireTime
,
TimeUnit
.
MILLISECONDS
);
try
{
if
(
locked
!=
null
&&
locked
)
{
return
locked
;
}
if
(
retryCount
!=
null
)
{
locked
=
retryLock
(
key
,
expireTime
,
retryCount
,
retryTime
);
}
return
locked
!=
null
&&
locked
;
}
finally
{
log
.
info
(
"锁获取结果{}"
,
locked
);
}
}
return
false
;
}
/**
* 重试
*
* @param key 锁key
* @param expireTime 过期时间
* @param retryCount 重试次数
* @param retryTime 重试时间
* @return
*/
private
static
boolean
retryLock
(
String
key
,
long
expireTime
,
Integer
retryCount
,
Long
retryTime
)
{
if
(
retryCount
==
null
)
{
retryCount
=
DEFAULT_RETRY_TIMES
;
}
if
(
retryTime
==
null
)
{
retryTime
=
DEFAULT_RETRY_INTERVAL
;
}
RedisTemplate
redisTemplate
=
redisTemplate
();
for
(
int
i
=
0
;
i
<
retryCount
;
i
++)
{
try
{
Thread
.
sleep
(
retryTime
);
log
.
info
(
"重试锁获取{} retry:{}"
,
key
,
i
);
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
}
public
static
void
unlock
(
String
key
)
{
RedisTemplate
redisTemplate
=
SpringUtils
.
getBean
(
"restRedisTemplate"
);
redisTemplate
.
delete
(
key
);
log
.
info
(
"锁释放{}"
,
key
);
}
Boolean
locked
=
redisTemplate
.
opsForValue
().
setIfAbsent
(
key
,
System
.
currentTimeMillis
(),
expireTime
,
TimeUnit
.
MILLISECONDS
);
if
(
locked
!=
null
&&
locked
)
{
return
true
;
}
}
return
false
;
}
public
static
void
unlock
(
String
key
)
{
RedisTemplate
<
String
,
?>
redisTemplate
=
redisTemplate
();
Object
v
=
redisTemplate
.
opsForValue
().
get
(
key
);
if
(
v
!=
null
)
{
redisTemplate
.
delete
(
key
);
log
.
info
(
"锁释放{} time:{}"
,
key
,
(
System
.
currentTimeMillis
()
-
Long
.
valueOf
(
v
.
toString
())));
}
else
{
log
.
info
(
"锁释放{}"
,
key
);
}
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment