簡體   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