Announcement Announcement Module
Collapse
No announcement yet.
optional jobParameter with CommandLineJobRunner Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:
    Code:
    <!-- Alimentation "principale" des données du distributeur -->
    <bean id="distributeurLoad" scope="step"
    	class="...processor.DistributeurLoaderProcessor" autowire="byType">
            <property name="dateInit" value="#{jobParameters[dateInit]}"/>
    </bean>
    Problem:
    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)=
    Code:
    java.lang.IllegalArgumentException: Date format is invalid: [], use yyyy/MM/dd
            at org.springframework.batch.core.converter.DefaultJobParametersConverter.getJobParameters(DefaultJobParametersConverter.java:95)
            at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:275)
            at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:448)
    It throws an exception when I tried an empty value without type informations : dateInit=
    Code:
    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(TypeConverterDelegate.java:231)
            at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
            at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:47)
    It throws an exception when I tried no parameter on the command line
    Code:
    Caused by: java.lang.IllegalStateException: Cannot bind to placeholder: jobParameters[dateInit]
            at org.springframework.batch.core.scope.util.PlaceholderTargetSource$1.convertIfNecessary(PlaceholderTargetSource.java:140)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1294)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1250)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    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.

    Comment


    • #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 schedule.date=
      the value of schedule.date 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:

      Comment


      • #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 (4.4.1.2. ExitCodes).

        From xml:

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

        Comment


        • #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 :
          Code:
          	<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" />
          			</bean>
          		</property>
          		<property name="defaultDate">
          		  <!-- Une valeur nulle oblige à positionner tout paramètre de type date à la valeur new Date() -->
          			<null />
          		</property>
          	</bean>

          Comment


          • #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!

            Comment

            Working...
            X