简体   繁体   中英

Spring Batch: How do I use placeholders in step listeners?

<beans>
<batch:job id="job1" restartable="false">
  <batch:step id="step1" next="step2">
    <batch:tasklet>
      <batch:chunk reader="readerA" writer="writerA" commit-interval="1" />
    </batch:tasklet>
  </batch:step>
  <batch:step id="step2">
    <batch:tasklet ref="tasklet1"/>
  </batch:step>
  <batch:listeners>
    <batch:listener ref="listenerA" />
  </batch:listeners>
</batch:job>

<bean id="listenerA" class="com.example.ListenerA" scope="step">
  <property name="archiveDate" value="#{jobParameters['jobRunDate']}" />
</bean>
</beans>

Why does the above give me the following error:

java.lang.IllegalStateException: No context available while replacing placeholders.

What would be the best way to work around this issue?

I believe the issue is that you wanted a step listener, and not a job listener. The step scope is out of scope where you've defined your listener, so scope step is unavailable - if it was a job listener, you wouldn't need the placeholders because you could obtain the variables from the current JobExecution.

If you instead altered your configuration like so (assuming this is a step listener and not a job listener):

<batch:step id="step1" next="step2">
<batch:tasklet>
  <batch:chunk reader="readerA" writer="writerA" commit-interval="1" />
  <batch:listeners>
      <batch:listener ref="listenerA" />
 </batch:listeners>
</batch:tasklet>
</batch:step>

I believe that would give you a proper step listener for the "step1" step - similar configuration if you wanted a listener for the second step.

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