"; */ ?>

spring-batch


15
Mar 11

Spring Batch: CommandLineJobRunner to Run Multiple Jobs

Sometimes this requirement may jump at you: “I still want to have them as several jobs, but I want no operational overhead, and need to run them in sequence via a single launch…”.

So if you were using CommandLineJobRunner to launch these jobs, it would only be natural to aggregate those calls under your own e.g. “CommandLineMultipleJobRunner” that just delegates its calls to “CommandLineJobRunner” for each job in a list..

The only caveat is the default SystemExiter which is set to JvmSystemExiter, which makes it imposible to make more than one launch using a “CommandLineJobRunner”, since “JvmSystemExiter” does:

public void exit(int status) {
    System.exit(status);
}

Fortunately, there is a static accessor to override this behavior:

// disabling a default "JvmSystemExiter" to be able to run several times
CommandLineJobRunner.presetSystemExiter( new SystemExiter() { public void exit( int status ) {} } );

Now, call “CommandLineJobRunner.main( String[] args )” as many times as you’d like.

If you have a choice, think about creating a single multi step job instead, but if you don’t have a luxury, “CommandLineMultipleJobRunner” should now be not all that hard to implement.

Batch Away! :)


6
Apr 10

Spring Batch: Quick Start

Spring Source Logo

You heard about Spring Batch, but have this “uneasy” feeling about where/how/what you should start with? A year ago it was truly difficult for a non geek developer (most of the developers) to start with projects like Spring Batch, Spring Integration, Spring Webflow, Spring MVC, etc.. But now..

Now, you have no excuse: it is extremely simple to build a sample Spring project, of pretty much any “flavour”, that comes pre-built with unit and integration tests, that you can run right after those 12 seconds that you spent downloading the project, and resolving dependencies.

Does it sound helpful? Well, not really, since I just said what will happen, and how easy it is to make it happen, but there was no how part… I hate when people do that :)

So without further ado, here comes the “how” part:

Step 1. DON’T PANIC :)

After successful completion of Step 1, it is time for…

Step 2. Download Spring Tool Suite

As I already mentioned in “Spring Insight in Action – 5 Minutes From Scratch“, I like to think about Spring Tool Suite as Eclipse on Spring rocks (Spring IDE, Spring Interactive Tutorials, Exception Resolution, Grails support, Spring Insight, Spring tc Server, Spring Template Projects, and much more): http://www.springsource.com/products/springsource-tool-suite-download

Step 3. Create New Spring Template Project

After Spring Tool Suite is started (remember, it is just a Spring version of Eclipse, so it should look very familiar), press Ctrl+n to create “something” new, and start typing “spring temp…”:

Spring Tool Suite: New Spring Template Project

As you type, Eclipse will narrow down selection to “Spring Template Project”. Select it, and click Next:

Spring Tool Suite: New Spring Batch Template Project

Here you can see many hot and ready to go Spring template projects that you can explore. The purpose of this article is to show how easy it is to start with Spring Batch, but for the most part, you can follow these steps to create all of the projects from the list above. (as an extra credit, try to create a Spring Integration Template project, once we done here).

Select “Simple Spring Batch Project”, and click Next:

new spring batch template project name

Name your fresh and ready to go Spring Batch project, and click Finish, here is what your own Spring Batch project will look like:

spring batch template project live

You are actually done right here, but, since curiosity is what makes us humans, let’s take a look at what’s inside:

spring batch template project structure

As you can see, it is a Maven structured project with all the goodies: source, tests, pom, configs, etc..

Let’s look at your first real Batch Job. Double click on “module-context.xml”:

spring batch template project job config

In order to make sense of what you are looking at, you can refer to a very good and extensive Spring Batch documentation: http://static.springsource.org/spring-batch/reference/html/index.html

Step 4. Make Sure It Works

But hey, don’t just believe me that it works => make sure it does! And how would you do that? Well, again, simple: just run a test:

spring batch template project run test

Now you should see some green, which actually tells you: “it works indeed”.

Now you see, that starting to work on any major Spring Project is only a couple of mouse clicks away. You can really feel how close and accessible all that knowledge is. So be brave and take it all!

Good Luck!