[SNMP4J] Table Listeners and multi-threading
Frank Fock
fock at agentpp.com
Tue Nov 16 01:37:57 CET 2004
pasteman8-agentpp at yahoo.com wrote:
>There is a basic question regarding the design of
>TableUtils:
>Can the getTable method be used in a runnable run()
>method? In otherwords, does getTable return only when
>the table is done?
>
>
>
No, the TableUtils.getTable() method available in v1.0.3 asynchronously
retrieves a table. It returns immediately. For a synchronously getTable
method, please add the following code to TableUtils class (this code
will be part of v1.0.4):
/**
* Gets synchronously SNMP tabular data from one or more tables.
* The data is returned row-by-row as a list of {@link TableEvent}
instances.
* Each instance represents a row (or an error condition). Besides the
* target agent, the OIDs of the columnar objects have to be specified
* for which instances should be retrieved. With a lower bound index and
* an upper bound index, the result set can be narrowed to improve
* performance. This method can be executed concurrently by multiple
threads.
*
* @param target
* a <code>Target</code> instance.
* @param columnOIDs
* an array of OIDs of the columnar objects whose instances should be
* retrieved. The columnar objects may belong to different tables.
* Typically they belong to tables that share a common index or
sub-index
* prefix. Note: The result of this method is not defined if
instance OIDs
* are supplied in this array!
* @param lowerBoundIndex
* an optional parameter that specifies the lower bound index.
* If not <code>null</code>, all returned rows have an index
greater than
* <code>lowerBoundIndex</code>.
* @param upperBoundIndex
* an optional parameter that specifies the upper bound index.
* If not <code>null</code>, all returned rows have an index less
or equal
* than <code>lowerBoundIndex</code>.
* @return
* a <code>List</code> of {@link TableEvent} instances. Each instance
* represents successfully retrieved row or an error condition. Error
* conditions (any status other than {@link TableEvent#STATUS_OK})
* may only appear at the last element of the list.
*/
public List getTable(Target target,
OID[] columnOIDs,
OID lowerBoundIndex,
OID upperBoundIndex) {
if ((columnOIDs == null) || (columnOIDs.length == 0)) {
throw new IllegalArgumentException("No column OIDs specified");
}
InternalTableListener listener = new InternalTableListener();
TableRequest req = new TableRequest(target, columnOIDs, listener,
null,
lowerBoundIndex,
upperBoundIndex);
synchronized (listener) {
req.sendNextChunk();
try {
listener.wait();
}
catch (InterruptedException ex) {
logger.warn(ex);
}
}
return listener.getRows();
}
class InternalTableListener implements TableListener {
private LinkedList rows = new LinkedList();
public boolean next(TableEvent event) {
rows.add(event);
return true;
}
public synchronized void finished(TableEvent event) {
if ((event.getStatus() != TableEvent.STATUS_OK) ||
(event.getIndex() != null)) {
rows.add(event);
}
notify();
}
public List getRows() {
return rows;
}
}
Best regards,
Frank
More information about the SNMP4J
mailing list