public class Resource
extends java.lang.Object
PollableResource interface.
For example, the Entrust toolkit for Java defines an object called
ManagerTransport which implements the PollableResource
interface. The PollableResource interface implements a method called
checkstatus() which is used to implement
the logic of checking the status of the resource.
The processing of polling the resource happens as follows:
Timer object is used to schedule a PollTask
based on the number of seconds defined in this constructor. The minimum
period is 1 second and the maximum is 604800.start() method.
When started, the PollableResource's checkStatus()
method is called and a ResourceEvent is returnedsendNotification()
method is called to notify all registered ResourceListeners.Notification() method is called containing the
ResourceEvent. The ResourceEvent is therefore passed to the ResourceListener.
Since the ResourceEvent contains the source of the event which is this Resource,
any ResourceEventHandlers assocated with the Resource
can be used by the ResourceListener.
Note: The ResourceMonitor class implements the ResourceListener
interface and can be used to monitor all Resources. See ResourceMonitor for more information.
For polling of the resource to work, at least 1 or more ResourceListener
objects should be added into this resource.
Example usage:
//Create the PollableResource object
ManagerTransport transport = new ManagerTransport(ip, port);
//Create the Resource with the PollableResource and polling period
Resource resource = new Resource(60, transport); //1 minute period
//Set event notification threshold. In this case, all events will
//cause a notification to happen
resource.setEventTrigger(ResourceEvent.OKAY);
//Each resource should have a ResourceListener attached so that
//it is notified of polling events. In this case, the
//ResourceMonitor class can be used to listen to all Resources
ResourceMonitor monitor = new ResourceMonitor();
// Register the Listener with the resource.
resource.addResourceListener(monitor);
//Add the appropriate EventHandler. The ResourceOutputHandler can
//write the contents of the event message to a FileOutputStream or
//a PrintWriter.
resource.setEventHandler(new ResourceOutputHandler(new FileOutputStream("logfile.log")),ResourceMonitor.SAME_HANDLER_FOR_ALL);
// Each resource can be started independently, or the
// resource monitor can be used to start/stop all resources. If
// the resource monitor is used to start the resources, they must
// be added into the monitor using the addPolledResource.
resource.start();
.
.
.
//To stop the polling
resource.stop();
ResourceMonitor,
ResourceOutputHandler| Modifier and Type | Field and Description |
|---|---|
java.util.ArrayList |
m_handler
Stores the ResourceEventHandlers which should be used to handle a
ResourceEvent notification.
|
java.util.ArrayList |
m_ResourceListener
The Resource listeners.
|
| Constructor and Description |
|---|
Resource(int delay,
PollableResource resource)
Create a new Resource
|
| Modifier and Type | Method and Description |
|---|---|
void |
addEventHandler(ResourceEventHandler handler)
Set the ResourceEventHandler to be used to handle a
ResourceEvent. |
void |
addResourceListener(ResourceListener listener)
Registers the given ResourceListener so that it will be
notified when an event occurs on this Resource object.
|
java.util.ArrayList |
getEventHandler()
Get the ResourceEventHandler which should be used to handle
event notification for a Resource
|
int |
getEventTrigger() |
int |
getPollingInterval() |
PollableResource |
getResource() |
java.util.ArrayList |
getResourceListener() |
boolean |
isStarted() |
void |
removeEventHandler(ResourceEventHandler handler)
Removes the given ResourceEventHandler from the list of ResourceEventHandlers
that will be used to handle events for this Resource.
|
void |
removeResourceListener(ResourceListener listener)
Removes the given ResourceListener from the list of ResourceListeners
that will be notified when an event occurs on this Resource object.
|
void |
sendNotification(ResourceEvent event)
Sends a ResourceEvent to any
ResourceListeners that have been
registered with this Resource. |
void |
setEventTrigger(int trigger)
Sets the level at which event notification is triggered.
|
void |
setPollingInterval(int seconds)
Sets the polling interval, which is the length of time between
successful calls to the
PollableResource.checkStatus(Resource)
method. |
void |
start()
Start polling the
PollableResource with a frequency specified by the pollingInterval |
void |
stop()
Stop polling the
PollableResource |
public java.util.ArrayList m_ResourceListener
public java.util.ArrayList m_handler
public Resource(int delay,
PollableResource resource)
delay - the length of time in seconds between resource pollingresource - The PollableResourcepublic PollableResource getResource()
public void setPollingInterval(int seconds)
PollableResource.checkStatus(Resource)
method. The default is 5 minutes. The minimum amount of time is 1 second and the maximum is 604800 seconds (1 week).
seconds - The number of seconds ranging from 1...604800.
If seconds is less than 1, then it becomes 1, if the value of
seconds is greater than 604800, it becomes 604800.public int getPollingInterval()
public void addResourceListener(ResourceListener listener)
listener - The ResourceListenerpublic void removeResourceListener(ResourceListener listener)
listener - The ResourceListenerpublic java.util.ArrayList getResourceListener()
ResourceListener objects contained in an ArrayListpublic boolean isStarted()
public void sendNotification(ResourceEvent event) throws ResourceEventException
ResourceListeners that have been
registered with this Resource. The order in which ResourceEvents
are sent is the same as the order in which the ResourceListeners were added.event - the ResourceEvent indicating the status of the eventResourceEventExceptionpublic void addEventHandler(ResourceEventHandler handler)
ResourceEvent.handler - the handler which will be used to handle ResourceEventspublic void removeEventHandler(ResourceEventHandler handler)
handler - The ResourceEventHandlerpublic java.util.ArrayList getEventHandler()
public void setEventTrigger(int trigger)
Each ResourceEvent contains at least the following 4 notification types:
If this method is not set, then the default value will be
ResourceEvent.WARNING. This means that if the resource event
type is returned as ResourceEvent.OKAY, no notifications will be sent
to any registered ResourceListeners. By default,
only messages whose return types are ResourceEvent.WARNING or greater
will trigger a notification.
Note: Setting the trigger value too high will disable all notifications.
trigger - the integer value which represent the point at which event
notifications should be sent to registered listeners. Must be greater than
or equal to 0.ResourceEventpublic int getEventTrigger()
setEventTrigger(int)public void start()
PollableResource with a frequency specified by the pollingIntervalpublic void stop()
PollableResource