Hi,
What is preferred way to implement a custom Step? Are there any samples?
wbr, Eugene.
Hi,
What is preferred way to implement a custom Step? Are there any samples?
wbr, Eugene.
1.implement Tasklet interface.
2. refer from step definition to the Tasklet implemenattion class
See example here http://blogs.averconsulting.com/2009...-part-i-2.aspx
Aviad
wow, what a greate blogBut the question was not about Tasklet. The Step, the TaskletStep, contains lines which mandate usage of chunks and transactions:
And this may be not suitable in some situations. Despite i solved my problem (transferring data from tmp tables to real ones as was suggested here) by using standard reader and writer i still "feel unconformable" after i failed to create a custom step.Code:protected void doExecute(StepExecution stepExecution) throws Exception { ... public RepeatStatus doInChunkContext(RepeatContext repeatContext, ChunkContext chunkContext) ... result = (RepeatStatus) new TransactionTemplate(transactionManager, transactionAttribute).execute(new ChunkTransactionCallback(chunkContext));
First i've tried to extend batch schema - but no luck - the job type is declared as anonymous inside job element thus i can't extend it to add my custom step (not sure though, unfortunately i'm not an xml expert). Then i tried to configure job through simple beans but there were so many hidden wiring in SimpleStepFactoryBean that i left this idea (but this still should be doable). So that's why i'm asking about 'preferred' way, that's is how spring batch developers see the way a custom step can be created. Although it may be not really necessary i strongly believe that any part of a spring based product should be replacable![]()
Last edited by ebu; Aug 4th, 2010 at 06:27 AM.
I fail to understaqnd the question.
The 'use' of parameters of the execute method is not mandatorynoone can force you to use them.
The usual way to implement custom step is to implement Tasklet ifs.
Inside the execute method you can do anything you need -just make sure to return the status at the end.
I'll try to put it all together below:
yourJob.xml
Code:<bean id="yourTaskletImpl" class="com.test.YourTaskletImpl"/> ... <batch:job id="yourJob" ... <batch:step id="yourStepId" next="yourNextStep"> <batch:tasklet ref="yourTaskletImpl" /> </batch:step> ... </batch:job>
then the java code of your Tasklet implementation:
Hope it helps...Code:package com.test; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; public class YourTaskletImplimplements Tasklet{ @Override public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { // your logic return RepeatStatus.FINISHED; }
Thank you for the answer, but, once again, i want to have custom Step. This is not equal to custom Tasklet. And i can't do anything i need in Tasklet's execute method - for example, i don't have full control over trunsactions since we're already in TransactionTemplate and it will call this.transactionManager.commit(status); from it's execute. Or am i missing somehting?
Once again...
Tasklet = step
You can do nearly anything you need inside the Tasklet jast autowire appropriate members (JobRepository, JobOperator, JobLauncher etc)
don't agree![]()
How about a TaskletStep with transaction propagation=NEVER? Should be identical to what you would get with a custom step (which is easy to implement but hard to integrate with the XML DSL as you have noted - if you want it to be easier maybe you can raise a JIRA?). I've seen this used in real projects quite successfully - but be very careful what you wish for: if I were you I would prefer to let Spring Batch manage my transactions, so it has to be a special use case to justify propagation=NEVER.
Thank you for pointing to propagation=NEVER - it's not mentioned in the docs and i missed it while browsing sources. And i still created
Jira improvement, just in case, may be useful.