View Javadoc

1   /*
2    * Copyright 2006-2007 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  
17  package org.springframework.batch.sample.common;
18  
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.springframework.batch.core.StepExecution;
24  import org.springframework.batch.core.StepExecutionListener;
25  import org.springframework.batch.core.listener.StepExecutionListenerSupport;
26  import org.springframework.batch.item.ItemWriter;
27  
28  /**
29   * Simple module implementation that will always return true to indicate that
30   * processing should continue. This is useful for testing graceful shutdown of
31   * jobs.
32   * 
33   * @author Lucas Ward
34   * 
35   */
36  public class InfiniteLoopWriter extends StepExecutionListenerSupport implements ItemWriter<Object> {
37  
38  	private StepExecution stepExecution;
39  
40  	private int count = 0;
41  
42  	private static final Log logger = LogFactory.getLog(InfiniteLoopWriter.class);
43  
44  	/**
45  	 * @see StepExecutionListener#beforeStep(StepExecution)
46  	 */
47  	@Override
48  	public void beforeStep(StepExecution stepExecution) {
49  		this.stepExecution = stepExecution;
50  	}
51  
52  	/**
53  	 * 
54  	 */
55  	public InfiniteLoopWriter() {
56  		super();
57  	}
58  
59  	public void write(List<? extends Object> items) throws Exception {
60  		try {
61  			Thread.sleep(500);
62  		}
63  		catch (InterruptedException e) {
64  			Thread.currentThread().interrupt();
65  			throw new RuntimeException("Job interrupted.");
66  		}
67  		stepExecution.setWriteCount(++count);
68  		logger.info("Executing infinite loop, at count=" + count);
69  	}
70  }