-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mixed tasks and mixed execution procedures
- Loading branch information
Showing
3 changed files
with
270 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,130 @@ | ||
package com.deep.crow.task.mix; | ||
|
||
import com.deep.crow.exception.CrowException; | ||
import com.deep.crow.multi.Multi; | ||
import com.deep.crow.multi.MultiHelper; | ||
|
||
import java.util.List; | ||
import java.util.*; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* <h2>混合执行过程</h2> | ||
* 混合任务的执行过程需要用到有向图,从而实现对任务纵向的解剖, | ||
* | ||
* @author Create by liuwenhao on 2022/6/14 15:59 | ||
*/ | ||
public class MixMulti { | ||
public List<Multi> multis; | ||
|
||
/** | ||
* 线程池 | ||
*/ | ||
ExecutorService executorService; | ||
|
||
/** | ||
* 混合任务集合 | ||
*/ | ||
List<MixTask> mixTasks = new ArrayList<>(); | ||
|
||
/** | ||
* 任务名称列表,验证是否存在 | ||
*/ | ||
Set<String> taskName = new HashSet<>(); | ||
|
||
/** | ||
* 任务尾结点集合,尾结点的前置节点不能是尾结点 | ||
*/ | ||
List<MixTask> tailMixTasks = new ArrayList<>(); | ||
|
||
/** | ||
* 开始节点 | ||
*/ | ||
MixTask headTask; | ||
|
||
/** | ||
* <h2>添加任务。前置任务默认为开始节点</h2> | ||
* | ||
* @param name 当前任务标识 | ||
* @param runnable 任务 | ||
* @return MixMulti | ||
* @author liuwenhao | ||
* @date 2022/6/18 17:20 | ||
*/ | ||
public MixMulti add(String name, Runnable runnable) { | ||
Multi<Void> multi = MultiHelper.runAsync(executorService, runnable); | ||
MixTask mixTask = new RunnableMixTask(name, multi); | ||
addTask(mixTask); | ||
return this; | ||
} | ||
|
||
/** | ||
* <h2>添加任务。并声明前置任务</h2> | ||
* | ||
* @param name 当前任务标识 | ||
* @param runnable 任务 | ||
* @param preName 前置任务 | ||
* @return MixMulti | ||
* @author liuwenhao | ||
* @date 2022/6/18 17:20 | ||
*/ | ||
public MixMulti add(String name, Runnable runnable, String... preName) { | ||
Multi<Void> multi = MultiHelper.runAsync(executorService, runnable); | ||
MixTask mixTask = new RunnableMixTask(name, multi); | ||
for (String s : preName) { | ||
mixTask.addPreName(s); | ||
} | ||
addTask(mixTask); | ||
return this; | ||
} | ||
|
||
|
||
/** | ||
* <h2>添加任务</h2> | ||
* | ||
* @param mixTask 任务实体 | ||
* @author liuwenhao | ||
* @date 2022/6/18 17:34 | ||
*/ | ||
private synchronized void addTask(MixTask mixTask) { | ||
checkName(mixTask.name()); | ||
checkPreName(mixTask.pre()); | ||
|
||
} | ||
|
||
/** | ||
* <h2>验证任务标识是否已经存在</h2> | ||
* | ||
* @param name 任务名称 | ||
* @author liuwenhao | ||
* @date 2022/6/18 17:27 | ||
*/ | ||
private void checkName(String name) { | ||
if (taskName.contains(name)) { | ||
CrowException.of("任务名称{}已存在", name); | ||
} | ||
} | ||
|
||
/** | ||
* <h2>验证前置任务是否已经存在</h2> | ||
* 如果不存在则异常 | ||
* | ||
* @param preName 前置任务集合 | ||
* @author liuwenhao | ||
* @date 2022/6/18 17:38 | ||
*/ | ||
private void checkPreName(Set<String> preName) { | ||
boolean flag = true; | ||
String name = null; | ||
|
||
for (String s : preName) { | ||
boolean contains = taskName.contains(s); | ||
if (!contains) { | ||
flag = false; | ||
name = s; | ||
break; | ||
} | ||
} | ||
if (!flag) { | ||
CrowException.of("前置任务{}不存在", name); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/main/java/com/deep/crow/task/mix/RunnableMixTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.deep.crow.task.mix; | ||
|
||
import com.deep.crow.multi.Multi; | ||
|
||
import java.util.HashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
/** | ||
* <h2></h2> | ||
* | ||
* @author Create by liuwenhao on 2022/6/18 16:17 | ||
*/ | ||
class RunnableMixTask implements MixTask { | ||
|
||
/** | ||
* 当前任务的名称,即唯一标识 | ||
*/ | ||
String name; | ||
|
||
/** | ||
* 前置任务的唯一标识 | ||
*/ | ||
Set<String> preName = new HashSet<>(); | ||
|
||
/** | ||
* 任务体 | ||
*/ | ||
Multi<Void> multi; | ||
|
||
/** | ||
* 是否是尾结点,这取决于任务是否存在后置任务,不可随意更改 | ||
*/ | ||
boolean isTail; | ||
|
||
public RunnableMixTask(String name, Multi<Void> multi) { | ||
this.name = name; | ||
this.multi = multi; | ||
this.isTail = true; | ||
} | ||
|
||
@Override | ||
public void addPreName(String preName) { | ||
this.preName.add(preName); | ||
} | ||
|
||
@Override | ||
public void removePreName(String preName) { | ||
this.preName.remove(preName); | ||
} | ||
|
||
@Override | ||
public boolean isTail() { | ||
return isTail; | ||
} | ||
|
||
@Override | ||
public void cancelTail() { | ||
this.isTail = false; | ||
} | ||
|
||
@Override | ||
public Set<String> pre() { | ||
return preName; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public boolean complete() { | ||
try { | ||
multi.get(); | ||
} catch (ExecutionException | InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
RunnableMixTask that = (RunnableMixTask) o; | ||
return Objects.equals(name, that.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name); | ||
} | ||
|
||
} |