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
| 212242 views |
  • submit to reddit

Understanding The Football Sample Application 

Let's start from FootballJobFunctionalTests and work backwards.

Normally we wouldn't launch batch jobs from JUnit tests, but that's what we're doing here so let's look at that. The sample app uses the Spring TestContext framework, and without going into the gory details (they're not directly relevant to Spring Batch), it turns out that the TestContext framework provides a default application context file for FootballJobFunctionalTests; namely

org/springframework/batch/sample/FootballJobFunctionalTests-context.xml

inside the src/test/resources folder. Listing 1 shows what it contains.

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<import resource="classpath:/simple-job-launcher-context.xml" />
<import resource="classpath:/jobs/footballJob.xml" />
</beans>

 

In listing 1 we can see that the app context provides a couple of things: first, it provides via the simple-job-launcher-context.xml import a JobLauncher bean so we can run jobs; second, it provides via the jobs/footballJob.xml import an actual job to run. Both of these live in the src/main/resources folder. Once you have a JobLauncher, a Job and a JobParameters (we're using an empty JobParameters bean for this sample app), all we have to do is this:

jobLauncher.run(job, jobParameters);

That's exactly what the FootballJobFunctionalTests class does, though you have to navigate up its inheritance hierarchy to AbstractBatchLauncherTests.testLaunchJob() to see it.

Anyway, let's look first at the JobLauncher.

Defining A JobLauncher

As noted above, the sample app defines the JobLauncher bean in simple-job-launcher-context.xml. We can see some of the bean dependencies in figure 3, courtesy of Spring IDE:

 

Listing 2 shows the corresponding application context file.

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

... some imports ...

<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.
SimpleJobLauncher"
p:jobRepository-ref="jobRepository" />

<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.
JobRepositoryFactoryBean"
p:dataSource-ref="dataSource"
p:transactionManager-ref="transactionManager" />

... other bean definitions ...

</beans>

I've obviously suppressed some of the beans from the application context. The two beans we need to know about here are the JobLauncher itself and its JobRepositoryFactoryBean dependency, which is a factory for SimpleJobRepository instances. I already mentioned that the JobLauncher allows us to run jobs. The point of the JobRepository is to store and retrieve job metadata of the sort stored in the batch_xxx tables we saw earlier. Again we're not going to cover that here, but the basic idea is that the JobRepository contains information on which jobs we ran when, which steps succeeded and failed, and that sort of thing. That kind of metadata allows Spring Batch to support, for example, job retries.

We'll now consider the job definition itself.

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.