简体   繁体   English

在Spring Batch中将参数从BatchJob传递到Tasklet

[英]Passing arguments from BatchJob to Tasklet in Spring Batch

To all Spring enthusiasts, here's a good challenge. 对于所有春季发烧友来说,这是一个很好的挑战。 Hope someone can crack it!!! 希望有人可以破解!!!

I use Spring to batch the extraction process. 我使用Spring批处理提取过程。 I have two classes 'ExtractBatchJob' and 'TaskletImpl' 我有两个类'ExtractBatchJob'和'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. 第二类TaskletImpl实现Spring Tasklet接口。

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. 我的问题很简单,如何将两个变量/对象“服务”和“工厂”从ExtractBatchJob类传递给TaskletImpl类。

The cleanest solution is to wire service and factory using Spring injection mechanism. 最干净的解决方案是使用弹簧注入机制在电线servicefactory进行。 You have two solution: 您有两种解决方案:

  1. Create SpringBusFactory as Spring bean and wire it into tasklet SpringBusFactory创建为Spring bean,并将其连接到tasklet
  2. Define a ContextBean (as singleton) for you job, create SpringBusFactory and set it as property of ContextBean ; 为您的工作定义一个ContextBean (作为单例),创建SpringBusFactory并将其设置为ContextBean属性; wire this bean to your tasklet 将此豆连接到您的tasklet

If you want to use object created outside Spring context (with new I meant) must be injected into Spring context. 如果要使用在Spring上下文外部创建的对象(我指的是new对象),必须将其注入Spring上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM