BatchSynchQueue.java
001 package org.apache.qpid.util.concurrent;
002 /*
003  
004  * Licensed to the Apache Software Foundation (ASF) under one
005  * or more contributor license agreements.  See the NOTICE file
006  * distributed with this work for additional information
007  * regarding copyright ownership.  The ASF licenses this file
008  * to you under the Apache License, Version 2.0 (the
009  * "License"); you may not use this file except in compliance
010  * with the License.  You may obtain a copy of the License at
011  
012  *   http://www.apache.org/licenses/LICENSE-2.0
013  
014  * Unless required by applicable law or agreed to in writing,
015  * software distributed under the License is distributed on an
016  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017  * KIND, either express or implied.  See the License for the
018  * specific language governing permissions and limitations
019  * under the License.
020  
021  */
022 
023 
024 import java.util.Collection;
025 import java.util.concurrent.BlockingQueue;
026 
027 /**
028  * BatchSynchQueue is an abstraction of the classic producer/consumer buffer pattern for thread interaction. In this
029  * pattern threads can deposit data onto a buffer whilst other threads take data from the buffer and perform usefull
030  * work with it. A BatchSynchQueue adds to this the possibility that producers can be blocked until their data is
031  * consumed or until a consumer chooses to release the producer some time after consuming the data from the queue.
032  *
033  <p>There are a number of possible advantages to using this technique when compared with having the producers
034  * processing their own data:
035  *
036  <ul>
037  <li>Data may be deposited asynchronously in the buffer allowing the producers to continue running.</li>
038  <li>Data may be deposited synchronously in the buffer so that producers wait until their data has been processed
039  *     before being allowed to continue.</li>
040  <li>Variable rates of production/consumption can be smoothed over by the buffer as it provides space in memory to
041  *     hold data between production and consumption.</li>
042  <li>Consumers may be able to batch data as they consume it leading to more efficient consumption over
043  *     individual data item consumption where latency associated with the consume operation can be ammortized.
044  *     For example, it may be possibly to ammortize the cost of a disk seek over many producers.</li>
045  <li>Data from seperate threads can be combined together in the buffer, providing a convenient way of spreading work
046  *     amongst many workers and gathering the results together again.</li>
047  <li>Different types of queue can be used to hold the buffer, resulting in different processing orders. For example,
048  *     lifo, fifo, priority heap, etc.</li>
049  </ul>
050  *
051  <p/>The asynchronous type of producer/consumer buffers is already well supported by the java.util.concurrent package
052  * (in Java 5) and there is also a synchronous queue implementation available there too. This interface extends the
053  * blocking queue with some more methods for controlling a synchronous blocking queue. In particular it adds additional
054  * take methods that can be used to take data from a queue without releasing producers, so that consumers have an
055  * opportunity to confirm correct processing of the data before producers are released. It also adds a put method with
056  * exceptions so that consumers can signal exception cases back to producers where there are errors in the data.
057  *
058  <p/>This type of queue is usefull in situations where consumers can obtain an efficiency gain by batching data
059  * from many threads but where synchronous handling of that data is neccessary because producers need to know that
060  * their data has been processed before they continue. For example, sending a bundle of messages together, or writing
061  * many records to disk at once, may result in improved performance but the originators of the messages or disk records
062  * need confirmation that their data has really been sent or saved to disk.
063  *
064  <p/>The consumer can put an element back onto the queue or send an error message to the elements producer using the
065  {@link SynchRecord} interface.
066  *
067  <p/>The {@link #take()}{@link #drainTo(java.util.Collection<? super E>)}  and
068  {@link #drainTo(java.util.Collection<? super E>, int)} methods from {@link BlockingQueue} should behave as if they
069  * have been called with unblock set to false. That is they take elements from the queue but leave the producers
070  * blocked. These methods do not return collections of {@link SynchRecord}s so they do not supply an interface through
071  * which errors or re-queuings can be applied. If these methods are used then the consumer must succesfully process
072  * all the records it takes.
073  *
074  <p/>The {@link #put} method should silently swallow any exceptions that consumers attempt to return to the caller.
075  * In order to handle exceptions the {@link #tryPut} method must be used.
076  *
077  <p/><table id="crc"><caption>CRC Card</caption>
078  <tr><th> Responsibilities <th> Collaborations
079  <tr><td> Handle synchronous puts, with possible exceptions.
080  <tr><td> Allow consumers to take many records from a queue in a batch.
081  <tr><td> Allow consumers to decide when to unblock synchronous producers.
082  </table>
083  */
084 public interface BatchSynchQueue<E> extends BlockingQueue<E>
085 {
086     /**
087      * Tries a synchronous put into the queue. If a consumer encounters an exception condition whilst processing the
088      * data that is put, then this is returned to the caller wrapped inside a {@link SynchException}.
089      *
090      @param e The data element to put into the queue.
091      *
092      @throws InterruptedException If the thread is interrupted whilst waiting to write to the queue or whilst waiting
093      *                              on its entry in the queue being consumed.
094      @throws SynchException       If a consumer encounters an error whilst processing the data element.
095      */
096     public void tryPut(E ethrows InterruptedException, SynchException;
097 
098     /**
099      * Takes all available data items from the queue or blocks until some become available. The returned items
100      * are wrapped in a {@link SynchRecord} which provides an interface to requeue them or send errors to their
101      * producers, where the producers are still blocked.
102      *
103      @param c       The collection to drain the data items into.
104      @param unblock If set to <tt>true</tt> the producers for the taken items will be immediately unblocked.
105      *
106      @return A count of the number of elements that were drained from the queue.
107      */
108     public SynchRef drainTo(Collection<SynchRecord<E>> c, boolean unblock);
109 
110     /**
111      * Takes up to maxElements available data items from the queue or blocks until some become available. The returned
112      * items are wrapped in a {@link SynchRecord} which provides an interface to requeue them or send errors to their
113      * producers, where the producers are still blocked.
114      *
115      @param c           The collection to drain the data items into.
116      @param maxElements The maximum number of elements to drain.
117      @param unblock     If set to <tt>true</tt> the producers for the taken items will be immediately unblocked.
118      *
119      @return A count of the number of elements that were drained from the queue.
120      */
121     public SynchRef drainTo(Collection<SynchRecord<E>> c, int maxElements, boolean unblock);
122 }