View Javadoc

1   /*
2    * Copyright 2006-2010 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.poller;
17  
18  import java.util.concurrent.Callable;
19  import java.util.concurrent.Future;
20  
21  /**
22   * Interface for polling a {@link Callable} instance provided by the user. Use
23   * when you need to put something in the background (e.g. a remote invocation)
24   * and wait for the result, e.g.
25   * 
26   * <pre>
27   * Poller&lt;Result&gt; poller = ...
28   * 
29   * final long id = remoteService.execute(); // do something remotely
30   * 
31   * Future&lt;Result&gt; future = poller.poll(new Callable&lt;Result&gt; {
32   *     public Object call() {
33   *     	   // Look for the result (null if not ready)
34   *     	   return remoteService.get(id);
35   *     }
36   * });
37   * 
38   * Result result = future.get(1000L, TimeUnit.MILLSECONDS);
39   * </pre>
40   * 
41   * @author Dave Syer
42   * 
43   */
44  public interface Poller<T> {
45  
46  	/**
47  	 * Use the callable provided to poll for a non-null result. The callable
48  	 * might be executed multiple times searching for a result, but once either
49  	 * a result or an exception has been observed the polling stops.
50  	 * 
51  	 * @param callable a {@link Callable} to use to retrieve a result
52  	 * @return a future which itself can be used to get the result
53  	 * 
54  	 */
55  	Future<T> poll(Callable<T> callable) throws Exception;
56  
57  }