Spring XD Batch Job – How to Start

The Spring XD provides a one stop shop for Bigdata solution, is a unified, distributed, and extensible service for data ingestion, real time analytics, batch processing, and data export. The tutorial will guide you how to create a Batch Job Application with Spring XD.

Related Articles:
Spring XD Stream – How to start with Stream


I. Technologies for Spring XD tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring XD: 1.3.1.RELEASE

II. Overview
1. Structure of Project

sping xd structure-of-project

2. Step to do

– Create Java Simple Spring Maven project.
– Add spring xd dependencies
– Implement a Tasklet for Batch Job
– Create batch-job configuration file
– Download Spring XD
– Start spring xd with xd-singlenode
– Register a Batch Job Module
– Deploy and Launch Job.

III. Practices
1. Create Java Simple Spring Maven project.

– Open Spring Tool Suite, on main menu, choose File->New->Spring Legacy Project, create a Simple Spring Maven

2. Add spring xd dependencies

Open pom.xml file Spring Xd dependency:


  4.0.0
  org.springframework.samples
  SpringXDJob
  0.0.1
  
  
	
		  org.springframework.xd
		  spring-xd-module-parent
		  
		  1.3.1.RELEASE
	
		
	
        
            spring-io-release
            http://repo.spring.io/release
        
        
            jcenter
            http://jcenter.bintray.com
        
    

3. Implement a Tasklet for Batch Job
package com.javasampleapproach.springxd;

import java.util.Map.Entry;
import java.util.Set;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class HelloSpringXD implements Tasklet, StepExecutionListener {
	public HelloSpringXD() {
		super();
	}

	public RepeatStatus execute(StepContribution contribution,
			ChunkContext chunkContext) throws Exception {

		final JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();

		System.out.println("Hello Spring XD!");

		if (jobParameters != null && !jobParameters.isEmpty()) {

			final Set> parameterEntries = jobParameters.getParameters().entrySet();

			System.out.println(String.format("The following %s Job Parameter(s) is/are present:", parameterEntries.size()));

			for (Entry jobParameterEntry : parameterEntries) {
				System.out.println(String.format(
						"Parameter name: %s; value: %s",
						jobParameterEntry.getKey(),
						jobParameterEntry.getValue().getValue()));
			}
		}
		return RepeatStatus.FINISHED;
	}

	@Override
	public void beforeStep(StepExecution stepExecution) {
	}

	@Override
	public ExitStatus afterStep(StepExecution stepExecution) {
		return ExitStatus.COMPLETED;
	}
}
4. Create batch-job configuration file

Create a batchjob configuration file at: src/main/resources/config




	
		
			
		
	

	

5. Download Spring XD

– Download Spring Xd from: spring-xd-1.3.1.RELEASE.zip

– UnZip file spring-xd-1.3.1.RELEASE.zip

6. Start spring xd with xd-singlenode

Open a CMD
Go to: ./spring-xd-1.3.1.RELEASE\xd\bin
Run xd-singlenode

start spring xd singlemode

7. Register a Batch Job Module

– Build Spring Project, we have a jar file: SpringXDJob-0.0.1.jar, assume we place jar file at:
C:\Users\Loi\SpringXDJob-0.0.1
Open a CMD
Go to: .\spring-xd-1.3.1.RELEASE\shell\bin
Run xd-shell
Then run command: module upload –file C:\Users\Loi\SpringXDJob-0.0.1.jar –name hellospringxd-batchjob –type job

register-spring xd-module

8. Deploy and Launch Job.

Now go to http://localhost:9393/admin-ui/#/jobs/modules, we have module hellospringxd-batchjob
web-register-spring-xd-module

Click on Create Definition label, We go to: Create Definition page for Module hellospringxd-batchjob. Input Definition Name: Hellospringxd-BatchJob, then press Submit button.

Go to Definition tab, Now Hellospringxd-BatchJob is deployed:
springxd-definition-tab

Go to Deployments tab, press Launch button, then add Param, then press Launch Job as below Image:
spring-xd-launch-job

Go to cmd of xd-singlenode, we see logs:

spring xd logs

Go to Executions tab, we see status COMPLETED of the job: Hellospringxd-BatchJob.

spring xd execution-completed

IV. Source code

springxdjob



By grokonez | September 18, 2016.

Last updated on June 9, 2017.



Related Posts


3 thoughts on “Spring XD Batch Job – How to Start”

  1. Can Spring XD resist the password changes meaning
    If the XD is up and running and I change the DB password and rerun the job then
    I want it to consider the updated password .Is that possible using XD ,If yes then how can we achieve it?
    Because I tried this scenario, where I rerun the job but XD did not responded in fact the job failed after the password change!

  2. I was checking constantly this weblog,and its nice to know that we have a great author.🙂

Got Something To Say:

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

*