0

对于所有 Spring 爱好者来说,这是一个很好的挑战。希望有人能破解!!!

我使用 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 类。

4

1 回答 1

0

最干净的解决方案是连接servicefactory使用 Spring 注入机制。你有两个解决方案:

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

如果你想使用在 Spring 上下文之外创建的对象(new我的意思是)必须注入到 Spring 上下文中。

于 2016-01-15T07:53:42.077 回答