schedulerx 脚本任务
官网:https://help.aliyun.com/document_detail/148195.html
相关类与接口
ShellProcessor
public class ShellProcessor implements JobProcessorEx {
private Process shellProcess = null;
private LogCollector logCollector = LogCollectorFactory.get();
protected static final Logger LOGGER = LogFactory.getLogger(ShellProcessor.class);
private String uniqueId = null;
public ShellProcessor() {
}
public ProcessResult process(JobContext context) {
ProcessResult result = new ProcessResult(false);
this.logCollector.collect(this.uniqueId, "Script process start to run, taskName=" + context.getTaskName());
try {
ProcessBuilder processBuilder = ShellUtil.createProcessBuilder(this.getContent(context));
if (this.redirectStream()) {
processBuilder.redirectErrorStream(true);
}
this.shellProcess = processBuilder.start();
CountDownLatch countDownLatch = null;
ShellStreamProcessor stderrStreamProcessor;
if (this.redirectStream()) {
countDownLatch = new CountDownLatch(1);
stderrStreamProcessor = new ShellStreamProcessor(this, this.shellProcess.getInputStream(), StreamType.STD_ERR, countDownLatch);
stderrStreamProcessor.start();
} else {
countDownLatch = new CountDownLatch(2);
stderrStreamProcessor = new ShellStreamProcessor(this, this.shellProcess.getInputStream(), StreamType.STD_OUT, countDownLatch);
stderrStreamProcessor.start();
Thread stderrStreamProcessor = new ShellStreamProcessor(this, this.shellProcess.getErrorStream(), StreamType.STD_ERR, countDownLatch);
stderrStreamProcessor.start();
}
countDownLatch.await();
if (this.shellProcess.waitFor() == 0) {
result.setStatus(true);
}
} catch (Throwable var10) {
LOGGER.error("", var10);
this.logCollector.collect(this.uniqueId, "script process errors", var10);
result.setResult(ExceptionUtil.getMessage(var10));
} finally {
this.logCollector.collect(this.uniqueId, "Script process end, run status=" + result.getStatus().getEnDesc());
return result;
}
}
protected String[] getContent(JobContext context) {
String[] parameters;
if (StringUtils.isNotEmpty(context.getShardingParameter())) {
parameters = new String[]{String.valueOf(context.getShardingId()), context.getShardingParameter()};
} else if (StringUtils.isNotEmpty(context.getInstanceParameters())) {
parameters = context.getInstanceParameters().trim().split(" ");
} else {
parameters = context.getJobParameters().trim().split(" ");
} //脚本参数
String[] contents = new String[3 + parameters.length];
contents[0] = "/bin/sh";
contents[1] = "-c";
contents[2] = context.getContent(); //从context中读取脚本内容
for(int i = 0; i < parameters.length; ++i) {
contents[3 + i] = parameters[i];
}
return contents;
}
protected void processStdOutputStream(InputStream inputStream) {
String line = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
while((line = br.readLine()) != null) {
this.logCollector.collect(this.uniqueId, line);
}
} catch (Throwable var7) {
LOGGER.error("error ShellJobProcessor stdout stream", var7);
this.logCollector.collect(this.uniqueId, "error process stdout stream", var7);
} finally {
this.logCollector.collect(this.uniqueId, line, StreamType.STD_OUT, true);
}
}
protected void processStdErrorStream(InputStream inputStream) {
String line = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
while((line = br.readLine()) != null) {
this.logCollector.collect(this.uniqueId, line);
}
} catch (Throwable var7) {
LOGGER.error("error ShellJobProcessor stderr stream", var7);
this.logCollector.collect(this.uniqueId, "error process stderr stream", var7);
} finally {
this.logCollector.collect(this.uniqueId, line, StreamType.STD_OUT, true);
}
}
protected boolean redirectStream() {
return true;
}
public ProcessResult postProcess(JobContext context) {
return null;
}
public void kill(JobContext context) {
try {
long pid = ShellUtil.getPidOfProcess(this.shellProcess);
if (pid > 0L) {
ShellUtil.killProcess(pid);
}
} catch (Throwable var5) {
LOGGER.error("kill shell job jobInstanceId={} failed, {}", new Object[]{context.getJobInstanceId(), var5});
}
try {
if (this.shellProcess != null) {
this.shellProcess.destroy();
}
} catch (Throwable var4) {
LOGGER.error("kill shell job jobInstanceId={} failed, {}", new Object[]{context.getJobInstanceId(), var4});
}
}
public void preProcess(JobContext context) throws Exception {
this.uniqueId = context.getUniqueId();
}
}
JobContext
public final class JobContext {
private long jobId;
private long jobInstanceId;
private Long wfInstanceId;
private long taskId;
private String jobName;
private DateTime scheduleTime;
private DateTime dataTime;
private String executeMode;
private String jobType;
private String instanceMasterActorPath;
private String taskName;
private Object task;
private String groupId;
private String content;
private String user;
private int maxAttempt;
private int attempt;
private String jobParameters;
private String instanceParameters;
private List<JobInstanceData> upstreamData;
private Map<Long, String> taskResults;
private Map<Long, TaskStatus> taskStatuses;
private int taskMaxAttempt;
private int taskAttempt;
private int taskAttemptInterval;
private long serialNum;
private LogCollector logCollector;
private Long shardingId;
private String shardingParameter;
private int shardingNum;
private List<String> allWorkerAddrs;
private String workerAddr;
private int timeType;
private String timeExpression;
private JobContext() {
this.taskId = 0L;
this.taskAttempt = 0;
this.shardingNum = 0;
}
public void setTaskAttempt(int taskAttempt) {
public void setInstanceParameters(String instanceParameters) {
public long getJobId() {
public String getJobName() {
public String getJobType() {
public long getJobInstanceId() {
public String getUser() {
public String getGroupId() {
public String getUniqueId() {
public Long getWfInstanceId() {
public String getExecuteMode() {
public List<JobInstanceData> getUpstreamData() {
public Object getTask() {
public long getTaskId() {
public String getTaskName() {
public Map<Long, String> getTaskResults() {
public Map<Long, TaskStatus> getTaskStatuses() {
public String getContent() {
public String getJobParameters() {
public String getInstanceParameters() {
public int getTimeType() {
public DateTime getDataTime() {
public DateTime getScheduleTime() {
public String getTimeExpression() {
public long getSerialNum() {
public int getShardingNum() {
public Long getShardingId() {
public String getShardingParameter() {
public int getAttempt() {
public int getMaxAttempt() {
public int getTaskAttempt() {
public int getTaskMaxAttempt() {
public int getTaskAttemptInterval() {
public String getTaskMasterActorPath() {
public String getInstanceMasterActorPath() {
public String getWorkerAddr() {
public List<String> getAllWorkerAddrs() {
private void collectLog(String log) {
*********
获取JobContext.JobContextBuilder实例
public static JobContext.JobContextBuilder newBuilder() {
return new JobContext.JobContextBuilder();
}
public static JobContext.JobContextBuilder newBuilder(JobContext context) {
return new JobContext.JobContextBuilder(context);
}
*********
内部类:JobContextBuilder
public static final class JobContextBuilder {
private JobContext context;
private JobContextBuilder() {
this.context = new JobContext();
}
private JobContextBuilder(JobContext context) {
this.context = new JobContext();
this.context = context;
}
public JobContext.JobContextBuilder setJobId(long jobId) {
public JobContext.JobContextBuilder setJobName(String jobName) {
public JobContext.JobContextBuilder setJobType(String jobType) {
public JobContext.JobContextBuilder setJobInstanceId(long jobInstanceId) {
public JobContext.JobContextBuilder setWfInstanceId(long wfInstanceId) {
public JobContext.JobContextBuilder setTask(Object task) {
public JobContext.JobContextBuilder setTaskId(long taskId) {
public JobContext.JobContextBuilder setTaskName(String taskName) {
public JobContext.JobContextBuilder setTaskResults(Map<Long, String> taskResults) {
public JobContext.JobContextBuilder setTaskStatuses(Map<Long, TaskStatus> taskStatuses) {
public JobContext.JobContextBuilder setTaskMasterActorPath(String actorPath) {
public JobContext.JobContextBuilder setUser(String user) {
public JobContext.JobContextBuilder setGroupId(String groupId) {
public JobContext.JobContextBuilder setDataTime(DateTime dataTime) {
public JobContext.JobContextBuilder setSerialNum(long serialNum) {
public JobContext.JobContextBuilder setExecuteMode(String executeMode) {
public JobContext.JobContextBuilder setUpstreamData(List<JobInstanceData> upstreamData) {
public JobContext.JobContextBuilder setTimeType(int timeType) {
public JobContext.JobContextBuilder setScheduleTime(DateTime scheduleTime) {
public JobContext.JobContextBuilder setTimeExpression(String timeExpression) {
public JobContext.JobContextBuilder setContent(String content) {
public JobContext.JobContextBuilder setJobParameters(String jobParameters) {
public JobContext.JobContextBuilder setInstanceParameters(String instanceParameters) {
public JobContext.JobContextBuilder setAttempt(int attempt) {
public JobContext.JobContextBuilder setMaxAttempt(int maxAttempt) {
public JobContext.JobContextBuilder setTaskMaxAttempt(int taskMaxAttempt) {
public JobContext.JobContextBuilder setTaskAttemptInterval(int taskAttemptInterval) {
public JobContext.JobContextBuilder setShardingId(long shardingId) {
public JobContext.JobContextBuilder setShardingNum(int shardingNum) {
public JobContext.JobContextBuilder setShardingParameter(String shardingParameter) {
public JobContext.JobContextBuilder setAllWorkerAddrs(List<String> allWorkerAddrs) {
public JobContext.JobContextBuilder setWorkerAddrs(String workerAddr) {
public JobContext build() {
脚本任务
本地创建任务:继承shellProcessor,设置脚本内容、参数,启动后接入控制台
控制台创建任务:直接在控制台创建任务,设置脚本内容、参数
shell脚本执行:shell脚本在shedulerx客户端(利用本地资源)执行
本地创建任务
application.yml
spring:
schedulerx2:
aliyunAccessKey: ***
aliyunSecretKey: ***
regionId: public
endpoint: acm.aliyun.com
namespaceName: lihu-test
namespace: ***
appName: lihu-job
groupId: lihu-group
appKey: OOs+XPhRsAa1Ia6+Op+NAw==
jobs:
simpleJob:
jobModel: standalone
className: com.example.demo.job.CustomJob
cron: "*/2 * * * * ?"
shellJob: #shell脚本任务,任务类型为java(客户端强制设置,不可设置为shell会报错)
jobModel: standalone
className: com.example.demo.job.CustomShellJob
cron: "0 */1 * * * ?"
CustomShellJob
public class CustomShellJob extends ShellProcessor {
@Override
public ProcessResult process(JobContext context) {
context = JobContext.newBuilder(context)
.setContent("echo 'hello shell脚本'").build();
return super.process(context);
}
}
*************
使用测试
查看任务控制台:shellJob任务类型为java
任务执行日志
说明:ip为本地主机ip地址
控制台创建任务
任务管理,创建应用
shell脚本任务
开启本地shedulerx客户端,设置相关参数,脚本任务利用本地客户端资源执行
shell脚本执行日志