001/*
002 * Copyright 2006-2013 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 */
016package org.springframework.batch.core.step.tasklet;
017
018import java.util.concurrent.Callable;
019
020import org.springframework.batch.core.StepContribution;
021import org.springframework.batch.core.scope.context.ChunkContext;
022import org.springframework.batch.repeat.RepeatStatus;
023import org.springframework.beans.factory.InitializingBean;
024import org.springframework.util.Assert;
025
026/**
027 * Adapts a {@link Callable}<{@link RepeatStatus}> to the {@link Tasklet}
028 * interface.
029 *
030 * @author Dave Syer
031 *
032 */
033public class CallableTaskletAdapter implements Tasklet, InitializingBean {
034
035        private Callable<RepeatStatus> callable;
036
037        /**
038         * Public setter for the {@link Callable}.
039         * @param callable the {@link Callable} to set
040         */
041        public void setCallable(Callable<RepeatStatus> callable) {
042                this.callable = callable;
043        }
044
045        /**
046         * Assert that the callable is set.
047         *
048         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
049         */
050        @Override
051        public void afterPropertiesSet() throws Exception {
052                Assert.notNull(callable, "A Callable is required");
053        }
054
055        /**
056         * Execute the provided Callable and return its {@link RepeatStatus}. Ignores
057         * the {@link StepContribution} and the attributes.
058         * @see Tasklet#execute(StepContribution, ChunkContext)
059         */
060        @Override
061        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
062                return callable.call();
063        }
064
065}