0

To all Spring enthusiasts, here's a good challenge. Hope someone can crack it!!!

I use Spring to batch the extraction process. I have two classes 'ExtractBatchJob' and 'TaskletImpl'

public class ExtractBatchJob {

/** Logger for current class */
private static Log log = LogFactory.getLog(Extractor.class);

public static void main(String[] args)
        throws IOrganizationServiceRetrieveMultipleOrganizationServiceFaultFaultFaultMessage,
        IOException {

    ApplicationContext context = new ClassPathXmlApplicationContext(
            "/META-INF/cxf/batch-context.xml");

    SpringBusFactory factory = new SpringBusFactory(context);
    Bus bus = factory.createBus();
    SpringBusFactory.setDefaultBus(bus);

    IOrganizationService service = (IOrganizationService) factory
            .getApplicationContext().getBean("service");

    JobLauncher jobLauncher = (JobLauncher)context.getBean("jobLauncher");
    Job job = (Job) context.getBean("firstBatchJob");

    try {
        JobExecution execution = jobLauncher.run(job, new JobParameters());
    }catch (Exception e){
        e.printStackTrace();
    }


}

The second class TaskletImpl implements the Spring Tasklet interface.

public class TaskletImpl implements Tasklet {

/** Logger for current class */
private static Log log = LogFactory.getLog(CRMExtractor.class);

/* (non-Javadoc)
 * @see org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)
 */
@Overridepublic RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
        throws Exception {
    // TODO Auto-generated method stub
    log.info("************ CRM Extraction Batch Job is executing!!! *******");  
  //QUESTION: To Extract Entity from third party
  // web service need   object reference for 
  //factory and service from ExtractBatchjob class
            List<Entity> orderEntities = getEntities("orderQueryImpl", factory, service);
            OrderDao orderDao = (OrderDao) factory.getApplicationContext()
                    .getBean("orderDao");
            orderDao.batchInsert(orderEntities);*/  
    return RepeatStatus.FINISHED;
}

public static List<Entity> getEntities(String queryImpl, SpringBusFactory factory,
        IOrganizationService service)
        throws IOrganizationServiceRetrieveMultipleOrganizationServiceFaultFaultFaultMessage,
        IOException {
    QueryBuilder queryBuilder = (QueryBuilderTemplate) factory
            .getApplicationContext().getBean(queryImpl);
    QueryExpression queryExpr = queryBuilder.getQuery();
    EntityCollection result = service
            .retrieveMultiple(queryExpr);
    return result.getEntities().getEntities();      

}

}

Below is the snippet of the context file

`<import resource="cxf.xml" />
<bean id="firstBatch" class="com.abc.model.TaskletImpl" />
<batch:step id="firstBatchStepOne">
    <batch:tasklet ref="firstBatch" />
</batch:step>
<batch:job id="firstBatchJob">
    <batch:step id="stepOne" parent="firstBatchStepOne" />
</batch:job>`

My question is quite straightforward, how do I pass the two variables/objects 'service' and 'factory' to the TaskletImpl class from the ExtractBatchJob class.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Why are you using `factory.getApplicationContext().getBean("orderDao");` instead of standard Spring DI? I think you are misusing Spring capabilities. – Luca Basso Ricci Jan 15 '16 at 07:51

1 Answers1

0

The cleanest solution is to wire service and factory using Spring injection mechanism. You have two solution:

  1. Create SpringBusFactory as Spring bean and wire it into tasklet
  2. Define a ContextBean (as singleton) for you job, create SpringBusFactory and set it as property of ContextBean; wire this bean to your tasklet

If you want to use object created outside Spring context (with new I meant) must be injected into Spring context.

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69