Use Application Mode libraries
This guide shows you how to use the ApplicationContext and ApplicationState classes from the io.deephaven.appmode package in Application Mode scripts. These classes give you more control over Deephaven's state during launch.
ApplicationContext provides access to the ApplicationState used during the initial startup of a Deephaven server that uses Application Mode.
Use ApplicationContext.get to read the ApplicationState, or ApplicationContext.initialize to pass it to a closure you supply. Use initialize for scripts split across multiple Groovy files; the file_N properties in the Application Mode config file list those files. Both work only during startup; see When the application state is available.
ApplicationState publishes tables and other objects as application fields, which the web IDE and API clients can open. A field is a named object, such as a table, that the server publishes to clients. For more on fields, see Initialize server state with Application Mode.
Example
The following example uses ApplicationContext and ApplicationState to create tables within Deephaven through Application Mode.
When the application state is available
ApplicationContext.get and ApplicationContext.initialize work only while the files of an Application Mode script run during startup. Calling either one at any other time throws an IllegalStateException.
Deephaven runs every file listed in the file_N properties under the same ApplicationContext, so each file gets the same ApplicationState. If a script needs to add fields later, it must keep a reference to the ApplicationState.
Add or replace a field
Call setField(name, value) to add a field. If a field with that name already exists, the call replaces it.
To attach a description to a field, pass it as a third argument. Clients can read the description. Pass a non-empty description; an empty string throws IllegalArgumentException.
Note
Fields set on ApplicationState appear in the web IDE's Panels menu and are available to API clients, but they are not added to the query scope. To refer to one of these tables by name in console code, also add it to the query scope in the script, for example by assigning it to a global variable.
Read a field
Call getField(name) to read a field. It returns a Field object, or null if no field has that name. Check the result for null before you call value on it. The Field object holds the field's name, value, and description (see List fields).
List fields
Call listFields to get every field as a list of Field objects. Each Field has name, value, and description methods. description returns an Optional<String>, so the example uses orElse for fields without one.
Remove a field
Call removeField(name) to remove a field. If no field has that name, the call does nothing.