To use the Concurrent Specimen Query Engine, download the
prerequisite jars, instantiate an Engine, create a
QueryListener, add the listener to the Engine, then run a
query. The Engine will notify the QueryListener as QueryEvents occur.
With the dependent jars installed in your build/run
environment, you can execute a query. First, instantiate the
engine:
import gov.nasa.jpl.oodt.erne.query.Engine;
...
Engine engine = new Engine();
This
Engine
can be used for any number of
queries. Next, create a class that implements the
QueryListener
interface. For this example, we'll
just print the SPECIMEN_COLLECTED_CODE for each specimen at each site:
import gov.nasa.jpl.oodt.erne.query.QueryListener;
import gov.nasa.jpl.oodt.erne.query.QueryEvent;
...
class SpecimenCollectedCode implements QueryListener {
public void rowReceived(QueryEvent e) {
String site = e.getSiteName();
List columns = e.getColumns();
String code = (String) columns.get(0);
System.out.println(site + ": " + code);
}
public void errorOccurred(QueryEvent e) {
String site = e.getSiteName();
Throwable error = e.getError();
System.err.println(site + ": " + error);
}
}
...
SpecimenCollectedCode x = new SpecimenCollectedCode();
engine.addQueryListener(x);
Note that listeners must implement two methods:
rowReceived
for each row of data received from
product servers, and
errorOccurred
, which is
invoked by the engine if an error occurs at a sp
ecific site.
The Engine calls the
rowReceived
method once
for each row received from each site. The calls are
serialized so you don't have to worry about thread safety in
your listener class; only one
rowReceived
call
is ever active per Engine. The rows come in order from each
site, but
rowReceived
may interleave calls from
multiple sites.
The
QueryEvent
encapsulates the event. For
each row received,
getSiteName
tells from which
site the row came, and
getColumns
gives each
column's value in the current row in order. For errors,
getSiteName
tells which site suffered the
error, and
getError
returns the exception
object.
Finally, to start the queries, create a
List
of
String
s containing the object name of each
site to query, and an
XMLQuery
object
representing the query to send:
import jpl.eda.xmlquery.XMLQuery;
...
List sites = new ArrayList();
sites.add("urn:eda:rmi:NIH.NCI.EDRN.BRIGHAM.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.COLORADO.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.CREIGHTON.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.DARTMOUTH.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.MDANDERSON.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.MOFFITT.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.PITTSBURGH.PRODUCT_SERVER");
sites.add("urn:eda:rmi:NIH.NCI.EDRN.SANANTONIO.PRODUCT_SERVER");
XMLQuery q = new XMLQuery("RETURN = SPECIMEN_COLLECTED_CODE",
null, null, null, null, null, null, null, 999);
engine.run(sites, q);
The Engine will call your registered listeners as rows are
recieved or errors occur. The call to
Engine.run
won't return until all the sites
have responded. You can call
Engine.run
on the
same
Engine
object from another thread; it is
internally thread-safe.