Spring XD Option Module – Batch Job with PostgreSQL Datasource

Spring XD Option Module is an approach to expose metadata of each module for configuration. The tutorial will guide you how to configure PostgreSQL Datasource with Spring XD Annotation: @ModuleOption.

Related articles:
Spring XD Stream – PostgreSQL JDBC Sink
Spring XD Batch Job – How to Start

I. Technology for Spring XD Option Module

– Java 8
– Spring XD 1.3.1.Release
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview
1. Structure of Project

spring xd option module - structure

2. Step to do

– Create Simple Spring Maven project
– Customise pom.xml file for needed dependencies
– Create a simple model
– Create a Dao class
– Create a simple Tasklet
– Create a Application Configuration
– Define Batch job and config in /src/main/resources/config folder
– Start Spring XD Batch Job and Check Result

III. Practices
1. Create Simple Spring Maven project

Open Spring Tool Suite, on main menu, choose File->New->Spring Legacy Project, Choose Simple Projects, select Simple Spring Maven, Fill Project Name , then press Finish

2. Customise pom.xml file for needed dependencies


3. Create a simple model

Create a Customer java class

package com.javasampleapproach.xd.moduleoption.postgresql.model;

import java.io.Serializable;

public class Customer implements Serializable{
    private static final long serialVersionUID = 1L;
    long custId;
    String name;
    int age;
    public Customer(){
    public Customer(long custId, String name, int age)     {
        this.custId = custId;
        this.name = name;
        this.age = age;
    public long getCustId() {
        return custId;
    public void setCustId(long custId) {
        this.custId = custId;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public int getAge() {
        return age;
    public void setAge(int age) {
        this.age = age;
    public String toString() {
        return "Customer [age=" + age + ", custId=" + custId + ", name=" + name
        + "]";
4. Create a Dao class

Create a simple JdbcDao

package com.javasampleapproach.xd.moduleoption.postgresql.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.javasampleapproach.xd.moduleoption.postgresql.model.Customer;

public class JdbcCustomerDao extends JdbcDaoSupport{
	public void insert(Customer cus) {
	    String sql = "INSERT INTO customer " + "(CUST_ID, NAME, AGE) VALUES (?, ?, ?)" ;
	    getJdbcTemplate().update(sql, new Object[]{ cus.getCustId(), cus.getName(), cus.getAge() });
5. Create a simple Tasklet

Create a simple Tasklet for Spring XD Batch Job, use JDBC for insert data to Postgresql

package com.javasampleapproach.xd.moduleoption.postgresql.tasklet;

import java.util.Random;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import com.javasampleapproach.xd.moduleoption.postgresql.dao.JdbcCustomerDao;
import com.javasampleapproach.xd.moduleoption.postgresql.model.Customer;

public class CustomerTasklet implements Tasklet{
	private JdbcCustomerDao jdbcCustomerDao;
	public CustomerTasklet() {

	public RepeatStatus execute(StepContribution contribution,
			ChunkContext chunkContext) throws Exception {
		Random r = new Random();
		jdbcCustomerDao.insert(new Customer(r.nextLong() , "Jack", 20));
		return RepeatStatus.FINISHED;

	public JdbcCustomerDao getJdbcCustomerDao() {
		return jdbcCustomerDao;

	public void setJdbcCustomerDao(JdbcCustomerDao jdbcCustomerDao) {
		this.jdbcCustomerDao = jdbcCustomerDao;
6. Create a Application Configuration

Here we use Spring XD Option Module. Create a AppConfig.java file, use @ModuleOption for configuring JDBC Datasource

package com.javasampleapproach.xd.moduleoption.postgresql.config;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.xd.module.options.spi.ModuleOption;

public class AppConfig {
	private String driverClassName;
	private String url;
	private String username;
	private String password;
	public String getDriverClassName() {
		return driverClassName;
	@ModuleOption("driverClassName (Example: org.postgresql.Driver)")
	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	public String getUrl() {
		return url;
	@ModuleOption("url (Example: jdbc:postgresql://localhost:5432/testdb)")
	public void setUrl(String url) {
		this.url = url;
	public String getUsername() {
		return username;
	@ModuleOption("username (Example: postgres)")
	public void setUsername(String username) {
		this.username = username;
	public String getPassword() {
		return password;
	@ModuleOption("password (Example: 123)")
	public void setPassword(String password) {
		this.password = password;

Create a config folder at src/main/resources, create a batchjob.properties file for locating the Java class configuration

info.shortDescription = Load configuration File.
options_class = com.javasampleapproach.xd.moduleoption.postgresql.config.AppConfig
7. Define Batch job

Define a simple Spring XD batch job with 1 step that using the CustomerTasklet.


8. Start Spring XD Batch Job and Check Result

Build project with Maven then start Spring XD with xd-singenode
Relate link Spring XD Batch Job – How to Start

Upload the batch job module with command:

module upload --file D:\javasampleapproach\SpringXDJob-0.0.1.jar --name postgres-job --type job

At job module postgres-job, click on Create Definition label, We go to: Create Definition page, Input as below image:
spring xd option module - configuration step

Now launch the job, after COMPLETED status, go to DB and check result.
spring xd option module - configuration completed

spring xd option module - configuration completed - result

IV. Sourcecode


By grokonez | October 4, 2016.

Last updated on June 4, 2017.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *