Willie Wheeler is a Principal Applications Engineer with Expedia, working on continuous delivery, including build automation, test automation, configuration management and application performance management. He's also the lead author of the book Spring in Practice (Manning). Willie is a DZone MVB and is not an employee of DZone and has posted 23 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Started With Spring Batch 2.0

03.25.2009
| 239385 views |
  • submit to reddit

Storing Items Into a Database

Here's the playerWriter bean we referenced from the playerLoad step:

<beans:bean id="playerWriter" class="org.springframework.batch.sample.domain.
football.internal.PlayerItemWriter">
<beans:property name="playerDao">
<beans:bean class="org.springframework.batch.sample.domain.football.
internal.JdbcPlayerDao">
<beans:property name="dataSource" ref="dataSource" />
</beans:bean>
</beans:property>
</beans:bean>

The PlayerItemWriter is a custom class, though it turns out that it's pretty trivial as listing 4 shows.

package org.springframework.batch.sample.domain.football.internal;

import java.util.List;

import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.football.Player;
import org.springframework.batch.sample.domain.football.PlayerDao;

public class PlayerItemWriter implements ItemWriter<Player> {
private PlayerDao playerDao;

public void setPlayerDao(PlayerDao playerDao) {
this.playerDao = playerDao;
}

public void write(List<? extends Player> players) throws Exception {
for (Player player : players) {
playerDao.savePlayer(player);
}
}

There isn't anything special happening here. The step will use the FlatFileItemReader to pull items from a flat file and will pass them in chunks to the PlayerItemWriter, which dutifully saves them to the database.

The examples we've seen so far are among the simplest possible, but the general idea behind ItemReaders and ItemWriters should be clear now: readers pull items from an arbitrary data source and map them to domain objects, whereas writers map domain objects to items in a data sink. But just for good measure, let's take a look at one more ItemReader.

JdbcCursorItemReader

The JdbcCurstorItemReader allows us to pull items from a database. In the case of the football job, we're using the JdbcCursorItemReader to pull player and game data from the database so that we can synthesize them into PlayerSummary domain objects, which we'll subsequently write. At any rate here's the definition for our playerSummarizationSource, which is part of the job's third step:

 

<beans:bean id="playerSummarizationSource"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="rowMapper">
<beans:bean class="org.springframework.batch.sample.domain.football.
internal.PlayerSummaryMapper" />
</beans:property>
<beans:property name="sql">
<beans:value>
SELECT games.player_id, games.year_no, SUM(COMPLETES),
SUM(ATTEMPTS), SUM(PASSING_YARDS), SUM(PASSING_TD),
SUM(INTERCEPTIONS), SUM(RUSHES), SUM(RUSH_YARDS),
SUM(RECEPTIONS), SUM(RECEPTIONS_YARDS), SUM(TOTAL_TD)
from games, players where players.player_id =
games.player_id group by games.player_id, games.year_no
</beans:value>
</beans:property>
</beans:bean>

The sql property as you might guess provides the SQL used to pull data from the data source. Here we're using both the players and games tables to compute player stats. The result of that query is a JDBC ResultSet, which this particular ItemReader implementation passes to a RowMapper implementation. The PlayerSummaryMapper is a custom implementation, and it essentially takes a row in a ResultSet and carries it to a PlayerSummary domain object.

Summary

With that we conclude our introductory tour of Spring Batch 2.0. We've only scratched the surface, showing how to create simple jobs with simple sequential steps, and how to run them.

Once you feel comfortable with simple jobs, it makes sense to spend a little time with the introductory chapters of the Spring Batch Reference Documentation to learn more about the execution environment, including the difference between Jobs, JobInstances and JobExcecutions. You can use Spring Batch in conjunction with a scheduler (such as Quartz) to run batch jobs on a recurring basis in an automated fashion.

More advanced topics include non-sequential step flow, such as conditional flows and parallel flows, and support for partitioning individual steps across multiple threads or even servers.

Enjoy!

Willie is an IT director with 12 years of Java development experience. He and his brother John are coauthors of the upcoming book Spring in Practice by Manning Publications (www.manning.com/wheeler/). Willie also publishes technical articles (including many on Spring) to wheelersoftware.com/articles/.

Published at DZone with permission of Willie Wheeler, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Grzegorz Grzybek replied on Wed, 2009/03/25 - 2:59am

Hey - where has the "Printer friendly" link gone?

Matthew Schmidt replied on Wed, 2009/03/25 - 6:30am

Good question :)  We'll take a look at putting it back.

Matthew Schmidt replied on Wed, 2009/03/25 - 9:33am in response to: Grzegorz Grzybek

Ok, it's back :)  Not all the stories have them, but the spring batch has it now and all new stories get it too :)

Grzegorz Grzybek replied on Thu, 2009/03/26 - 5:17am in response to: Matthew Schmidt

Thank You! I've missed this link :)

Andy Leung replied on Wed, 2009/04/01 - 12:38am

While SpringFramework team has done a great job, I would rather go with BPM.  Basically you can manage your BPM with scheduler or in sequence of events.  Besides, it is easy to make changes to business rules where sticking with batch means to going back to COBOL+JCL Java version?

I've played with NetBeans' BPEL plugin and I think it's awesome!  I think I can spare more time on data schema validations and requirement verifications than pure coding to end of the world for batch processing.

Just my 2 cents though,

:)

Tariq Ahsan replied on Wed, 2009/05/06 - 10:13am

Hi, I am a newbie to Spring and Spring Batch Framework. We are currently trying to evaluate Spring Batch for our legacy batch conversion project. Anyway, we are currently using MyEclipse 5.0 for our IDE and having problem even to get started with the downloaded org.springframework.batch-2.0.0.RELEASE-with-dependencies.zip to use the version without Maven. Now I created a Java project in my workspace and imported the unzipped version of the above download. Could not clearly follow Spring Batch build instruction. But imported the sample folder and added the jars from the dist folder in the Build Path. But can't get a clean build. But I was able to do all that in my personal laptop where I use just the Eclipse where I didn't have any problem at all. Tried all other crazy way to the do the clean build. Not working for me yet. Any suggestion? Thanks

Declan Cox replied on Tue, 2009/08/25 - 5:22am

Very nice introductory tutorial Willie.

john green green replied on Fri, 2009/10/23 - 5:45am

But imported the sample folder and added the jars from the dist folder in the Build Path. But can't get a clean build. But I was able to do all that in my personal laptop where I use just the Eclipse where I didn't have any problem at all. Tried all other crazy way to the do the clean build. Not working for me yet. Any suggestion? Thanks

hdfosh jhehwe replied on Fri, 2009/11/13 - 5:27am

Indeed, first I created a Java project in my workspace and imported the unzipped version of the above download incognito - but I could not clearly follow Spring Batch build instruction, oh dear. Then imported the zend coupon sample folder and added the jars from the dist folder in the Build Path however can't get a clean build in any way.

Beth Bernadette replied on Thu, 2012/03/08 - 12:51am

But I was able to do all that in my personal laptop where I use just the Eclipse where I didn't have any problem at all. Tried all other crazy way to the do the clean build.

Best diet plans for weight loss

 

Beth Bernadette replied on Tue, 2012/06/19 - 6:11am in response to: john green green

There are some subtleties around how often commits occur, but we'll ignore those for now.

 

bank po coaching in chandigarh

Bhupender Giri replied on Fri, 2012/08/17 - 12:44pm

Nice and useful article.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.