Results 1 to 7 of 7

Thread: How to personalize the name of output file

  1. #1
    Join Date
    Jan 2009
    Posts
    3

    Default How to personalize the name of output file

    Hello,

    i want to personaliez the name of outputfile writer using the current date:
    for example
    the writer is :

    <bean id="writer" class="org.springframework.batch.item.file.FlatFil eItemWriter">
    <property name="resource" ref= "${output_file_name}date_execution_batch" />
    <property name="shouldDeleteIfExists" value="true" />
    <property name="fieldSetCreator">
    <bean class="org.springframework.batch.item.file.mapping .PassThroughFieldSetMapper"/>
    </property>
    </bean>


    in properties file:
    output_file_name=file://temp/myfile


    any issues??
    thanks

  2. #2
    Join Date
    Feb 2008
    Posts
    488

    Default

    In Spring Batch 1.x you can use the StepExecutionResourceProxy [1] to extract the date from the jobParameters. For this to work you have to register the proxy as a listener on the step.
    Code:
    <property name="resource" ref="fileLocator" />
    
    <bean id="fileLocator" class="org.springframework.batch.core.resource.StepExecutionResourceProxy">
        <property name="filePattern" value="file://temp/myfile%schedule.date%.txt"/>
    </bean>
    In Spring Batch 2.0 you will be able to use the late-binding feature to say something like:
    Code:
    <property name="resource" value="file://temp/myfile#{jobParameters[schedule.date]}.txt" />

    [1] http://static.springframework.org/sp...urceProxy.html

  3. #3
    Join Date
    Jan 2009
    Posts
    3

    Default

    Quote Originally Posted by DHGarrette View Post
    In Spring Batch 1.x you can use the StepExecutionResourceProxy [1] to extract the date from the jobParameters. For this to work you have to register the proxy as a listener on the step.
    Code:
    <property name="resource" ref="fileLocator" />
    
    <bean id="fileLocator" class="org.springframework.batch.core.resource.StepExecutionResourceProxy">
        <property name="filePattern" value="file://temp/myfile%schedule.date%.txt"/>
    </bean>
    In Spring Batch 2.0 you will be able to use the late-binding feature to say something like:
    Code:
    <property name="resource" value="file://temp/myfile#{jobParameters[schedule.date]}.txt" />

    [1] http://static.springframework.org/sp...urceProxy.html
    Thanks.
    i have to specify in the name of result file to parameters: the first one is the current date, the other is a parameter passing during the execution of batch.
    so i add this bloc of code in context.xml :
    1: in job
    <property name="listeners" >
    <list>
    <ref ean="outputFile"/>
    </list>
    </property>
    2: in writer
    <bean id="writer" class="org.springframework.batch.item.file.FlatFil eItemWriter">
    <property name="resource" ref= "outputFile" />
    <property name="shouldDeleteIfExists" value="true" />
    <property name="fieldSetCreator">
    <bean class="org.springframework.batch.item.file.mapping .PassThroughFieldSetMapper"/>
    </property>
    </bean>
    3: the bean outputfile
    <bean id="outputFile" class="org.springframework.batch.core.resource.Ste pExecutionResourceProxy" >
    <property name="filePattern" value="${file-name}%param1%${param_date_batch}#%datebatch%}${fil e_extension}" />
    </bean>
    4: adding param in properties file:
    param_date_batch,file_extension and file-name

    i executed the batch . the result i get thevar "param1" with the correct value, but i don't know how to give to datebatch a value during execution.
    should i implements a another class where can i specify paramdate.
    i also want to modify the value of "param2" beforing using it in the name of result file.

    thks for your help

  4. #4
    Join Date
    Apr 2008
    Posts
    174

    Default

    Why dont you have a step listener and inject the writer into it? Then in before step you can set the resource to whatever name you want. Somethinglike this:
    File outputFile = new File("dynamicFileName");
    writer.setResource(new FileSystemResource(outputFile));

  5. #5
    Join Date
    Feb 2008
    Posts
    488

    Default

    hailspring,

    I think the downside to the way you've described is that it moves the specification of the filename out from the config file and into a Java class.

  6. #6
    Join Date
    Feb 2008
    Posts
    488

    Default

    Quote Originally Posted by springo View Post
    i executed the batch . the result i get thevar "param1" with the correct value, but i don't know how to give to datebatch a value during execution.
    should i implements a another class where can i specify paramdate.
    i also want to modify the value of "param2" beforing using it in the name of result file.
    springo,

    I'm a little confused. How exactly are you entering the job parameters? Is it on the command line? Or programmatically in Java code?

  7. #7
    Join Date
    Jan 2009
    Posts
    3

    Unhappy

    hailspring,
    I use a propeties file (contain a list of parameter) where i specify the prefix and path of output file.

    DHGarrette,

    for the first parameter , i enter it in command line.
    for example ./mybatch.sh param1

    and in the context file:

    i specify a listener :
    <property name="listeners" >
    <list>
    <ref bean="stepExecution"/>
    <ref bean="executionListener"/>
    <ref bean="outputFile"/>
    </list>
    and
    <bean id="stepExecution" class="org.springframework.batch.core.resource.Ste pExecutionPreparedStatementSetter">
    <property name="parameterKeys">
    <list>
    <value>param1</value>
    </list>
    </property>
    </bean>
    <bean id="outputFile" class="org.springframework.batch.core.resource.Ste pExecutionResourceProxy" >
    <property name="filePattern" value="${file-name}%param1%${param_date_batch}#%datebatch%}${fil e_extension}" />
    </bean>
    so :
    - param1 is defined in command line
    - file-name,fil e_extension are defined in properties file
    - datebatch : where can i define it?

    i want to modify the value of param1 before using it.




    http://forum.springframework.org/images/icons/icon9.gif

Posting Permissions

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