View Javadoc

1   /*
2    * Copyright 2002-2007 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  
17  package org.springframework.batch.repeat.support;
18  
19  import java.util.NoSuchElementException;
20  import java.util.concurrent.BlockingQueue;
21  
22  import org.springframework.core.task.TaskExecutor;
23  
24  /**
25   * Abstraction for queue of {@link ResultHolder} objects. Acts a bit likeT a
26   * {@link BlockingQueue} with the ability to count the number of items it
27   * expects to ever hold. When clients schedule an item to be added they call
28   * {@link #expect()}, and then collect the result later with {@link #take()}.
29   * Result providers in another thread call {@link #put(Object)} to notify the
30   * expecting client of a new result.
31   * 
32   * @author Dave Syer
33   * @author Ben Hale
34   */
35  interface ResultQueue<T> {
36  
37  	/**
38  	 * In a master-slave pattern, the master calls this method paired with
39  	 * {@link #take()} to manage the flow of items. Normally a task is submitted
40  	 * for processing in another thread, at which point the master uses this
41  	 * method to keep track of the number of expected results. It has the
42  	 * personality of an counter increment, rather than a work queue, which is
43  	 * usually managed elsewhere, e.g. by a {@link TaskExecutor}.<br/><br/>
44  	 * Implementations may choose to block here, if they need to limit the
45  	 * number or rate of tasks being submitted.
46  	 * 
47  	 * @throws InterruptedException if the call blocks and is then interrupted.
48  	 */
49  	void expect() throws InterruptedException;
50  
51  	/**
52  	 * Once it is expecting a result, clients call this method to satisfy the
53  	 * expectation. In a master-worker pattern, the workers call this method to
54  	 * deposit the result of a finished task on the queue for collection.
55  	 * 
56  	 * @param result the result for later collection.
57  	 * 
58  	 * @throws IllegalArgumentException if the queue is not expecting a new
59  	 * result
60  	 */
61  	void put(T result) throws IllegalArgumentException;
62  
63  	/**
64  	 * Gets the next available result, blocking if there are none yet available.
65  	 * 
66  	 * @return a result previously deposited
67  	 * 
68  	 * @throws NoSuchElementException if there is no result expected
69  	 * @throws InterruptedException if the operation is interrupted while
70  	 * waiting
71  	 */
72  	T take() throws NoSuchElementException, InterruptedException;
73  
74  	/**
75  	 * Used by master thread to verify that there are results available from
76  	 * {@link #take()} without possibly having to block and wait.
77  	 * 
78  	 * @return true if there are no results available
79  	 */
80  	boolean isEmpty();
81  
82  	/**
83  	 * Check if any results are expected. Usually used by master thread to drain
84  	 * queue when it is finished.
85  	 * 
86  	 * @return true if more results are expected, but possibly not yet
87  	 * available.
88  	 */
89  	public boolean isExpecting();
90  
91  }