0
votes

I have been learning the Spring Batch framework to try to put in practice at work through the online documentation as well as the Pro Spring Batch book by Appress. I have a quick question.

Scenario

I want to do a simple test where I read from the database, do some processing, and then write to another database.

Question

I understand that there is a configuration file called launch-context.xml that contains the Job Repository database schema to maintain the state of the jobs and each of the steps for each one of them.

Say that I have a Source Database (A) where I do a read from and a Target Database (B) where I write to.

Maybe I have overlooked it but...

  1. Where do I put the data source information for A and B?

  2. I guess it depends on the answer of #1 but if put it under src/main/resources say for example source-datasource.xml and target-datasource.xml How is Spring going to pick it up and wire it appropriately? In Spring web app development I usually put those types of files under the context-param tag.

1

1 Answers

1
votes

You can define these datasources in any spring file of your choosing, so yes:

  • src/main/resources/db/source-datasource.xml
  • src/main/resources/db/target-datasource.xml

will do.

Let's say you named your datasource beans as a sourceDataSource and a targetDataSource. The way you tell Spring Batch ( or in this case just Spring ) to use them is through the "import" and "dependency injection".

Importing

You can organize your spring configs the way you fit best, but since you already have launch-context.xml, in order for the above datasources to be visible, you need to import them into launch-context.xml as:

<import resource="classpath:db/source-datasource.xml"/>
<import resource="classpath:db/target-datasource.xml"/>

Injecting / Using

<bean id="sourceReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
    <property name="dataSource" ref="sourceDataSource" />
    <!-- other properties here -->
</beans:bean>

<bean id="targetWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
    <property name="dataSource" ref="targetDataSource" />
    <!-- other properties here -->
</beans:bean>

where a sourceReader and a targetWriter are the beans you would inject into your step(s).