KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > FeedRefreshDaemon


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// 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
26 // $Id: FeedRefreshDaemon.java,v 1.4 2004/06/28 19:33:44 niko_schmuck Exp $
27

28 package de.nava.informa.utils;
29
30 import java.util.Date JavaDoc;
31 import java.util.Timer JavaDoc;
32 import java.util.TimerTask JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 /**
38  * Use a Timer and TimerTask to periodically refresh feeds added by
39  * FeedManager.
40  *
41  * @author jga
42  */

43 public class FeedRefreshDaemon {
44
45   private static Log logger = LogFactory.getLog(FeedRefreshDaemon.class);
46
47   private Timer JavaDoc refreshTimer = null;
48
49   public FeedRefreshDaemon() {
50     logger.info("FeedRefresh Daemon instancied");
51     this.refreshTimer = new Timer JavaDoc(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 JavaDoc fiveMinuteLater = new Date JavaDoc(new Date JavaDoc().getTime() + (5 * 60 * 1000));
60
61     this.refreshTimer.schedule(refreshTask, fiveMinuteLater, (5 * 60 * 1000));
62   }
63
64   private class FeedRefreshTask extends TimerTask JavaDoc {
65
66     FeedManagerEntry f = null;
67
68     int nbError = 0;
69
70     public void setFeedME(FeedManagerEntry feedME) {
71       f = feedME;
72     }
73
74     public void run() {
75       try {
76         f.getFeed(); // call isOutofDate and optionnaly fetch new feed
77
logger.debug("feed refreshed" + f.getFeed().getLocation());
78       } catch (Exception JavaDoc e) {
79         this.nbError++;
80         logger.warn("Error retrieving feed" + f.toString() + " " + e);
81       }
82
83     }
84   }
85
86 }
Popular Tags