英文:
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_NAME:correlationId
STRING_VAL:eaa81b53-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,我需要从中获取包括BatchStatus、ExitStatus等在内的作业执行结果,以构建我的响应对象。
我的问题是,我如何查询以根据提供的参数值获取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<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);
专注分享java语言的经验与见解,让所有开发者获益!



评论