作业实例已存在且参数完整
A job instance already exists and is complete for parameters
每当我尝试 运行 异步作业时,我都会收到此错误
A job instance already exists and is complete for
parameters={fileName=D:\experimentemployeeCSVFile.csv}. If you
want to run this job again, change the parameters.
这是我正在尝试做的事情:
@Autowired
JobLauncher jobLauncher;
@Autowired
@Qualifier("importEmployeeJob")
Job job;
@RequestMapping("/jobLauncher")
public ResponseEntity<String> handle() throws Exception {
log.info("Rest request to handle()");
Thread async = new Thread(new Runnable() {
@Override
public void run() {
String fileLocation = "D:\experiment\31employeeCSVFile.csv";
Map<String, JobParameter> JobParameters = new HashMap<String, JobParameter>();
JobParameters.put("fileName", new JobParameter(fileLocation));
try {
jobLauncher.run(job, new JobParameters(JobParameters));
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
e.printStackTrace();
}
log.info("success");
}
});
log.info("Starting job...");
async.start();
return ResponseEntity.ok("Job started");
}
完整的异常跟踪:
org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={fileName=D:\experimentemployeeCSVFile.csv}. If you want to run this job again, change the parameters.
at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:131)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean.invoke(AbstractJobRepositoryFactoryBean.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy63.createJobExecution(Unknown Source)
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy68.run(Unknown Source)
at com.diatoz.demo.rest.EmployeeResource.run(EmployeeResource.java:61)
at java.lang.Thread.run(Unknown Source)
至少第一次应该运行。我做错了什么?
还有一点要补充的是,如果我不使用 运行nable,那么一切都很好。
更新:
根据错误消息,我尝试设置一个唯一的作业参数(即时间戳),它 运行。但是我仍然很困惑为什么它甚至是第一次拒绝运行。
您的数据库中应该已经有一个带有该参数的作业实例(可能是在您之前的测试中创建的?)。使用新的数据库,您不应该在第一个 运行、 中出现此错误,除非 两个请求同时到来并尝试创建具有相同参数的作业实例并且其中一个在另一个尝试创建作业实例之前完成。(这不太可能发生)。
除此之外,您不应该手动创建线程来异步启动作业。您需要做的是使用异步 TaskExecutor
实现来配置您的作业启动器。有关详细信息,请参阅 Running Jobs from within a Web Container 部分。
每当我尝试 运行 异步作业时,我都会收到此错误
A job instance already exists and is complete for parameters={fileName=D:\experimentemployeeCSVFile.csv}. If you want to run this job again, change the parameters.
这是我正在尝试做的事情:
@Autowired
JobLauncher jobLauncher;
@Autowired
@Qualifier("importEmployeeJob")
Job job;
@RequestMapping("/jobLauncher")
public ResponseEntity<String> handle() throws Exception {
log.info("Rest request to handle()");
Thread async = new Thread(new Runnable() {
@Override
public void run() {
String fileLocation = "D:\experiment\31employeeCSVFile.csv";
Map<String, JobParameter> JobParameters = new HashMap<String, JobParameter>();
JobParameters.put("fileName", new JobParameter(fileLocation));
try {
jobLauncher.run(job, new JobParameters(JobParameters));
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
e.printStackTrace();
}
log.info("success");
}
});
log.info("Starting job...");
async.start();
return ResponseEntity.ok("Job started");
}
完整的异常跟踪:
org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={fileName=D:\experimentemployeeCSVFile.csv}. If you want to run this job again, change the parameters. at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:131) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean.invoke(AbstractJobRepositoryFactoryBean.java:181) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy63.createJobExecution(Unknown Source) at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:137) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) at com.sun.proxy.$Proxy68.run(Unknown Source) at com.diatoz.demo.rest.EmployeeResource.run(EmployeeResource.java:61) at java.lang.Thread.run(Unknown Source)
至少第一次应该运行。我做错了什么?
还有一点要补充的是,如果我不使用 运行nable,那么一切都很好。
更新:
根据错误消息,我尝试设置一个唯一的作业参数(即时间戳),它 运行。但是我仍然很困惑为什么它甚至是第一次拒绝运行。
您的数据库中应该已经有一个带有该参数的作业实例(可能是在您之前的测试中创建的?)。使用新的数据库,您不应该在第一个 运行、 中出现此错误,除非 两个请求同时到来并尝试创建具有相同参数的作业实例并且其中一个在另一个尝试创建作业实例之前完成。(这不太可能发生)。
除此之外,您不应该手动创建线程来异步启动作业。您需要做的是使用异步 TaskExecutor
实现来配置您的作业启动器。有关详细信息,请参阅 Running Jobs from within a Web Container 部分。