|
@@ -40,13 +40,13 @@ import com.thebeastshop.liteflow.parser.ZookeeperXmlFlowParser;
|
|
import com.thebeastshop.liteflow.util.LOGOPrinter;
|
|
import com.thebeastshop.liteflow.util.LOGOPrinter;
|
|
|
|
|
|
public class FlowExecutor {
|
|
public class FlowExecutor {
|
|
-
|
|
|
|
|
|
+
|
|
private static final Logger LOG = LoggerFactory.getLogger(FlowExecutor.class);
|
|
private static final Logger LOG = LoggerFactory.getLogger(FlowExecutor.class);
|
|
-
|
|
|
|
|
|
+
|
|
private List<String> rulePath;
|
|
private List<String> rulePath;
|
|
-
|
|
|
|
|
|
+
|
|
private String zkNode;
|
|
private String zkNode;
|
|
-
|
|
|
|
|
|
+
|
|
public void init() {
|
|
public void init() {
|
|
XmlFlowParser parser = null;
|
|
XmlFlowParser parser = null;
|
|
for(String path : rulePath){
|
|
for(String path : rulePath){
|
|
@@ -71,88 +71,88 @@ public class FlowExecutor {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private boolean isZKConfig(String path) {
|
|
private boolean isZKConfig(String path) {
|
|
Pattern p = Pattern.compile("[\\w\\d][\\w\\d\\.]+\\:(\\d)+(\\,[\\w\\d][\\w\\d\\.]+\\:(\\d)+)*");
|
|
Pattern p = Pattern.compile("[\\w\\d][\\w\\d\\.]+\\:(\\d)+(\\,[\\w\\d][\\w\\d\\.]+\\:(\\d)+)*");
|
|
Matcher m = p.matcher(path);
|
|
Matcher m = p.matcher(path);
|
|
return m.find();
|
|
return m.find();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private boolean isLocalConfig(String path) {
|
|
private boolean isLocalConfig(String path) {
|
|
Pattern p = Pattern.compile("^[\\w\\/]+(\\/\\w+)*\\.xml$");
|
|
Pattern p = Pattern.compile("^[\\w\\/]+(\\/\\w+)*\\.xml$");
|
|
Matcher m = p.matcher(path);
|
|
Matcher m = p.matcher(path);
|
|
return m.find();
|
|
return m.find();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private boolean isClassConfig(String path) {
|
|
private boolean isClassConfig(String path) {
|
|
Pattern p = Pattern.compile("^\\w+(\\.\\w+)*$");
|
|
Pattern p = Pattern.compile("^\\w+(\\.\\w+)*$");
|
|
Matcher m = p.matcher(path);
|
|
Matcher m = p.matcher(path);
|
|
return m.find();
|
|
return m.find();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public void reloadRule(){
|
|
public void reloadRule(){
|
|
init();
|
|
init();
|
|
}
|
|
}
|
|
|
|
|
|
- public <T extends Slot> T execute(String chainId,Object param){
|
|
|
|
|
|
+ public <T extends Slot> T execute(String chainId,Object param) throws Exception{
|
|
return execute(chainId, param, DefaultSlot.class,null,false);
|
|
return execute(chainId, param, DefaultSlot.class,null,false);
|
|
}
|
|
}
|
|
-
|
|
|
|
- public <T extends Slot> T execute(String chainId,Object param,Class<? extends Slot> slotClazz){
|
|
|
|
|
|
+
|
|
|
|
+ public <T extends Slot> T execute(String chainId,Object param,Class<? extends Slot> slotClazz) throws Exception{
|
|
return execute(chainId, param, slotClazz,null,false);
|
|
return execute(chainId, param, slotClazz,null,false);
|
|
}
|
|
}
|
|
-
|
|
|
|
- public void invoke(String chainId,Object param,Class<? extends Slot> slotClazz,Integer slotIndex){
|
|
|
|
|
|
+
|
|
|
|
+ public void invoke(String chainId,Object param,Class<? extends Slot> slotClazz,Integer slotIndex) throws Exception{
|
|
execute(chainId, param, slotClazz,slotIndex,true);
|
|
execute(chainId, param, slotClazz,slotIndex,true);
|
|
}
|
|
}
|
|
-
|
|
|
|
- public <T extends Slot> T execute(String chainId,Object param,Class<? extends Slot> slotClazz,Integer slotIndex,boolean isInnerChain){
|
|
|
|
|
|
+
|
|
|
|
+ public <T extends Slot> T execute(String chainId,Object param,Class<? extends Slot> slotClazz,Integer slotIndex,boolean isInnerChain) throws Exception{
|
|
Slot slot = null;
|
|
Slot slot = null;
|
|
try{
|
|
try{
|
|
if(FlowBus.needInit()) {
|
|
if(FlowBus.needInit()) {
|
|
init();
|
|
init();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
Chain chain = FlowBus.getChain(chainId);
|
|
Chain chain = FlowBus.getChain(chainId);
|
|
-
|
|
|
|
|
|
+
|
|
if(chain == null){
|
|
if(chain == null){
|
|
String errorMsg = MessageFormat.format("couldn't find chain with the id[{0}]", chainId);
|
|
String errorMsg = MessageFormat.format("couldn't find chain with the id[{0}]", chainId);
|
|
throw new ChainNotFoundException(errorMsg);
|
|
throw new ChainNotFoundException(errorMsg);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if(!isInnerChain && slotIndex == null) {
|
|
if(!isInnerChain && slotIndex == null) {
|
|
slotIndex = DataBus.offerSlot(slotClazz);
|
|
slotIndex = DataBus.offerSlot(slotClazz);
|
|
LOG.info("slot[{}] offered",slotIndex);
|
|
LOG.info("slot[{}] offered",slotIndex);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if(slotIndex == -1){
|
|
if(slotIndex == -1){
|
|
throw new NoAvailableSlotException("there is no available slot");
|
|
throw new NoAvailableSlotException("there is no available slot");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
slot = DataBus.getSlot(slotIndex);
|
|
slot = DataBus.getSlot(slotIndex);
|
|
if(slot == null) {
|
|
if(slot == null) {
|
|
throw new NoAvailableSlotException("the slot is not exist");
|
|
throw new NoAvailableSlotException("the slot is not exist");
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if(StringUtils.isBlank(slot.getRequestId())) {
|
|
if(StringUtils.isBlank(slot.getRequestId())) {
|
|
slot.generateRequestId();
|
|
slot.generateRequestId();
|
|
LOG.info("requestId[{}] has generated",slot.getRequestId());
|
|
LOG.info("requestId[{}] has generated",slot.getRequestId());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if(!isInnerChain) {
|
|
if(!isInnerChain) {
|
|
slot.setRequestData(param);
|
|
slot.setRequestData(param);
|
|
slot.setChainName(chainId);
|
|
slot.setChainName(chainId);
|
|
}else {
|
|
}else {
|
|
slot.setChainReqData(chainId, param);
|
|
slot.setChainReqData(chainId, param);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
List<Condition> conditionList = chain.getConditionList();
|
|
List<Condition> conditionList = chain.getConditionList();
|
|
-
|
|
|
|
|
|
+
|
|
List<Node> nodeList = null;
|
|
List<Node> nodeList = null;
|
|
NodeComponent component = null;
|
|
NodeComponent component = null;
|
|
for(Condition condition : conditionList){
|
|
for(Condition condition : conditionList){
|
|
nodeList = condition.getNodeList();
|
|
nodeList = condition.getNodeList();
|
|
-
|
|
|
|
|
|
+
|
|
if(condition instanceof ThenCondition){
|
|
if(condition instanceof ThenCondition){
|
|
for(Node node : nodeList){
|
|
for(Node node : nodeList){
|
|
component = node.getInstance();
|
|
component = node.getInstance();
|
|
@@ -176,6 +176,9 @@ public class FlowExecutor {
|
|
LOG.error(errorMsg,t);
|
|
LOG.error(errorMsg,t);
|
|
throw t;
|
|
throw t;
|
|
}
|
|
}
|
|
|
|
+ }finally {
|
|
|
|
+ component.removeSlotIndex();
|
|
|
|
+ component.removeIsEnd();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}else if(condition instanceof WhenCondition){
|
|
}else if(condition instanceof WhenCondition){
|
|
@@ -190,7 +193,7 @@ public class FlowExecutor {
|
|
}catch(Exception e){
|
|
}catch(Exception e){
|
|
String errorMsg = MessageFormat.format("[{0}]executor cause error", slot.getRequestId());
|
|
String errorMsg = MessageFormat.format("[{0}]executor cause error", slot.getRequestId());
|
|
LOG.error(errorMsg,e);
|
|
LOG.error(errorMsg,e);
|
|
- throw new FlowSystemException(errorMsg);
|
|
|
|
|
|
+ throw e;
|
|
}finally{
|
|
}finally{
|
|
if(!isInnerChain) {
|
|
if(!isInnerChain) {
|
|
slot.printStep();
|
|
slot.printStep();
|
|
@@ -198,24 +201,24 @@ public class FlowExecutor {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private class WhenConditionThread extends Thread{
|
|
private class WhenConditionThread extends Thread{
|
|
-
|
|
|
|
|
|
+
|
|
private Node node;
|
|
private Node node;
|
|
-
|
|
|
|
|
|
+
|
|
private Integer slotIndex;
|
|
private Integer slotIndex;
|
|
-
|
|
|
|
|
|
+
|
|
private String requestId;
|
|
private String requestId;
|
|
-
|
|
|
|
|
|
+
|
|
private CountDownLatch latch;
|
|
private CountDownLatch latch;
|
|
-
|
|
|
|
|
|
+
|
|
public WhenConditionThread(Node node,Integer slotIndex,String requestId,CountDownLatch latch){
|
|
public WhenConditionThread(Node node,Integer slotIndex,String requestId,CountDownLatch latch){
|
|
this.node = node;
|
|
this.node = node;
|
|
this.slotIndex = slotIndex;
|
|
this.slotIndex = slotIndex;
|
|
this.requestId = requestId;
|
|
this.requestId = requestId;
|
|
this.latch = latch;
|
|
this.latch = latch;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
try{
|
|
try{
|
|
@@ -240,7 +243,7 @@ public class FlowExecutor {
|
|
public void setRulePath(List<String> rulePath) {
|
|
public void setRulePath(List<String> rulePath) {
|
|
this.rulePath = rulePath;
|
|
this.rulePath = rulePath;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
public String getZkNode() {
|
|
public String getZkNode() {
|
|
return zkNode;
|
|
return zkNode;
|
|
}
|
|
}
|