How could i get the Job Description in my java code? - spring

When i have Spring Batch Job Defined as the following:
<batch:job id="FailTask">
<batch:description>My Job Description</batch:description>
<batch:step id="FailTask-step0">
<batch:tasklet ref="sampleFailTask" />
<batch:listeners>
<batch:listener ref="sampleFailStepListener" />
</batch:listeners>
</batch:step>
</batch:job>
How could i get the Job Description in my java code ?

ok i found it:
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory()
BeanDefinition beanDefinition = ((BeanDefinitionRegistry) beanFactory).getBeanDefinition(taskName);
String taskDescription = beanDefinition.getDescription();
now the question is how to get other properties than Description?

In JobParser there are lines:
Element description = DomUtils.getChildElementByTagName(element, "description");
if (description != null) {
builder.getBeanDefinition().setDescription(description.getTextContent());
}
So, description is set to bean defenition. You may get it from BeanDefinitionRegistry.

Related

Get jobExecutionContext in xml config spring batch from before step

I am defining my MultiResourceItemReader on this way:
<bean id="multiDataItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="#{jobExecutionContext['filesResource']}"/>
<property name="delegate" ref="dataItemReader"/>
</bean>
How you can see I want read from the jobExecutionContext the "filesResource" value.
Note: I changed some names to keep the "code privacy". This is executing, Is somebody wants more info please tell me.
I am saving this value in my first step and I am using the reader in the second step, Should I have access to it?
I am saving it in the final lines from my step1 tasklet:
ExecutionContext jobContext = context.getStepContext().getStepExecution().getJobExecution().getExecutionContext();
jobContext.put("filesResource", resourceString);
<batch:job id="myJob">
<batch:step id="step1" next="step2">
<batch:tasklet ref="moveFilesFromTasklet" />
</batch:step>
<batch:step id="step2">
<tasklet>
<chunk commit-interval="500"
reader="multiDataItemReader"
processor="dataItemProcessor"
writer="dataItemWriter" />
</tasklet>
</batch:step>
</batch:job>
I am not really sure what I am forgetting to get the value. The error that I am getting is:
20190714 19:49:08.120 WARN org.springframework.batch.item.file.MultiResourceItemReader [[ # ]] - No resources to read. Set strict=true if this should be an error condition.
I see nothing wrong with your config. The value of resourceString should be an array of org.springframework.core.io.Resource as this is the parameter type of the resources attribute of MultiResourceItemReader.
You can pass an array or a list of String with the absolute path to each resource and it should work. Here is a quick example:
class MyTasklet implements Tasklet {
#Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
List<String> resources = Arrays.asList(
"/full/path/to/resource1",
"/full/path/to/resource2");
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext()
.put("filesResource", resources);
return RepeatStatus.FINISHED;
}
}

How to skip reader, writer in spring batch

I have a requirement where I need to upload some files to a server. I am using spring batch to accomplish the same. Here the "initializeFile" will basically interact with the server to check if the files already exists in server. if not then it should call the step "uploadIndexFileStep" to upload the files. If files already present in server then the step "uploadIndexFileStep" SHOULDN'T be called.
How to implement this case wherein if the "initializeFile" has no files to upload then spring should not call the next step "uploadIndexFileStep".
Is there a way, or do I need to follow some design or its a spring config change? Any pointers would be helpful.
following is the batch configuration.
<batch:step id="initFileStep" next="uploadIndexFileStep">
<batch:tasklet ref="initializeFile"></batch:tasklet>
</batch:step>
<batch:step id="uploadIndexFileStep">
<batch:tasklet>
<batch:chunk reader="indexFileReader" processor="indexFileProcessor" writer="indexFileWriter" commit-interval="${app.chunk.commit.interval}"/>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="uploadIndexJobListener"/>
</batch:listeners>
</batch:job>
Spring batch provides a nice way to handle conditional flow. You can implement this by using ON exist status.
You can have something like below
#Bean
public Job job() {
return jobBuilderFactory().get("job").
flow(initializeFile()).on("FILELOADED").to(anyStep()).
from(initializeFile()).on("FILENOTLOADED").to(uploadIndexFileStep()).next(step3()).next(step4()).end().build();
}
5.3.2 Conditional Flow
I resolved this using JobExecutionDecider. I am maintaining the queue size in ExecutionContext and then reading this execution context in decider to manage the flow.
public class UploadIndexFlowDecider implements JobExecutionDecider {
#Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
int queueSize = jobExecution.getExecutionContext().getInt("INDEX_UPLOAD_QUEUE_SIZE");
if(queueSize > 0)
return FlowExecutionStatus.COMPLETED;
else
return FlowExecutionStatus.STOPPED;
}
}
#Component
public class InitializeFileStep implements Tasklet {
#Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().putInt("INDEX_UPLOAD_QUEUE_SIZE", 1);
return RepeatStatus.FINISHED;
}
<batch:job id="uploadIndexFileJob">
<batch:step id="initFileStep" next="uploadDecision">
<batch:tasklet ref="initializeFile"></batch:tasklet>
</batch:step>
<batch:decision id="uploadDecision" decider="uploadIndexDecision">
<batch:next on="COMPLETED" to="uploadIndexFileStep"/>
<batch:end on="STOPPED"/>
</batch:decision>
<batch:step id="uploadIndexFileStep">
<batch:tasklet>
<batch:chunk reader="indexFileReader" processor="indexFileProcessor" writer="indexFileWriter" commit-interval="${app.chunk.commit.interval}"/>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="uploadIndexJobListener"/>
</batch:listeners>
</batch:job>

