Announcement Announcement Module
Collapse
No announcement yet.
JobParameters in job.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JobParameters in job.xml

    Can I access JobParameter in job.xml file?
    Suppose I launched a job with JobParameters (key->"key1", value="value1").
    How do i use the job parameter "key1" in my job.xml?

  • #2
    I'm not sure I understand what you mean? An xml file can't really capture runtime parameters. Or do you mean, you want to use the value from a JobParameter in say, a sql statement?

    Comment


    • #3
      Yes, this is what i exactly meant. I want to use in sql statement.

      [Or do you mean, you want to use the value from a JobParameter in say, a sql statement?[/QUOTE]

      Comment


      • #4
        If using the JdbcCursorItemReader.....use StepExecutionPreparedStatementSetter to accomplish this.

        Comment


        • #5
          I am using JdbcCursorItemReader.
          Would you please give me one example about how to configure StepExecutionPreparedStatementSetter with my JdbcCursorItemReader in job xml?

          Originally posted by lucasward View Post
          If using the JdbcCursorItemReader.....use StepExecutionPreparedStatementSetter to accomplish this.

          Comment


          • #6
            Here it is in code, from the unit test, you would just need to do the same in xml, which should be straightforward:

            Code:
            		itemReader = new JdbcCursorItemReader<Foo>();
            		itemReader.setDataSource(dataSource);
            		itemReader.setSql("select ID, NAME, VALUE from T_FOOS where ID > ? and ID < ?");
            		itemReader.setIgnoreWarnings(true);
            		itemReader.setVerifyCursorPosition(true);
            		
            		itemReader.setMapper(new FooRowMapper());
            		itemReader.setFetchSize(10);
            		itemReader.setMaxRows(100);
            		itemReader.setQueryTimeout(1000);
            		itemReader.setSaveState(true);
            		StepExecutionPreparedStatementSetter pss = new StepExecutionPreparedStatementSetter();
            		JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", 1L).addLong("end.id", 4L).toJobParameters();
            		
            		List<String> parameterNames = new ArrayList<String>();
            		parameterNames.add("begin.id");
            		parameterNames.add("end.id");
            		pss.setParameterKeys(parameterNames);
            		
            		itemReader.setPreparedStatementSetter(pss);

            Comment


            • #7
              Thank you for your valuable input.

              I have a main itemReader(JdbcCursorItemReader) and a delegating Item reader(com.batch.item.reader.CustomReader extends DelegatingItemReader).
              My job config is given below:

              <property name="streams">
              <list>
              <ref bean="parentReader" />
              </list>
              </property>

              <property name="itemReader">
              <bean class="com.batch.item.reader.CustomReader">
              <property name="itemReader" ref="parentReader" />
              <property name="dataSource" ref="dataSource" />
              </bean>
              </property>

              <bean id="parentReader" class="org.springframework.batch.item.database.Jdb cCursorItemReader">
              <property name="dataSource" ref="dataSource" />
              <property name="mapper">
              <bean class="com.batch.mapping.ParentRowMapper" />
              </property>
              <property name="sql" value="SELECT id, name FROM SRC_PARENT WHERE name=?" />
              </property>
              </bean>

              I added the following config to my "parentReader":
              <property name="preparedStatementSetter">
              <bean class="org.springframework.batch.core.resource.Ste pExecutionPreparedStatementSetter">
              <property name="parameterKeys" value="nameP" />
              </bean>
              </property>

              I ran like
              CommandLineJobRunner jobs/poc.xml pocJob nameP=EFG4

              But I am getting JobParameters as null, so null pointer on first line of StepExecutionPreparedStatementSetter:setValues().

              Is there anything extra i need to set here?

              Comment


              • #8
                You need to register the StepExecutionPreparedStatementSetter as a listener to the step.

                Comment


                • #9
                  Thank you very much. Thanks a lot.
                  It worked fine.

                  Comment


                  • #10
                    I have to generate a report(flat file) after execution of each step or in case of errors. (Or i want to do some notification via email on error.)
                    What my plan now is to generate the report using FlatFileItemWriter in the methods provided by the listeners (afterStep(),onError(), etc...).
                    Is there any other approach to accomplish the same in a better way?

                    Comment


                    • #11
                      "If the input is not a String and a converter is available the converter will be applied and then this method recursively called with the result.If no converter is supplied the input object's toString method will be used."

                      This is mentioned in javadoc of FlatFileItemWriter:write() method.
                      What is actually the "converter" and how it can be used?

                      Comment


                      • #12
                        listener

                        Is there any listener method which will be executed always regardslesswhether the job completed or failed or interrupted.

                        I found that the methods in job level listener will be notified in various scenarios. afterJob() will get notified only if job is completed. onError() will get notified if any error in job. So would you please tell me if any listener method is available that will be executed always?

                        Comment


                        • #13
                          In 1.1, no, you will need to implement all three methods to always be notified. In working on another issue in 2.0, I've advocated moving away from that and having one method that is always called:

                          http://jira.springframework.org/browse/BATCH-825

                          If you feel strongly about this, please contribute to the jira issue.

                          Comment


                          • #14
                            You need to register the StepExecutionPreparedStatementSetter as a listener to the step.
                            How do I do this?

                            Comment


                            • #15
                              From section 5.1.10 in the documentation:
                              Code:
                                <step id="step1">
                                  <tasklet reader="reader" writer="writer" commit-interval="10"/>
                                    <listeners>
                                      <listener ref="stepListener"/>
                                    </listeners>
                                </step>

                              Comment

                              Working...
                              X