Discovering databases
If your application needs to find available databases, implement the following steps:
1. Given a Datasource event, call the event’s getDatabases action.
2. Create a handler action to perform callback actions on the results of the getDatabases action.
3. In the handler action, declare a variable for a Database event.
The definitions for the two forms of the getDatabases action are:
action getDatabases(
string serviceId,
string user,
string password,
action< string, sequence<Database> > callback)
and
action getDatabasesFull(
string serviceId,
string locationURL,
string user,
string password,
dictionary<string,string> extraParams,
action < string, sequence<Database> > callback)
Note: JDBC data sources will usually require user and password values.
The definition of the Database event is:
event Database
{
string shortName;
string dbUrl;
string description;
dictionary<string,string> extraParams;
}
shortName — A short display name
dbUrl — The complete URL of the database, for example,
jdbc:sqlserver://localhost/ApamaTest.
extraParams — Optional parameters.
The relevant code in the samples\adbc\api-example\ADBC_Example.mon file is similar to this:
action handleAvailableServers(string error,
sequence<com.apama.database.DataSource> results)
{
if error.length() != 0 then {
log "Error occurred getting available data sources: " +
error at ERROR;
}
else {
if results.size() > 0 then {
// Save off first service ID found.
// Assumes first data source has at least one db
if getDbServiceId() = "" then {
dbServiceId := results[0].serviceId;
}
com.apama.database.DataSource ds;
log " DataSources: " at INFO;
for ds in results {
log " " + ds.name + " - " + ds.serviceId at INFO;
}
log "Finding Databases ..." at INFO;
for ds in results {
adbc.getDatabases(ds.serviceId, USER, PASSWORD,
handleAvailableDatabases);
}
}
else {
log " No DataSources found" at INFO;
}
}
}
string dbName;
action handleAvailableDatabases(string error,
sequence<com.apama.database.Database> results)
{
if error.length() != 0 then {
log "Error occurred getting available databases: " +
error at ERROR;
}
else {
if results.size() > 0 then {
// Save name of first db found
if getDbName() = "" then {
dbName := results[0].shortName;
}
com.apama.database.Database db;
log " Databases: ";
for db in results {
log " " + db.shortName + " - " +
db.description + " - " + db.dbUrl at INFO;
}
// ... other logic...
}
else {
log " No Databases found" at INFO;
}
}
}