简体   繁体   中英

Spring Batch same stepnames in multiple jobs?

I'm pretty confused by naming conventions of Spring Batch using spring-batch 2.1.8.RELEASE.

the main problem:

2 different Jobs, but with the same step inside (which will get different properties), which name isn't unique. If i try to run one of the jobs, for example job1. Then it gets the value "file2" for prop2, even it's defined for job2. ( only a abstraction example with not that much details of stepamount, listeners, etc.. ) Can't figure out any regularity or reason for that process.

<batch:job id="job1" parent="parentJob">
    <batch:step id="copyFile">
        <batch:tasklet>
            <bean class="xxx.xyz.classXXX"
                scope="step">
                <property name="prop1"
                    value="file1" />
            </bean>
        </batch:tasklet>
    </batch:step>
</batch>

Each job is defined in a own file.

<batch:job id="job2" parent="parentJob">
    <batch:step id="copyFile">
        <batch:tasklet>
            <bean class="xxx.xyz.classXXX"
                scope="step">
                <property name="prop2"
                    value="file2" />
            </bean>
        </batch:tasklet>
    </batch:step>
</batch>

We got about 80 jobs - each of them has the copyFile as first step. But there also steps in the middle of every job, which should be named equally. Is there any possibility, to avoid those injection-confusions/failures? Beside naming steps like "copyFile1", "copyFile2" and so on..

(names and properties are nothing but smoke and mirrors!)

Do you need any further information? Hope my explanation isn't too bad. Thank you in advance!

Cheers max.

Apparently step id's are global in spring batch (checked that today). They are loaded into a map upon context creation, and since they have the same id's the step read as last wins.

Workaround for this feature: use distinct step id's, for example with a prefix that is equal to batch:job id like this:

<batch:job id="job1" parent="parentJob">
  <batch:step id="job1_copyFile">
    <batch:tasklet>
      <bean class="xxx.xyz.classXXX" scope="step">
        <property name="prop1" value="file1" />
      </bean>
    </batch:tasklet>
  </batch:step>
</batch>

<batch:job id="job2" parent="parentJob">
  <batch:step id="job2_copyFile">
    <batch:tasklet>
      <bean class="xxx.xyz.classXXX" scope="step">
        <property name="prop2" value="file2" />
      </bean>
    </batch:tasklet>
  </batch:step>
</batch>

You have two anonymous beans that override each other since they do not have id property set although they have the same class.

Since having different parameters does not differentiate your beans, they are overriden.

Simply putting id attributes to your beans should solve your problem.

   <bean id="file1" class="xxx.xyz.classXXX"
        scope="step">
        <property name="prop1"
            value="file1" />
    </bean>

Hope that this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM