简体   繁体   English

春季批处理的批处理作业

[英]Batch job for spring batch

I have following job to be processed at certain time interval or on ad-hoc basis. 我有以下工作要在特定的时间间隔或临时进行。

Steps in the job are: 工作步骤如下:

  1. Call twitter api and gather tweets for bunch of users and write them in a file 调用twitter api并收集一堆用户的tweet并将其写入文件

  2. Read them from a file and Process them 从文件中读取它们并进行处理

  3. Dump processed results in database 将处理后的结果转储到数据库中

I want UI as well, where I can trigger a job on ad-hoc basis, moreover I should be able to provide parameters to it from UI. 我也需要UI,我可以在其中临时触发工作,此外,我应该能够从UI提供参数。

Was thinking of Spring batch for this but it is for more of a read -> process -> write kind of job. 为此考虑了Spring批处理,但这是为了更多的读取->处理->写入工作。 Here in first step I am generating data which is read by second step. 在第一步中,我正在生成第二步读取的数据。 Not sure if I can still use Spring batch for this OR there could be better way for this. 不知道我是否仍然可以使用Spring批处理,否则可能会有更好的方法。

Using spring batch, you can design your job as steps, each step has it's own reader, processor, writer. 使用spring batch,您可以将工作设计为步骤,每个步骤都有其自己的读取器,处理器,写入器。

<job id="yourJobID" >
    <step id="gatherTweet" next="processTweet">
        <tasklet>
            <chunk reader="tweetCollector" writer="tweetFileWriter"/>
        </tasklet>
    </step>
    <step id="processTweet">
        <tasklet>
            <chunk reader="tweetFileWriterReader" processor="tweetProcessor" writer="tweetDataBaseWriter"/>
        </tasklet>
    </step> 
</job>

For the UI to launch the job you can use spring batch admin. 为了让UI启动作业,您可以使用spring batch admin。

Alternatively you could configure your job and steps using Java rather than using XML. 另外,您可以使用Java而不是XML来配置作业和步骤。

For example myJob bean is defined below and uses step0, step1 and step2 (the steps are just beans too): 例如,下面定义了myJob bean并使用step0,step1和step2(步骤也只是bean):

// Job Definition //
@Bean
public Job myJob(JobBuilderFactory jobs)
{
    return jobs.get("My Twitter Job")
            .incrementer(new RunIdIncrementer())               
            .from(step0()).next(step1()).next(step2())
            .end()
            .build();
}

...

// Custom ItemReader that is Autowired in //    
@Autowired
protected ItemReader<TweetDto> gatherTweetItemReader;

...

@Bean
public Step step0()
{
    return steps.get("Step0 - gatherTweet")
            .tasklet(gatherTweetItemReader)
            .allowStartIfComplete(true) // Always run this step //
            .build();
}

// ... Step1 and Step2 definitions ... //

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

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