01: //
02: // Informa -- RSS Library for Java
03: // Copyright (c) 2002 by Niko Schmuck
04: //
05: // Niko Schmuck
06: // http://sourceforge.net/projects/informa
07: // mailto:niko_schmuck@users.sourceforge.net
08: //
09: // This library is free software.
10: //
11: // You may redistribute it and/or modify it under the terms of the GNU
12: // Lesser General Public License as published by the Free Software Foundation.
13: //
14: // Version 2.1 of the license should be included with this distribution in
15: // the file LICENSE. If the license is not included with this distribution,
16: // you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17: // or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18: // MA 02139 USA.
19: //
20: // This library is distributed in the hope that it will be useful,
21: // but WITHOUT ANY WARRANTY; without even the implied waranty of
22: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23: // Lesser General Public License for more details.
24:
25: // $Id: FeedRefreshDaemon.java,v 1.4 2004/06/28 19:33:44 niko_schmuck Exp $
26:
27: package de.nava.informa.utils;
28:
29: import java.util.Date;
30: import java.util.Timer;
31: import java.util.TimerTask;
32:
33: import org.apache.commons.logging.Log;
34: import org.apache.commons.logging.LogFactory;
35:
36: /**
37: * Use a Timer and TimerTask to periodically refresh feeds added by
38: * FeedManager.
39: *
40: * @author jga
41: */
42: public class FeedRefreshDaemon {
43:
44: private static Log logger = LogFactory
45: .getLog(FeedRefreshDaemon.class);
46:
47: private Timer refreshTimer = null;
48:
49: public FeedRefreshDaemon() {
50: logger.info("FeedRefresh Daemon instancied");
51: this .refreshTimer = new Timer(true);
52: }
53:
54: public void addFeed(FeedManagerEntry feed) {
55: FeedRefreshTask refreshTask = new FeedRefreshTask();
56: refreshTask.setFeedME(feed);
57: // verifying every 5 minutes
58: logger.info("scheduling new feed ");
59: Date fiveMinuteLater = new Date(new Date().getTime()
60: + (5 * 60 * 1000));
61:
62: this .refreshTimer.schedule(refreshTask, fiveMinuteLater,
63: (5 * 60 * 1000));
64: }
65:
66: private class FeedRefreshTask extends TimerTask {
67:
68: FeedManagerEntry f = null;
69:
70: int nbError = 0;
71:
72: public void setFeedME(FeedManagerEntry feedME) {
73: f = feedME;
74: }
75:
76: public void run() {
77: try {
78: f.getFeed(); // call isOutofDate and optionnaly fetch new feed
79: logger.debug("feed refreshed"
80: + f.getFeed().getLocation());
81: } catch (Exception e) {
82: this .nbError++;
83: logger.warn("Error retrieving feed" + f.toString()
84: + " " + e);
85: }
86:
87: }
88: }
89:
90: }
|