001: package org.methodize.nntprss.rss;
002:
003: /* -----------------------------------------------------------
004: * nntp//rss - a bridge between the RSS world and NNTP clients
005: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
006: *
007: * email: nntprss@methodize.org
008: * mail: Methodize Solutions
009: * PO Box 3865
010: * Grand Central Station
011: * New York NY 10163
012: *
013: * This file is part of nntp//rss
014: *
015: * nntp//rss is free software; you can redistribute it
016: * and/or modify it under the terms of the GNU General
017: * Public License as published by the Free Software Foundation;
018: * either version 2 of the License, or (at your option) any
019: * later version.
020: *
021: * This program is distributed in the hope that it will be
022: * useful, but WITHOUT ANY WARRANTY; without even the implied
023: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
024: * PURPOSE. See the GNU General Public License for more
025: * details.
026: *
027: * You should have received a copy of the GNU General Public
028: * License along with this program; if not, write to the
029: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
030: * Boston, MA 02111-1307 USA
031: * ----------------------------------------------------- */
032:
033: import java.util.ConcurrentModificationException;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: import org.apache.log4j.Logger;
038: import org.apache.log4j.Priority;
039: import org.methodize.nntprss.util.FixedThreadPool;
040: import org.methodize.nntprss.util.SimpleThreadPool;
041:
042: /**
043: * @author Jason Brome <jason@methodize.org>
044: * @version $Id: ChannelPoller.java,v 1.5 2003/03/22 16:29:46 jasonbrome Exp $
045: */
046: public class ChannelPoller extends Thread {
047:
048: private Logger log = Logger.getLogger(ChannelPoller.class);
049:
050: private Map channels;
051: private boolean active = true;
052: // private SimpleThreadPool simpleThreadPool;
053: private FixedThreadPool fixedThreadPool;
054:
055: // private static final int MAX_POLL_THREADS = 20;
056: private static final int MAX_POLL_THREADS = 4;
057:
058: // Check pending polls every 30 seconds
059: private static final int POLL_INTERVAL = 30 * 1000;
060:
061: public ChannelPoller(Map channels) {
062: super ("Channel Poller");
063: this .channels = channels;
064: // simpleThreadPool = new SimpleThreadPool("Channel Poll Workers", "Channel Poll Worker", MAX_POLL_THREADS);
065: fixedThreadPool = new FixedThreadPool("Channel Poll Workers",
066: "Channel Poll Worker", MAX_POLL_THREADS);
067: }
068:
069: public synchronized void shutdown() {
070: active = false;
071: fixedThreadPool.shutdown();
072: this .notify();
073: }
074:
075: /**
076: * @see java.lang.Runnable#run()
077: */
078: public void run() {
079: while (active) {
080: if (log.isDebugEnabled()) {
081: log.debug("Checking feeds for poll action");
082: }
083:
084: try {
085: // Moved channel iterator retrieval within loop...
086: Iterator channelIter = channels.values().iterator();
087:
088: while (channelIter.hasNext() && active) {
089: Channel channel = (Channel) channelIter.next();
090: if (channel.isEnabled()) {
091: if (channel.isPolling()) {
092: channel.checkConnection();
093: }
094:
095: if (channel.isAwaitingPoll()) {
096: fixedThreadPool.run(channel);
097: }
098: }
099: }
100: } catch (ConcurrentModificationException cme) {
101: // Some channel management activity coincided with channel poll
102: // FIXME implement thread-safe approach
103: if (log.isDebugEnabled()) {
104: log
105: .debug("ConcurrentModificationException in Channel Poller");
106: }
107: } catch (Exception e) {
108: if (log.isEnabledFor(Priority.WARN)) {
109: log.warn("Exception thrown in Channel Poller", e);
110: }
111: }
112:
113: if (log.isDebugEnabled()) {
114: log.debug("Finished checking feeds for poll action");
115: }
116:
117: synchronized (this ) {
118: if (active) {
119: try {
120: // Check pending polls every 30 seconds
121: wait(POLL_INTERVAL);
122: } catch (InterruptedException e) {
123: }
124: }
125: }
126: }
127: }
128:
129: }
|