Java Spring Batch - 一个作业实例已经存在:JobInstanceAlreadyCompleteException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22455739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:49:34  来源:igfitidea点击:

Spring Batch - A job instance already exists: JobInstanceAlreadyCompleteException

javaspringbatch-processing

提问by ruhungry

I wrote an easy scheduler included in my Spring Application. I run my local server and after few seconds, in class Importer, checker.start() is being invoked each 5 seconds as how I configured in config file.

我在 Spring 应用程序中编写了一个简单的调度程序。我运行我的本地服务器,几秒钟后,在类 Importer 中,每 5 秒调用一次 checker.start(),就像我在配置文件中配置的那样。

After that, this method invokes Job with JobLauncher and here I have got an error.

之后,此方法使用 JobLauncher 调用 Job,这里出现错误。

org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for p arameters={}. If you want to run this job again, change the parameters.

org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException:作业实例已存在且参数={} 已完成。如果要再次运行此作业,请更改参数。

I found a solution how to fix it using annotation but I want to keep it this way.

我找到了一个如何使用注释修复它的解决方案,但我想保持这种方式。

Thank you in advance

先感谢您

public class Importer {

    private Checker checker;

    public Importer() {
    }

    public void myMethod() {
        try {
            checker.start(); 
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

with .xml file:

使用 .xml 文件:

<bean id="schedulerTask"
    class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="fileimport" />
    <property name="targetMethod" value="myMethod" />
</bean>

<bean id="fileimport" />
    <property name="targetMethod" value" class="com...Importer">
    <property name="checker">
        <bean id="checker" class="com...Checker">

        </bean>
    </property>
</bean>

<bean id="scheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="${xyz.startdelay}" />
    <property name="period" value="${xyz.checkinterval}" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="scheduledTask" />
        </list>
    </property>
</bean>

And property file:

和属性文件:

xyz.checkinterval=5000

xyz.startdelay=0

xyz.checkinterval=5000

xyz.startdelay=0

In class Checker I have got the method:

在 Checker 课程中,我得到了方法:

static ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/simplefile-context.xml");
Job job = (Job) applicationContext.getBean("fileBatch");
JobLauncher launcher = (JobLauncher) applicationContext.getBean("jobLauncher");
public void start() throws ClientProtocolException, IOException {
    // ...
    try {
        launcher.run(job, new JobParameters());
        } catch (Exception e) {
        e.printStackTrace();
    }
}

and my "simplefile-context.xml" file looks like this:

我的“simplefile-context.xml”文件如下所示:

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

<!-- bean for lauching the job -->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

<task:executor id="taskExecutor" pool-size="100" />

<!--                                   -->
<!--                JOBS               -->
<!--                                   -->
<batch:job id="fileBatch" restartable="true">
    <batch:step id="readLines" >
        <batch:tasklet task-executor="taskExecutor" >
            <batch:chunk reader="fileReader" writer="fooWriter" commit-interval="100" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="fileReader" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="linesToSkip" value="1"/>
    <property name="resource" value="file:./src/main/resources/sample.csv" />
    <property name="lineMapper" ref="lineMapper" />
</bean>

<bean id="lineMapper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
    <property name="lineTokenizer" ref="lineTokenizer"/>
    <property name="fieldSetMapper" ref="fieldsetEntityMapper"/>
</bean>

<bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
    <property name="delimiter" value=";" />
    <property name="names" value="field1,field2,field3,field4,field5,field6,field7" />
    <property name="strict" value="false"/>
</bean>

<bean id="fieldsetEntityMapper" class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
    <property name="targetType" value="com...model.ModelObject"/>
    <property name="customEditors">
        <map>
            <entry key="java.lang.Double">
                <bean class="org.springframework.beans.propertyeditors.CustomNumberEditor">
                    <constructor-arg index="0" value="java.lang.Double"/>
                    <constructor-arg index="1" value="true"/>
                </bean>
            </entry>
        </map>
    </property>
</bean>

<bean id="fooWriter" class="com...springbatch.writer.FooWriter" />

采纳答案by M. Deinum

With your current setup you will only be able to launch the job only once.

使用您当前的设置,您将只能启动一次作业。

    launcher.run(job, new JobParameters());

The job is unique identified by its idtogether with the parameters. Currently there is no way to make a distinction based on the parameters. Instead of adding new JobParameters()use the JobParamtersBuilderBuilderand add the current date and time.

作业是唯一的,由它id和参数一起标识。目前无法根据参数进行区分。而不是添加new JobParameters()使用JobParamtersBuilderBuilder并添加当前日期和时间。

JobParametersBuilder builder = new JobParametersBuilder();
builder.addDate("date", new Date());
launcher.run(job, builder.toJobParameters());

This will allow you to run the job multiple times.

这将允许您多次运行作业。

回答by Jeff Cook

Yon can also include the time to make it more unique as I was getting the same error and passing the same JobParameters in my testing.

Yon 还可以包括使其更加独特的时间,因为我在测试中遇到了相同的错误并传递了相同的 JobParameters。

JobParameters jobParameters = new JobParametersBuilder()
                .addDate("date", new Date())
                .addLong("time",System.currentTimeMillis()).toJobParameters();

回答by user10308858

In Application.property file Add new fields missing.

在 Application.property 文件中添加缺少的新字段。

spring.batch.initialize-schema=always spring.batch.job.enabled = false

spring.batch.initialize-schema=总是 spring.batch.job.enabled = false

回答by Yura

add the line to jobBuilderFactorychain

将线添加到jobBuilderFactory

.incrementer(new RunIdIncrementer())

next (as you run job manually with jobLauncherand with custom JobParameters)

下一步(当您使用jobLauncher和使用 custom手动运行作业时JobParameters

paramsBuilder.getNextJobParameters(job);

instead of.addDate("date", new Date());like they advicing you

而不是.addDate("date", new Date());像他们建议你