Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Passing Job Parameters to ItemWriter with Late Binding

Hybrid View

  1. #1
    Join Date
    Jan 2007
    Posts
    13

    Default Passing Job Parameters to ItemWriter with Late Binding

    I have recently started using spring-batch. I am trying to get the file name from Job Parameters that I can use for writer. So I am defining my bean definition for writer as scope="test".

    Code:
    	<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    	    <property name="resource" value="#{jobParameters['OUTFILE']}" />
    	    <property name="lineAggregator">
    	        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
    	            <property name="delimiter" value=","/>
    	            <property name="fieldExtractor">
    	                <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
    	                    <property name="names" value="policyId,version,vehicleNumber,coverageGroup"/>
    	                </bean>
    	            </property>
    	        </bean>
    	    </property>
    	</bean>
    The code for launcher is
    Code:
    	public void testLaunchJob() throws Exception {
    		JobParameters jobParameters = new JobParametersBuilder().addString("owner","NAME").addDate("currentTime", new Date()).addString("OUTFILE", "file:target/test-outputs/output2.txt").toJobParameters();
    		sampleJobLauncher.run(job, jobParameters);
    	}
    I made these changes based on few threads in the spring-batch forum and chapter-5 of spring-batch documentation. But when I run the job, I get the following errors

    Code:
    Caused by: java.io.FileNotFoundException: class path resource [#{jobParameters['OUTFILE']}] cannot be resolved to URL because it does not exist
    	at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:162)
    	at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:47)
    	at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:337)
    	... 55 more
    Any insights regarding this issue would be of great help.

    Thanks,
    Praveen

  2. #2
    Join Date
    May 2007
    Posts
    13

    Default

    Hi, try to remove the apostrophes when referency the parameter

    value="#{jobParameters[OUTFILE]}"

    You can look the format in the docs

  3. #3
    Join Date
    Jan 2007
    Posts
    13

    Default

    I tried with and without quotes. But it yeilds same result. I am using Spring 3.0 and Spring batch 2.1.

  4. #4

    Default

    you need the quotes. How do you load your job?

  5. #5
    Join Date
    Jan 2007
    Posts
    13

    Default

    If I understand your question correctly, I am loading it using job launcher.

  6. #6
    Join Date
    Jan 2007
    Posts
    13

    Default

    I am loading the spring context/ jobs from Junit testcase. The following is the code which does that
    Code:
    @ContextConfiguration(locations={"/launch-context.xml"})
    @RunWith(SpringJUnit4ClassRunner.class)
    public class ExampleJobConfigurationTests {
    	..........

    launch-context.xml has the job launcher configuration and it also imports few other spring configuration files where batch repository and other job definitions are defined.

    Code:
    	<import resource="classpath:batchframework-context.xml" />
    	<import resource="classpath:FL-batchjobs-context.xml" />
        
        
    	<bean id="jobLauncher"
    		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    		<property name="jobRepository" ref="batchJobRepository" />
    	</bean>

  7. #7
    Join Date
    Jan 2007
    Posts
    13

    Default

    Victor,
    I already have get/set methods for secondInput. I am enclosing my project. Let me know if you find something odd in the code.


    Thanks,
    Praveen
    Attached Files Attached Files

  8. #8
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    You have a mixture of Spring 2.5.6 and Spring 3.0 in there. If you add an explicit dependency on spring-context and spring-aop I think it will work. (The sample project is fixed in trunk by the way, it just didn't make it to STS yet.)

  9. #9
    Join Date
    Jan 2007
    Posts
    13

    Default

    Dave,
    It worked after adding explicit dependency. I did not pay attention to the indirect dependency. I just checked spring-core and spring-beans versions and thought everything was OK. I was looking more at job initialization and job config files. Thanks for pointing out. I can move forward now.

    Thanks

  10. #10
    Join Date
    Apr 2010
    Posts
    6

    Default

    Dave,
    Is there any examples for using late-binding, especially for files. Maybe I missed something, but I didn't find a real helpful example. I also spent a lot of time to make it work for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •