FileDocCategorySizeDatePackage
RSSSubscriber.javaAPI DocExample4391Sun Nov 23 16:30:32 GMT 2003apexample.model

RSSSubscriber

public class RSSSubscriber extends Thread
A class to manage subscriptions to RSS pages

Fields Summary
private static final int
UPDATE_FREQ
the default update frequency. 30 seconds
private SortedSet
subscriptions
a set of subscription information, sorted by the next update time
private Map
cache
existing subscriptions
private boolean
quit
when to quit
private static RSSSubscriber
subscriber
singelton subscriber
Constructors Summary
RSSSubscriber()
Constructor. Initialize data structures.

        subscriptions = new TreeSet();
        cache = Collections.synchronizedMap(new HashMap());
        
        // exit if we're the only thread left
        setDaemon(true);
    
Methods Summary
public RSSInfogetInfo(java.lang.String url)
Get an RSSInfo object from cache, or create a new subscritpion if its not in the cache.

        if (cache.containsKey(url)) {
            return (RSSInfo)cache.get(url);
        }

        // add to cache
        RSSInfo rInfo = new RSSInfo();
        rInfo.parse(url);
        cache.put(url, rInfo);
        
        // create new subscription
        RSSSubscription newSub = new RSSSubscription();
        newSub.url = url;
        newSub.updateFreq = UPDATE_FREQ;
        putSubscription(newSub);
        
        return rInfo;
    
public static apexample.model.RSSSubscribergetInstance()
Get a reference to the subscriber. Use this instead of the constructor.

    
    /* initialize singelton the singleton */
     
        subscriber = new RSSSubscriber();
        subscriber.start();
    
        return subscriber;
    
private synchronized apexample.model.RSSSubscriber$RSSSubscriptiongetSubscription()
Wait for next subscription to be ready for refresh

        while(true) {
            while(subscriptions.size() == 0) {
                try { wait(); } catch(InterruptedException ie) {}
            }
            
            RSSSubscription nextSub = (RSSSubscription)subscriptions.first();
               
            long curTime = System.currentTimeMillis();
            if(curTime >= nextSub.nextUpdate) {
                subscriptions.remove(nextSub);
                return nextSub;
            }
            
            try {
                wait(nextSub.nextUpdate - curTime);
            } catch(InterruptedException ie) {}
        }
    
private synchronized voidputSubscription(apexample.model.RSSSubscriber$RSSSubscription subs)
Add a subscription

        subs.nextUpdate = System.currentTimeMillis() + subs.updateFreq;
        subscriptions.add(subs);
        notify();
    
public voidquit()
Cause this subscriber to stop

        this.quit = true;
    
public voidrun()
Update subscriptions as they become ready for refresh

        while(!quit) {
            RSSSubscription subs = getSubscription();
            
            try {
                RSSInfo rInfo = new RSSInfo();
                rInfo.parse(subs.url);
                cache.put(subs.url, rInfo);
            } catch(Exception ex) {
                ex.printStackTrace();
            }
            
            putSubscription(subs);
        }