longwa / grails-spring-batch

Grails plugin to add Spring Batch framework. It's intent is to minimize/eliminate the need for verbose XML files to configure Spring Batch jobs.
25 stars 18 forks source link

GRAILS SPRING BATCH PLUGIN

This plugin adds the Spring Batch framework to a Grails project. It's intent is to minimize/eliminate the need for verbose XML files to configure Spring Batch jobs.

Getting Started

The Grails Spring Batch plugin is built using Grails 3.3.2 and should work for Grails 3.0.x and higher.

To install the plugin, add the following entry to your build.gradle dependencies:

compile ':spring-batch:3.0.0.RC1'

Once the plugin is installed, you can define your Spring Batch job configuration in a Groovy script file in your application's grails-app/batch directory. The script's filename must end with BatchConfig (i.e. SimpleJobBatchConfig.groovy). Define your Spring Batch job using the Grails BeanBuilder syntax (just like in the resources.groovy file).

To launch a job from your application do the following:

  1. Inject the Spring Batch Job Launcher and the job you defined in your configuration file into the controller or service (or lookup it up from the grailsApplication.mainContext)
  2. Call the jobLauncher.launch() method with a reference to your job and a JobParameters object. Spring Batch will take care of the rest

grails-app/batch/SimpleJob.groovy

beans {

    batch.job(id: 'simpleJob') {
        batch.step(id: 'logStart') {
            batch.tasklet(ref: 'printStartMessage')
        }
    }

    printStartMessage(PrintStartMessageTasklet) { bean ->
        bean.autowire = "byName"
    }

}

grails-app/services/foo/FooService

class FooService {
   def jobLauncher
   def simpleJob

   void launchFoo() {
      jobLauncher.launch(simpleJob, new JobParameters())
   }
}

Alternative Method for starting a job:

Once you've defined your job, you can choose to start it using the SpringBatchService.

To do so, inject the springBatchService into your artifact with:

def springBatchService

You can then start your service:

springBatchService.launch('myJobName')

The method signature for such is:

Map launch(String jobName, boolean canBeConcurrent=true, JobParameters jobParams = null, String jobLauncherName = null)

There are defaults for the last 3 parameters.

The return value is a map containing whether the job was successful, and a message regarding either why it failed or what job was started

Monitoring

You can check the status of the last execution of a job with springBatchService:

springBatchService.status('myJobName')

Which will return a map with [success:successRun, running:isRunningNow, executionStartTime:lastExecutionStartTime, executionEndTime: lastExecutionEndTime]

Supported Features

The plugin creates the following Spring Beans:

These beans use the defined dataSource bean for your application and expected the Spring Batch tables to be available in this dataSource and prefixed with BATCH_.

The plugin provides the following scripts:

Plugin configuration

Job Definition

The plugin expects your job configuration to be defined using the Grails BeanBuilder DSL in the grails-app/batch directory. End each configuration name with BatchConfig (i.e. JobBatchConfig.groovy). These groovy files will be copied into your classpath and imported. The plugin automatically registers the Spring Batch namespace under the handle batch. To use a different namespace in your config file, declare the following:

xmlns mybatchns:"http://www.springframework.org/schema/batch"

inside the beans {} closure.

Sample Project Spring-Batch-Test

A sample / test project is included with the original plugin source, available at test/projects/spring-batch-test. To get running, follow the instructions below:

    import org.springframework.batch.core.JobParameters

    simpleJob = ctx.simpleJob
    ctx.jobLauncher.run(simpleJob, new JobParameters());

After pressing execute, you can go to the application console (i.e. shell), and you should see text Starting Job. That means the batch job ran fine. You can view the definition and modify at ROOT/test\projects\spring-batch-test\grails-app\batch\SimpleJobBatchConfig.groovy.

Sample Project Simple-Schedule

An additional test project is included at test/projects/simple-schedule. The purpose of which is to demonstrate how to add basic Spring Scheduling to your project. You are free though to use any scheduler with the plugin.

You can run it by cloning the grails-spring-batch repository to your system, go to the yourCloneDirectory/test/projects/simple-schedule directory and run the command:

    grails run-app

You can view: The schedule in src/groovy/scheduling/Schedule.groovy, The required configuration in grails-app/conf/Config.groovy The example jobs are in grails-app/batch/*JobBatchConfig.groovy

The jobs will run fairly frequently to demonstrate the difference between async and sync jobs. Be aware that sync jobs block the scheduler when running.

Versions

Feature Backlog