如何将中止作业的ExitStatus设置为“中止原因”消息?

huangapple go评论46阅读模式
英文:

How to set ExitStatus of aborted job to "reason for abort" message?

问题

I need to abort a running job and set its ExitStatus to "reason for abort".

It's quite easy to abort a running job:

try {
    if (jobOperator.stop(jobExecutionId)) {
        jobOperator.abandon(jobExecutionId);
        log.info("The job with JobId :" + jobExecutionId + " was canceled.");
    }
}

And yes, I know, the only way to set the ExitStatus is to use AfterJob in JobExecutionListener (https://stackoverflow.com/questions/30106218/setting-exit-message-in-batch-job-execution)

But how can I transfer "reason for abort" message from the code that abort the job to JobExecutionListener's AfterJob?

英文:

I need to abort a running job and set its ExitStatus to "reason for abort".

It's quite easy to abort a running job:

try {
    if (jobOperator.stop(jobExecutionId)) {
        jobOperator.abandon(jobExecutionId);
        log.info("The job with JobId :" + jobExecutionId + " was canceled.");
    }
}

And yes, I know, the only way to set the ExitStatus is to use AfterJob in JobExecutionListener (https://stackoverflow.com/questions/30106218/setting-exit-message-in-batch-job-execution)

But how can I transfer "reason for abort" message from the code that abort the job to JobExecutionListener's AfterJob?

答案1

得分: 1

无法将“中止原因”消息从中止作业的代码传递到JobExecutionListener的AfterJob方法中。你只能从“外部”(通过作业操作者)传递停止信号到“内部”(作业执行及其监听器)。其他所有操作都可以在停止或中止作业后在作业执行本身中完成,类似于:

try {
    if (jobOperator.stop(jobExecutionId)) {
        jobOperator.abandon(jobExecutionId);
        JobExecution jobExecution = jobExplorer.getJobExecution(jobExecutionId);
        jobExecution.setExitStatus(new ExitStatus("ABORTED", "中止原因"));
        jobRepository.update(jobExecution);
        log.info("作业ID为:" + jobExecutionId + " 的作业已被取消。");
    }
}
英文:

> how can I transfer "reason for abort" message from the code that abort the job to JobExecutionListener's AfterJob?

There is no way to do that. The only information that you can pass from the "outside" (through the job operator) to the "inside" (the job execution and its listeners) is the stop signal.

Everything else can be done on the job execution itself after stopping or aborting it, something like:

try {
    if (jobOperator.stop(jobExecutionId)) {
        jobOperator.abandon(jobExecutionId);
        JobExecution jobExecution = jobExplorer.getJobExecution(jobExecutionId);
        jobExecution.setExitStatus(new ExitStatus("ABORTED", "reason for abort"));
        jobRepository.update(jobExecution);
        log.info("The job with JobId :" + jobExecutionId + " was canceled.");
    }
}

huangapple
  • 本文由 发表于 2023年2月7日 01:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定