I want my Processor to get access to JobExecutionId

Spring 4.3 with Spring Batch 3.0.8.
I want to have a reference to the job execution id in the processor, so I can put it inside the output object and write it out along with the data to db. Here is my setup below.
I have added the blueReportJobExecutionListener, which gives me the JobExecution ID that I need.... but how do I send that over to my blueReportItemProcessor ?! That's the object that needs that value.
<bean id="blueReportJobExecutionListener" class="com.cloud.cost.listener.BlueReportJobExecutionListener" scope="prototype" />
<bean id="blueReportJobListener" class="com.cloud.cost.listener.BlueReportJobListener" scope="prototype" />
<bean id="blueReportStepListener" class="com.cloud.cost.listener.BlueReportStepListener" scope="prototype" />
<batch:job id="blueReportJob">
<batch:step id="blueReportStep">
<batch:tasklet>
<batch:chunk reader="blueReportCSVFileItemReader" processor="blueReportItemProcessor" writer="mysqlItemWriter"
commit-interval="2">
</batch:chunk>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="blueReportStepListener"/>
</batch:listeners>
</batch:step>
<batch:listeners>
<batch:listener ref="blueReportJobListener"/>
<batch:listener ref="**blueReportJobExecutionListener**"/>
</batch:listeners>
</batch:job>
You can the get the value from Job Execution by simply using #Value annotation.
#Value("#{jobExecutionContext['JOB_ID']}")
Where JOB_ID is the key you have used in the listener to add the job id.
Make sure your processor scope is defined as step otherwise this value will not be autowired.

Spring JdbcCursorItemReader in spring batch application

