de.nava.informa.utils.poller
Class Poller

java.lang.Object
  extended by de.nava.informa.utils.poller.Poller

public class Poller
extends Object

Background poller of channels and groups of channels. Default poller period of channel is set to an hour.

Each Poller instance has its own scheduler to manage the schedule of updates and worker threads manager to manage processing of updates.

Right after the instance of Poller is created you might be interested in adding some observers (addObserver()) to receive events on channels updates. Registered observers are added to the composite observer which is following Composite pattern to combine all of the listeners and present them as single instance convenient for operation. This instance is passed to all newly created worker threads. Later it will be used by worker threads to report about changes.

If you are interested in making changes directly in persistence storage then, please, look at PersistenceObserver class. It's simple implementation of persistence changer based on Observer events.

You also may require to filter newly found items in channels being polled. Using Approvers you can receive events only when wanted items are detected. Each registered Approver votes for addition of newly detected item in channel. All of registered Approvers should vote for addition in order to send event about newly detected item to Observers.

Finally, you need to register some channels to get benefits from Poller. Using appropriate methods you can register and unregister channels, request immediate updates and change polling period for all regitered channels at once.

Poller has different item scanning policies. By default, it uses POLICY_SCAN_ALL which means that every item in the feed is scanned. It's possible to tell Poller to skip items which are going after the first detected existing item. It is very convenient when you need to find only really new items. Use POLICY_SKIP_AFTER_EXISTING policy for that.

It's also possible to specify user-agent name to be used when making connection through HTTP protocol. You can do so only during Poller construction.

When the poller will be ready to parse the feed it will pass the opened InputStream to InputSourceProviderIF implementation specified during Poller creation. It's convenient if some additional wrapping of input streams is required to, for example, pre-process data in some way before it gets into the parser. One of the coolest applications could be is reading of compressed streams, which are decompressed in real-time. The parser will get decompressed copy and will not notice the change.

Author:
Aleksey Gureev (spyromus@noizeramp.com)

Field Summary
static int POLICY_SCAN_ALL
          Scanning for new items performed fully.
static int POLICY_SKIP_AFTER_EXISTING
          Scanning for new items in channel finishes when existing item detected.
 
Constructor Summary
Poller()
          Creates poller with default number of worker threads, POLICY_SCAN_ALL scanning policy and default user agent.
Poller(int workerThreads)
          Creates poller with POLICY_SCAN_ALL scanning policy and default user agent.
Poller(int workerThreads, int itemScanningPolicy)
          Creates poller with default user agent.
Poller(int workerThreads, int itemScanningPolicy, String userAgent)
          Creates poller.
Poller(int workerThreads, int itemScanningPolicy, String userAgent, InputSourceProviderIF inputSourceProvider, InputStreamProviderIF inputStreamProvider)
          Creates poller.
 
Method Summary
 void addApprover(PollerApproverIF approver)
          Adds approver to the list.
 void addObserver(PollerObserverIF observer)
          Adds observer to the list of interested parties.
 void registerChannel(ChannelIF channel)
          Register new channel for background poller.
 void registerChannel(ChannelIF channel, long period)
          Register new channel for background poller with custom period.
 void registerChannel(ChannelIF channel, long delay, long period)
          Register new channel for background poller with custom period.
 void removeApprover(PollerApproverIF approver)
          Removes approver from the list.
 void removeObserver(PollerObserverIF observer)
          Removes observer from the list.
 void setPeriod(long period)
          Sets global update period to the specified value.
 void setWorkerThreads(int count)
          Sets count on number of poller threads.
 void unregisterChannel(ChannelIF channel)
          Unregister the channel from background poller.
 void updateChannel(ChannelIF channel)
          Performs immediate update of the channel and reworks the schedule starting from current time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POLICY_SKIP_AFTER_EXISTING

public static final int POLICY_SKIP_AFTER_EXISTING
Scanning for new items in channel finishes when existing item detected.

See Also:
Constant Field Values

POLICY_SCAN_ALL

public static final int POLICY_SCAN_ALL
Scanning for new items performed fully.

See Also:
Constant Field Values
Constructor Detail

Poller

public Poller()
Creates poller with default number of worker threads, POLICY_SCAN_ALL scanning policy and default user agent.


Poller

public Poller(int workerThreads)
Creates poller with POLICY_SCAN_ALL scanning policy and default user agent.

Parameters:
workerThreads - desired number of worker threads.

Poller

public Poller(int workerThreads,
              int itemScanningPolicy)
Creates poller with default user agent.

Parameters:
workerThreads - desired number of worker threads.
itemScanningPolicy - policy of item scanning.

Poller

public Poller(int workerThreads,
              int itemScanningPolicy,
              String userAgent)
Creates poller.

Parameters:
workerThreads - desired number of worker threads.
itemScanningPolicy - policy of item scanning.
userAgent - user agent to be used for HTTP connections or NULL for default.

Poller

public Poller(int workerThreads,
              int itemScanningPolicy,
              String userAgent,
              InputSourceProviderIF inputSourceProvider,
              InputStreamProviderIF inputStreamProvider)
Creates poller.

Parameters:
workerThreads - desired number of worker threads.
itemScanningPolicy - policy of item scanning.
userAgent - user agent to be used for HTTP connections or NULL for default.
inputSourceProvider - provider of InputSource ready for parsing for a given feed InputStream.
inputStreamProvider - provider of InputStream ready for fetching data of feed.
Method Detail

setWorkerThreads

public final void setWorkerThreads(int count)
Sets count on number of poller threads. This method will not kill all currently running extra threads. It will instruct extra threads to terminate once their job is over or create new threads if new count is bigger than previous.

Parameters:
count - new count on number of poller threads.

registerChannel

public final void registerChannel(ChannelIF channel)
Register new channel for background poller.

Parameters:
channel - channel to register.

registerChannel

public final void registerChannel(ChannelIF channel,
                                  long period)
Register new channel for background poller with custom period.

Parameters:
channel - channel.
period - period in millis.

registerChannel

public final void registerChannel(ChannelIF channel,
                                  long delay,
                                  long period)
Register new channel for background poller with custom period.

Parameters:
channel - channel.
delay - delay before first check.
period - period in millis.

updateChannel

public final void updateChannel(ChannelIF channel)
Performs immediate update of the channel and reworks the schedule starting from current time. If channel isn't registered yet it will be registered with normal priority.

Parameters:
channel - channel to update.

unregisterChannel

public final void unregisterChannel(ChannelIF channel)
Unregister the channel from background poller.

Parameters:
channel - channel to unregister.

addObserver

public final void addObserver(PollerObserverIF observer)
Adds observer to the list of interested parties.

Parameters:
observer - new observer.

removeObserver

public final void removeObserver(PollerObserverIF observer)
Removes observer from the list.

Parameters:
observer - observer to remove.

addApprover

public final void addApprover(PollerApproverIF approver)
Adds approver to the list.

Parameters:
approver - approver.

removeApprover

public final void removeApprover(PollerApproverIF approver)
Removes approver from the list.

Parameters:
approver - approver object.

setPeriod

public final void setPeriod(long period)
Sets global update period to the specified value. All tasks will be rescheduled.

Parameters:
period - period in millis.


Copyright © 2002-2007 Niko Schmuck. All Rights Reserved.