Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.batch.poller;
 
A Poller that uses the callers thread to poll for a result as soon as it is asked for. This is often appropriate if you expect a result relatively quickly, or if there is only one such result expected (otherwise it is more efficient to use a background thread to do the polling).

Parameters:
<S> the type of the result
Author(s):
Dave Syer
 
 public class DirectPoller<S> implements Poller<S> {
 
 	private final long interval;
 
 	public DirectPoller(long interval) {
 		this. = interval;
 	}

Get a future for a non-null result from the callback. Only when the result is asked for (using java.util.concurrent.Future.get() or java.util.concurrent.Future.get(long,java.util.concurrent.TimeUnit) will the polling actually start.

 
 	public Future<S> poll(Callable<S> callablethrows Exception {
 		return new DirectPollingFuture<S>(callable);
 	}
 
 	private static class DirectPollingFuture<S> implements Future<S> {
 
 		private final long startTime = System.currentTimeMillis();
 
 		private volatile boolean cancelled;
 
 		private volatile S result = null;
 
 		private final long interval;
 
 		private final Callable<S> callable;
 
 		public DirectPollingFuture(long intervalCallable<S> callable) {
 			this. = interval;
 			this. = callable;
 		}
 
 		public boolean cancel(boolean mayInterruptIfRunning) {
 			 = true;
 			return true;
 		}
 
 		public S get() throws InterruptedExceptionExecutionException {
 			try {
 				return get(-1, .);
 			}
 			catch (TimeoutException e) {
 				throw new IllegalStateException("Unexpected timeout waiting for result"e);
 			}
 		}
 
 		public S get(long timeoutTimeUnit unitthrows InterruptedExceptionExecutionExceptionTimeoutException {
 
 			try {
 			}
 			catch (Exception e) {
 				throw new ExecutionException(e);
 			}
 
 			Long nextExecutionTime =  + ;
 			long currentTimeMillis = System.currentTimeMillis();
 			long timeoutMillis = ..convert(timeoutunit);
 
 			while ( == null && !) {
 
 				long delta = nextExecutionTime - ;
				if (delta >= timeoutMillis && timeoutMillis > 0) {
					throw new TimeoutException("Timed out waiting for task to return non-null result");
				}
				if (nextExecutionTime > currentTimeMillis) {
					Thread.sleep(nextExecutionTime - currentTimeMillis);
				}
				currentTimeMillis = System.currentTimeMillis();
				nextExecutionTime = currentTimeMillis + ;
				try {
				}
				catch (Exception e) {
					throw new ExecutionException(e);
				}
			}
			return ;
		}
		public boolean isCancelled() {
			return ;
		}
		public boolean isDone() {
			return  ||  != null;
		}
	}
New to GrepCode? Check out our FAQ X