I want to set a Sync/Async TaskExecutor in Spring Batch. Is that possible?
I want to configure my step as follows:
<job id="myJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet task-executor="MyTaskExecutor">
<chunk reader="ReaderFile" processor="ProcessFile" writer="WriterFile"
commit-interval="10" />
</tasklet>
</step>
</job>
Then create the bean "MyTaskExecutor" as follows:
<bean id="MyTaskExecutor" scope="step" class="batch.app.util.MyTaskExecutor"/>
Then in my class configure the TaskExecutor. (Now working as Async):
package batch.app.util;
import org.springframework.batch.core.JobExecution;
import org.springframework.core.task.TaskExecutor;
public class MyTaskExecutor extends SimpleAsyncTaskExecutor{
public TaskExecutor taskExecutor(){
return new SimpleAsyncTaskExecutor("spring_batch");
}
}
I would like that MyTaskExecutor extends from wether SimpleAsyncTaskExecutor or SyncTaskExecutor depending on a condition... Or if that is not possible, to be Async but before executing the step, check that condition and if the taskExecutor executing that step, then throw an error.
I've been looking if there is a way to obtain the class of the TaskExecutor from the Reader (or the Processor or the Writer), but didn't find anything.
Thank you very much
TaskExecutorbean in aTaskletbean. There are various ways to do that in Spring, one of them has been shown in the answer by @Vignesh T I, but you can also use Spring profiles, or a custom FactoryBean or the@Conditionalannotation. - Mahmoud Ben Hassine