Initialization and clean up

The main method in MainClass is called when the agent is started. It creates a MainClass instance and then enters the long-running method to receive and handle agent requests.

Most of the initialization and clean-up code should be added to MainClass. In the constructor, add initialization that is needed to create or access your resources. You may want to open connections to remote resources, create handles, or initialize data structures.

Before the agent terminates, the stopDataCollection method is called. If you need to close connections or perform any clean up before the client ends, add that code to the stopDataCollection method.

If initialization is needed only for a particular attribute group, that initialization can be added to the constructor of the attribute group's class. Similarly, if any clean up is needed only for a particular attribute group, that clean-up code can be added to the attribute group's stopDataCollection method.

When stopDataCollection is called, if you pass the clean-up work to another thread, be sure to wait for that thread to finish (or at least give it a reasonable amount of time to finish) before returning from the stopDataCollection method. Otherwise, the clean-up work could be abruptly terminated when the process ends because the main thread has completed.

Any code in the Java client can use the logger object to write log entries. (The main helper class creates a protected logger object in its constructor, and the attribute group helper objects create a protected reference to that logger in their constructors.) The logger object uses Java's trace logging utility. Errors and detailed trace information can be obtained from the trace log that is created by the logger. This is very important for troubleshooting problems with the provider.

One of the agent configuration settings is for the Java trace level. The following table shows the values you can set in the JAVA_TRACE_LEVEL configuration property, and if the API created the logger for you, the Level used by the logger.

Table 30. Java trace level options
Configured trace level Java logging trace level Description
Off OFF No logging is performed.
Error SEVERE Trace problems that have occurred in the Java client.
Warning WARNING Trace errors and potential errors.
Information INFORMATION Trace important information about the Java client.
Minimum Debug FINE Trace high level details necessary to analyze the behavior of the Java client.
Medium Debug FINER Trace details about the program flow of the Java client.
Maximum Debug FINEST Trace all details about the Java client.
All ALL Trace all messages.

The name of the log file created by the Java client in this example is k91_trace0.log. If the agent is a multiple instance agent the instance name is included in the log file name.

Note:
Do not write messages to standard error or to standard out. On Windows systems, these messages will be lost. On UNIX and Linux systems, this data will be written to a file that does not wrap.