maxstreifeneder
maxstreifeneder

Reputation: 635

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.

Upvotes: 4

Views: 5485

Answers (2)

LuGo
LuGo

Reputation: 5045

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>

Upvotes: 4

Serkan Arıkuşu
Serkan Arıkuşu

Reputation: 5619

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...

Upvotes: 0

Related Questions