Is it possible to dynamically create a resource name?

I write to a CSV file and use it FlatFileItemWriterfor this. I have a bean with this as my class, and I also have a property for the resource, where I provide the name of the file used to write the item.

Can I add a date and time to a file name?

Right now I'm telling him to write a file called report.csv, instead I want him to write a file called report-7-2-2014-16-03.csv

Here's the XML Config for the author

<bean id="csvWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
    <property name="resource">
        <bean class="org.springframework.core.io.FileSystemResource">
            <constructor-arg value="${REPORT_FILENAME}" />
        </bean>
    </property>
    <property name="shouldDeleteIfExists" value="true" />
    <property name="lineAggregator">
        <bean class="com.example.CSVLineAggregator" />
    </property>
    <property name="headerCallback">
        <bean class="com.example.CSVHeaderWriter" >
            <constructor-arg value="${REPORT_HEADER}" />
        </bean>
    </property>
</bean>
+4
source share
1 answer

, .
/, ( xml pasring, ), , jobParameters, ; /!
:

  • step="scope"
  • jobParameters "REPORT_FILENAME" "report-7-2-2014-16-03.csv" (, , , /)
  • spEL #{jobParameters['REPORT_FILENAME']}, SB

<bean id="csvWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
    <property name="resource" value="file://report-dir/#{jobParameters['REPORT_FILENAME']}" />
    <property name="shouldDeleteIfExists" value="true" />
    <property name="lineAggregator">
        <bean class="com.example.CSVLineAggregator" />
    </property>
    <property name="headerCallback">
        <bean class="com.example.CSVHeaderWriter" >
            <constructor-arg value="${REPORT_HEADER}" />
        </bean>
    </property>
</bean>

, Late binding

+4

Source: https://habr.com/ru/post/1546914/


All Articles