查询带有批处理参数值的Spring Batch JobExecution

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

Querying Spring Batch JobExecution with Batch Param values

问题

鉴于存在一个Spring Batch表,它通过键值捕获所有作业执行参数:

create table BATCH_JOB_EXECUTION_PARAMS
(
    JOB_EXECUTION_ID BIGINT not null,
    TYPE_CD VARCHAR(6) not null,
    KEY_NAME VARCHAR(100) not null,
    STRING_VAL VARCHAR(250),
    DATE_VAL TIMESTAMP(26,6) default NULL,
    LONG_VAL BIGINT,
    DOUBLE_VAL DOUBLE,
    IDENTIFYING CHAR(1) not null,
    constraint JOB_EXEC_PARAMS_FK
        foreign key (JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION
);

例如:

KEY_NAMEcorrelationId
STRING_VALeaa81b53-7f7d-4637-9935-b765405756be

我可以查询以检索JOB_EXECUTION_ID

SELECT JOB_EXECUTION_ID FROM BATCH_JOB_EXECUTION_PARAMS
WHERE KEY_NAME = 'correlationId' AND STRING_VAL = 'eaa81b53-7f7d-4637-9935-b765405756be';

然后,我可以查询BATCH_JOB_EXECUTION表以获取作业执行的完整详细信息。

我有一个REST端点,客户端提供了这个correlationId,我需要从中获取包括BatchStatusExitStatus等在内的作业执行结果,以构建我的响应对象。

我的问题是,我如何查询以根据提供的参数值获取JobExecution对象?是否有办法使用JobExplorer来实现这一点?

英文:

Given that there is a Spring batch table that captures all the job execution params by key value:

create table BATCH_JOB_EXECUTION_PARAMS
(
	JOB_EXECUTION_ID BIGINT not null,
	TYPE_CD VARCHAR(6) not null,
	KEY_NAME VARCHAR(100) not null,
	STRING_VAL VARCHAR(250),
	DATE_VAL TIMESTAMP(26,6) default NULL,
	LONG_VAL BIGINT,
	DOUBLE_VAL DOUBLE,
	IDENTIFYING CHAR(1) not null,
	constraint JOB_EXEC_PARAMS_FK
		foreign key (JOB_EXECUTION_ID) references BATCH_JOB_EXECUTION
);

For example:

KEY_NAME correlationId
STRING_VAL eaa81b53-7f7d-4637-9935-b765405756be

I can query to retrieve the JOB_EXECUTION_ID

SELECT JOB_EXECUTION_ID FROM BATCH_JOB_EXECUTION_PARAMS
WHERE KEY_NAME = 'correlationId' AND STRING_VAL = 'eaa81b53-7f7d-4637-9935-b765405756be'

Then I can query the BATCH_JOB_EXECUTION table for full details of the job execution.

I have a rest endpoint where client provided this correlationId from which I need to grab the results of the job execution that includes BatchStatus, ExitStatus etc to build my response object.

My question is, how can I query to grab the JobExecution object from this param value provided? Is there anyway I can do this using JobExplorer?

答案1

得分: 1

使用 org.springframework.batch.core.explore.JobExplorer 来搜索作业执行情况。如果你有 executionId,可以使用 getJobExecution

这是由 SpringBatch 定义的一个 bean,因此你可以在某个地方简单地自动装配它:@Autowired JobExplorer jobExplorer

还有一些方法,比如 findRunningJobExecutions 用于查找正在运行的作业。

英文:

Use the org.springframework.batch.core.explore.JobExplorer to search for job executions. If you have the executionId you can use getJobExecution.

This is a bean defined by SpringBatch so you can simply auto wire it in somewhere @Autowired JobExplorer jobExplorer.

There are also methods such as findRunningJobExecutions to find running jobs.

答案2

得分: 0

你可以使用 JobRepository#getLastJobExecution(String jobName, JobParameters parameters) 进行操作:

Map<String, JobParameter> parameters = new HashMap<>();
parameters.put("correlationId", new JobParameter("eaa81b53-7f7d-4637-9935-b765405756be", true));
JobParameters jobParameters = new JobParameters(parameters);
JobExecution jobExecution = jobRepository.getLastJobExecution("myJob", jobParameters);
英文:

You can do that using JobRepository#getLastJobExecution(String jobName, JobParameters parameters):

Map&lt;String, JobParameter&gt; parameters = new HashMap&lt;&gt;();
parameters.put(&quot;correlationId&quot;, new JobParameter(&quot;eaa81b53-7f7d-4637-9935-b765405756be&quot;, true));
JobParameters jobParameters = new JobParameters(parameters);
JobExecution jobExecution = jobRepository.getLastJobExecution(&quot;myJob&quot;, jobParameters);

huangapple
  • 本文由 发表于 2020年7月24日 06:52:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064254.html
匿名

发表评论

匿名网友

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

确定