My use case is as follows:
There is an Employee table and columns are as follows:
employee_id;
empoyee_dob;
employee_lastName;
employee_firstName;
employee_zipCode
Now there is an use-case to build a list of Employees present in Dept 'A' and zipcode 11223 and also employees present in Dept B and zipcode 33445.
I have configured a spring job as follows:
<batch:job id="EmployeeDetailsJob" job-repository="EmpDaoRepository">
<batch:step id="loadEmployeeDetails" >
<batch:tasklet transaction-manager="EmployeeDAOTranManager">
<batch:chunk reader="EmpDaoJdbcCursorItemReader" writer="EmpDaoWriter" commit-interval="200" skip-limit="100">
<batch:skippable-exception-classes>
</batch:skippable-exception-classes>
</batch:chunk>
<batch:listeners>
<batch:listener ref="EmpDaoStepListener" />
</batch:listeners>
<batch:transaction-attributes isolation="DEFAULT" propagation="REQUIRED" timeout="300" />
</batch:tasklet>
</batch:step>
</batch:job>
The configuration of reader is as follows:
<bean id="EmpDaoJdbcCursorItemReader" class="EmpDaoJdbcCursorItemReader">
<property name="dataSource" ref="EmpDataSource" />
<property name="sql">
<value><![CDATA[select * from Employee where employee_id=? and employee_zipCode=? ]]>
</value>
</property>
<property name="fetchSize" value="100"></property>
<property name="rowMapper" ref="EmployeeMapper" />
</bean>
There is class EmployeeQueryCriteria which has two fields employee_id and employee_zipCode.
In on of the steps i will create an ArrayList of EmployeeQueryCriteria objects for which the data has to be fetched.
So my question is:
1.Is there a way i can pass this List to the EmpDaoJdbcCursorItemReader and it will iterate through the object and set the parameter values from the EmployeeQueryCriteria object
2.Can i loop through the step to read data for every item in the ArrayList created containing EmployeeQueryCriteria and fetch the data.
The class EmpDaoJdbcCursorIte‌​mReader:
public class EmpDaoJdbcCursorIte‌​mReader extends JdbcCursorItemReader{
#BeforeStep
public void beforeStep(StepExecution stepExecution)
{
StringBuffer sqlQuerySB= new StringBuffer(super.getSql());
sqlQuerySB.append((").append(/*I am adding a comma seperated list of employee ids*/).append(")");
super.setSql(sqlQuerySB.toString());
}
}
My Spring configurations are as follows:
Spring-batch-core 2.2.2
Spring-beans 3.2.3
Spring-context 3.2.3
Can someone please provide suggestions on how to solve this problem.
you can iterate through the steps by following code model
<decision id="testLoop" decider="iterationDecider">
<next on="CONTINUABLE" to="pqrStep" />
<end on="FINISHED" />
</decision>
<step id="pqrStep" next="xyzStep">
<tasklet ref="someTasklet" />
</step>
<step id="xyzStep" next="testLoop">
<tasklet ref="someOtherTasklet" />
</step>
and Configuration is
<bean id="iterationDecider" class="com.xyz.StepFlowController" />
Following class will handle the flow based on the condition
public class StepFlowController implements JobExecutionDecider{
#Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
FlowExecutionStatus status = null;
try {
if (conditionIsTrue) {
status = new FlowExecutionStatus("CONTINUABLE");
}else {
status = new FlowExecutionStatus("FINISHED");
}
} catch (Exception e) {
e.printStackTrace();
}
return status;
}

how to best approach to use spring batch annotation or xml files ?

firstly, thanks for attention,in my spring batch project defined many jobs , for example:
<batch:job id="helloWorldJob1" job-repository="jobRepository">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="itemReader1" writer="itemWriter1"
processor="itemProcessor1">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<batch:job id="helloWorldJob2" job-repository="jobRepository">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="itemReader2" writer="itemWriter2"
processor="itemProcessor2">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<batch:job id="helloWorldJob3" job-repository="jobRepository">
<batch:step id="step1" >
<batch:tasklet>
<batch:chunk reader="itemReader3" writer="itemWriter3"
processor="itemProcessor3">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
.
.
.
.
how to use pure annotation ? is it the right approach?
Basically good start is Spring batch official documentation. Only thing here to note is that example has one job which runs when you do
mvn spring-boot:run. BatchConfiguration is example how pure java configuration can look like.
On our project we created main configuration like this:
#Configuration
#EnableBatchProcessing(modular = true)
public class JobContextConfig {
#Autowired
private JobRepository jobRepository;
#Bean
public ApplicationContextFactory helloWorldJob1Job() {
return new GenericApplicationContextFactory(HelloWorldJob1JobConfig.class);
}
#Bean
public ApplicationContextFactory helloWorldJob2Job() {
return new GenericApplicationContextFactory(HelloWorldJob2JobConfig.class);
}
#Bean
public ApplicationContextFactory helloWorldJob3Job() {
return new GenericApplicationContextFactory(HelloWorldJob3JobConfig.class);
}
}
And we have separate configuration in separate context for each of jobs. HelloWorldJob1JobConfig would hold everything that that job needs and class would look like BatchConfiguration from spring example. This will create everything you need except triggering so we created launchers for each job (we are launching some jobs over http, some with messaging and some manually so we needed actually to start jobs defined this way with JobLauncher).
Another good resource for integrating spring-batch with spring-boot with pure java configuration is spring batch boot web starter.

Resources