View Javadoc

1   /*
2    * Copyright 2012-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.batch.sample.config;
17  
18  import org.springframework.batch.core.Job;
19  import org.springframework.batch.core.Step;
20  import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
21  import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
22  import org.springframework.batch.item.ItemReader;
23  import org.springframework.batch.item.ItemWriter;
24  import org.springframework.batch.sample.domain.trade.Trade;
25  import org.springframework.batch.sample.domain.trade.internal.GeneratingTradeItemReader;
26  import org.springframework.batch.sample.support.RetrySampleItemWriter;
27  import org.springframework.beans.factory.annotation.Autowired;
28  import org.springframework.context.annotation.Bean;
29  import org.springframework.context.annotation.Configuration;
30  
31  /**
32   * @author Dave Syer
33   * 
34   */
35  @Configuration
36  public class RetrySampleConfiguration {
37  
38  	@Autowired
39  	private JobBuilderFactory jobs;
40  
41  	@Autowired
42  	private StepBuilderFactory steps;
43  
44  	@Bean
45  	public Job retrySample() throws Exception {
46  		return jobs.get("retrySample").start(step()).build();
47  	}
48  
49  	@Bean
50  	protected Step step() throws Exception {
51  		return steps.get("step").<Trade, Object> chunk(1).reader(reader()).writer(writer()).faultTolerant()
52  				.retry(Exception.class).retryLimit(3).build();
53  	}
54  
55  	@Bean
56  	protected ItemReader<Trade> reader() {
57  		GeneratingTradeItemReader reader = new GeneratingTradeItemReader();
58  		reader.setLimit(10);
59  		return reader;
60  	}
61  
62  	@Bean
63  	protected ItemWriter<Object> writer() {
64  		return new RetrySampleItemWriter<Object>();
65  	}
66  
67  }