001/*
002 * Copyright 2006-2018 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.poller;
017
018import java.util.concurrent.Callable;
019import java.util.concurrent.Future;
020
021/**
022 * Interface for polling a {@link Callable} instance provided by the user. Use
023 * when you need to put something in the background (e.g. a remote invocation)
024 * and wait for the result, e.g.
025 * 
026 * <pre>
027 * Poller&lt;Result&gt; poller = ...
028 * 
029 * final long id = remoteService.execute(); // do something remotely
030 * 
031 * Future&lt;Result&gt; future = poller.poll(new Callable&lt;Result&gt; {
032 *     public Object call() {
033 *         // Look for the result (null if not ready)
034 *         return remoteService.get(id);
035 *     }
036 * });
037 * 
038 * Result result = future.get(1000L, TimeUnit.MILLISECONDS);
039 * </pre>
040 * 
041 * @author Dave Syer
042 * @author Mahmoud Ben Hassine
043 * 
044 */
045public interface Poller<T> {
046
047        /**
048         * Use the callable provided to poll for a non-null result. The callable
049         * might be executed multiple times searching for a result, but once either
050         * a result or an exception has been observed the polling stops.
051         * 
052         * @param callable a {@link Callable} to use to retrieve a result
053         * @return a future which itself can be used to get the result
054         * @throws java.lang.Exception allows for checked exceptions
055         */
056        Future<T> poll(Callable<T> callable) throws Exception;
057
058}