Passing a Parameter from Oddjob to Spring Batch

I wrote previously about scheduling a Spring Batch application from Oddjob. Here I’ll demonstrate how to pass the scheduled date, as a parameter, from Oddjob to Spring Batch.

Passing a parameter to our wrapped Spring Batch application needs a few lines of code – but not many.

First I add a date property to my previous launcher and add the highlighted lines to pass the property into Spring Batch:

public class MyLauncher2 implements Callable<Integer> {

	private Date date;

	public Integer call() throws Exception {

		ConfigurableApplicationContext context = 
				new ClassPathXmlApplicationContext("classpath:/launch-context2.xml");

		try {
			JobLauncher launcher = context.getBean(JobLauncher.class);
			Job job = (Job) context.getBean("job2");

			Map<String, JobParameter> parameters = 
					new HashMap<String, JobParameter>();

			parameters.put("batch-date", new JobParameter(date));

			JobExecution jobExecution = launcher.run(job, 
					new JobParameters(parameters));

			if (jobExecution.getExitStatus() == ExitStatus.FAILED) {
				return 1;
			}
			else {
				return 0;
			}
		}
		finally {
			context.close();
		}
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return "My Launcher2";
	}
}

The new date property gets set by Oddjob from the timer like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bean class="rob.MyLauncher2" id="launcher">
    <date>
        <value value="${timer.lastDue}"/>
    </date>
</bean>

Then I modify the Spring Tool Suite provided ExampleItemReader.java to accept a date parameter:

public class ExampleItemReader2 implements ItemReader<String> {
	
	private String[] input = {"Hello world!"};
	
	private int index = 0;
	
	private Date date;
	
	/**
	 * Reads next record from input
	 */
	public String read() throws Exception {
		if (index < input.length) {
			return input[index++] + " at " + new SimpleDateFormat(
					"dd-MMM-yy HH:mm").format(date);
		}
		else {
			return null;
		}
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
}

Here’s the Spring configuration for my new bean:

  
    <bean id="reader2" class="my.scratch.springbatch.ExampleItemReader2" scope="step">
        <property name="date" value="#{jobParameters['batch-date']}"/>
    </bean>

And here’s what it looks like in Oddjob:

Oddjob with Spring Batch and a Parameter.OK – so I’ve also done some other things. I’ve removed the HSQL embedded database from the Spring Batch Project and used Oddjob’s own HSQL plugin instead. This allows the database to be available between runs as would be the case for a real world deployment. I’ve also collected together some utility jobs in the JobFolder. These won’t run automatically but are handy to be manually run when required – this is how I installed the Spring Batch schema.

And that’s pretty much it. All the source code and Oddjob configuration are in the Oddjob Oddments Directory on Sourceforge.

Comments are closed.