001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.jicarilla.plumbing;
056:
057: import org.jicarilla.plumbing.Collector;
058: import org.jicarilla.plumbing.Source;
059: import org.jicarilla.lang.Assert;
060:
061: import java.util.ArrayList;
062: import java.util.List;
063:
064: /**
065: * A collector which tries to gather messages from a random subset of its
066: * sources (the subset is of fixed size though).
067: *
068: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
069: * @version $Id: SimpleCollector.java,v 1.2 2004/03/23 13:37:58 lsimons Exp $
070: */
071: public class SimpleCollector implements Collector {
072: /** how long to poll a source when doing a {@link #take()}. */
073: public final static int POLLING_TIME = 20;
074: /** how often to retry polling when doing a {@link #take()}. */
075: public final static int RETRIES = 5;
076: /**
077: * what to split the polling time by when doing a {@link #poll(long)}.
078: */
079: public final static long POLLING_TIME_DIVIDE = 5;
080:
081: /** the sources to collect from. */
082: protected final List m_sources;
083: /** synchronization point for {@link m_sources}. */
084: protected final Object m_mutex = new Object();
085:
086: /**
087: * Create a new instance backed by an {@link ArrayList}.
088: */
089: public SimpleCollector() {
090: m_sources = new ArrayList();
091: }
092:
093: /**
094: * Add a source to the processor.
095: *
096: * @param source the source to add
097: */
098: public void addSource(final Source source) {
099: Assert.assertNotNull(source);
100:
101: synchronized (m_mutex) {
102: m_sources.add(source);
103: }
104: }
105:
106: /**
107: * See {@link org.jicarilla.plumbing.Stage#take()}.
108: *
109: * @return the message retrieved
110: * @throws InterruptedException if the current thread has been
111: * {@link java.lang.Thread#interrupt()}ed
112: */
113: public Object take() throws InterruptedException {
114: for (int i = 0; i < RETRIES; i++) {
115: final Source source = selectSource();
116: final Object result = source.poll(POLLING_TIME);
117: if (result != null) {
118: return result;
119: }
120: }
121:
122: final Source source = selectSource();
123: return source.take();
124: }
125:
126: /**
127: * See {@link org.jicarilla.plumbing.Stage#poll(long)}.
128: *
129: * @param time how long to try and fetch before giving up
130: * @return the message retrieved, or null if none was retrieved within
131: * the specified time interval
132: * @throws InterruptedException if the current thread has been
133: * {@link java.lang.Thread#interrupt()}ed
134: */
135: public Object poll(final long time) throws InterruptedException {
136: final long pollingTime = (time / POLLING_TIME_DIVIDE) + 1;
137:
138: for (int i = 0; i < POLLING_TIME_DIVIDE; i++) {
139: final Source source = selectSource();
140: final Object result = source.poll(pollingTime);
141: if (result != null) {
142: return result;
143: }
144: }
145:
146: return null;
147: }
148:
149: /**
150: * Select a random source from all referenced sources.
151: *
152: * @return the selected source
153: */
154: protected Source selectSource() {
155: synchronized (m_mutex) {
156: final int selected = (int) (Math.random() * m_sources
157: .size());
158: return (Source) m_sources.get(selected);
159: }
160: }
161: }
|