繁体   English   中英

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

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

对于所有春季发烧友来说,这是一个很好的挑战。 希望有人可以破解!!!

我使用Spring批处理提取过程。 我有两个类'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();
    }


}

第二类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();      

}

}

以下是上下文文件的片段

`<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>`

我的问题很简单,如何将两个变量/对象“服务”和“工厂”从ExtractBatchJob类传递给TaskletImpl类。

最干净的解决方案是使用弹簧注入机制在电线servicefactory进行。 您有两种解决方案:

  1. SpringBusFactory创建为Spring bean,并将其连接到tasklet
  2. 为您的工作定义一个ContextBean (作为单例),创建SpringBusFactory并将其设置为ContextBean属性; 将此豆连接到您的tasklet

如果要使用在Spring上下文外部创建的对象(我指的是new对象),必须将其注入Spring上下文。

暂无
暂无

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

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