Skip to content

Commit

Permalink
测试的任务类Lark
Browse files Browse the repository at this point in the history
  • Loading branch information
chouchouGG committed Dec 11, 2024
1 parent ee2f6e1 commit 6883eb1
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
56 changes: 56 additions & 0 deletions ChronAnt-app/src/test/java/test/Lark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package test;

import cn.uhoc.domain.executor.model.entity.TaskContextEntity;
import cn.uhoc.domain.executor.model.entity.TaskSetStageEntity;
import cn.uhoc.domain.register.MultiStageAsyncTask;
import cn.uhoc.domain.task.Executable;
import cn.uhoc.domain.task.TaskRet;
import com.alibaba.fastjson.JSON;

import java.lang.reflect.Method;

// 测试任务
// 此处可以定义自己的任务
@MultiStageAsyncTask(value = "test.Lark")
public class Lark implements Executable {
public TaskRet printMsg(String msg) {
System.out.println("The printed msg is: " + msg);
TaskSetStageEntity taskSetStageEntity = null;
try {
Method method = this.getClass().getMethod("printMsg2", String.class);
taskSetStageEntity = setStage(this.getClass(), method.getName(), new Object[]{"我要开花!"}, method.getParameterTypes());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return new TaskRet("SUCCESS", taskSetStageEntity);
}

public TaskRet printMsg2(String msg) {
System.out.println("第二阶段开启中文打印: " + msg);
return new TaskRet("执行成功");
}

@Override
public TaskRet handleProcess() {
return printMsg("I did it!");
}

@Override
public TaskRet handleFinish() {
System.out.println("任务后置处理,可以自定义做点任务执行成功后的后置处理,例如回收资源等");
return new TaskRet("全部任务阶段执行完毕~");
}

@Override
public TaskRet handleError() {
System.out.println("任务最终执行失败干点事,可以自定义一些操作");
return new TaskRet("任务实在是执行不了了,还是人工检查一下吧~");
}

@Override
public TaskRet contextLoad(String context) {
System.out.println("上下文加载,用户可以根据自己定义的协议格式对上下文进行解析");
TaskContextEntity TaskContextEntity = JSON.parseObject(context, TaskContextEntity.class);
return new TaskRet(TaskContextEntity);
}
}
52 changes: 52 additions & 0 deletions ChronAnt-app/src/test/java/test/TaskCreateBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package test;

import cn.uhoc.domain.executor.model.entity.ScheduleLogEntity;
import cn.uhoc.domain.executor.model.entity.TaskContextEntity;
import cn.uhoc.domain.task.Executable;
import cn.uhoc.trigger.api.dto.TaskCreateReqDTO;
import cn.uhoc.type.common.ReflectionUtils;
import cn.uhoc.type.common.UserConfig;
import cn.uhoc.type.exception.ReflectionException;
import com.alibaba.fastjson.JSON;

import java.lang.reflect.Method;

/**
* 用于构建任务传输对象
*/
public class TaskCreateBuilder {

public static TaskCreateReqDTO build(Executable executable) throws NoSuchMethodException {
Class<? extends Executable> aClass = executable.getClass();
Method handProcess = aClass.getMethod("handleProcess");
return TaskCreateBuilder.build(aClass, handProcess.getName(), new Object[0], new Class[0]);
}

// 利用类信息创建任务
public static TaskCreateReqDTO build(Class<?> clazz, String methodName, Object[] params, Class<?>[] paramTypes, Object... envs) {
if (!Executable.class.isAssignableFrom(clazz)) {
throw new RuntimeException("The task must be implemented TaskDefinition!");
}
Method method;
try {
ReflectionUtils.checkParamsNum(params, paramTypes);
method = ReflectionUtils.getMethod(clazz, methodName, paramTypes);
} catch (ReflectionException e) {
throw new RuntimeException(e);
}

// 获取类名
String taskType = method.getDeclaringClass().getSimpleName();
// get 方法名
String taskStage = method.getName();
// 调度日志
ScheduleLogEntity sl = new ScheduleLogEntity();
String scheduleLog = JSON.toJSONString(sl);

// 上下文信息
TaskContextEntity taskContextEntity = new TaskContextEntity(params, paramTypes, envs);
String taskContext = JSON.toJSONString(taskContextEntity);
return new TaskCreateReqDTO(UserConfig.USERID, taskType, taskStage, scheduleLog, taskContext);
}

}

0 comments on commit 6883eb1

Please sign in to comment.