Announcement Announcement Module
No announcement yet.
optional jobParameter with CommandLineJobRunner Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • optional jobParameter with CommandLineJobRunner

    Hi, I've trouble with a jobParameter. I saw forum entries about "nullability" of a jobParameter but can't get it to work.

    I'm using Spring-batch 2.0.4 with CommandLineJobRunner.

    Use case:
    There is a job running every day, or supposed to.
    According to maintenance purposes it may not run some days.
    Unfortunately, in this case, the next time it will be launch we need to specify a date from witch the batch should "restart".
    But this date depends on "manual" calculation (some kind of business rules people in charge won't let us build in the code; there choice).

    So I need the following behaviour for the job:
    - it can run without jobParameter (calculate the correct date by itself)
    - it can be run with a jobParameter (ex : dateInit(date)=2009/12/31)

    Spring conf:
    <!-- Alimentation "principale" des données du distributeur -->
    <bean id="distributeurLoad" scope="step"
    	class="...processor.DistributeurLoaderProcessor" autowire="byType">
            <property name="dateInit" value="#{jobParameters[dateInit]}"/>
    If I run the batch with a value for the parameter everything works fine.
    But I couldn't find a way to launch without a value for the parameter.

    It throws an exception when I tried an empty parameter value on the command line : dateInit(date)=
    java.lang.IllegalArgumentException: Date format is invalid: [], use yyyy/MM/dd
            at org.springframework.batch.core.converter.DefaultJobParametersConverter.getJobParameters(
    It throws an exception when I tried an empty value without type informations : dateInit=
    Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date]: no matching editors or conversion strategy found
            at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(
            at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(
            at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(
    It throws an exception when I tried no parameter on the command line
    Caused by: java.lang.IllegalStateException: Cannot bind to placeholder: jobParameters[dateInit]
            at org.springframework.batch.core.scope.util.PlaceholderTargetSource$1.convertIfNecessary(
    Question :
    How can I get a null value in a date jobParameter when launching with CommandLineJobRunner?

    Thank you for your attention.
    Happy new year !

  • #2
    You can override the JobParametersConverter by providing one in the application context.


    • #3
      Thank you for the advice.
      I modified the DefaultJobParameterConverter into DefaultValueJobParameterConverter. It provides default values for types date, double and long. The default values are used as soon as the value of the parameter is empty (see javadoc for details).

      The behaviour for type date JobParameter is a little more complexe because it's not possible to have a null value. So if the defaultDate is configured to be null a new Date() is used instead.

      sample :
      CommandLineJobRunner testJob.xml testJob
      the value of will forced to DefaultValueJobParameterConverter.defaultDate
      If DefaultValueJobParameterConverter.defaultDate is null the value will be new Date()

      The source code an junit test is provided in the attachements:


      • #4
        How does this get wired to the CommandLineJobRunner? I also have a custom converter and cannot seem to get it "autowired." I am assuming that it would be autowired by the javadoc and according to Spring documentation similar to ExitCodeMapper ( ExitCodes).

        From xml:

        <bean id="jobParametersConverter" class="DefaultJobParametersConverterWithDefaulter"/>


        • #5
          Autowireing works for me

          In my case it works fine with autowireing.
          I'm using Spring-batch version 2.0.4.
          The only thing I had to do is declaring the parameter converter like this :
          	<bean id="jobParameterConverter" class="org.springframework.batch.core.converter.DefaultValueJobParameterConverter">
          		<property name="dateFormat">
          			<bean class="java.text.SimpleDateFormat">
          				<constructor-arg index="0" value="yyyy/MM/dd" />
          		<property name="defaultDate">
          		  <!-- Une valeur nulle oblige à positionner tout paramètre de type date à la valeur new Date() -->
          			<null />


          • #6
            Thanks. You're right. I was not testing via the CommandLineJobRunner in my debug scenario and thus there was no jobParameterConvert to autowire. Ooops. I will need to figure out a way to mock/simulate it in my job unit test.

            Thank you for the quick reply however!