001/*
002 * Copyright 2006-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.sample.common;
018
019import java.util.List;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.springframework.batch.core.StepExecution;
024import org.springframework.batch.core.listener.StepExecutionListenerSupport;
025import org.springframework.batch.item.ItemWriter;
026
027/**
028 * Simple module implementation that will always return true to indicate that
029 * processing should continue. This is useful for testing graceful shutdown of
030 * jobs.
031 * 
032 * @author Lucas Ward
033 * 
034 */
035public class InfiniteLoopWriter extends StepExecutionListenerSupport implements ItemWriter<Object> {
036        private static final Log LOG = LogFactory.getLog(InfiniteLoopWriter.class);
037
038        private StepExecution stepExecution;
039        private int count = 0;
040
041        /**
042         * @see org.springframework.batch.core.StepExecutionListener#beforeStep(StepExecution)
043         */
044        @Override
045        public void beforeStep(StepExecution stepExecution) {
046                this.stepExecution = stepExecution;
047        }
048
049        public InfiniteLoopWriter() {
050                super();
051        }
052
053        @Override
054        public void write(List<? extends Object> items) throws Exception {
055                try {
056                        Thread.sleep(500);
057                }
058                catch (InterruptedException e) {
059                        Thread.currentThread().interrupt();
060                        throw new IllegalStateException("Job interrupted.", e);
061                }
062
063                stepExecution.setWriteCount(++count);
064                LOG.info("Executing infinite loop, at count=" + count);
065        }
066}