Run Deephaven from your own Java application
The Deephaven server is a Java library. The production application and the Docker images are prebuilt applications that start it, but any JVM can start a Deephaven server. This guide shows you how to build your own Java application that starts a Deephaven server, so that you control the classpath, the startup code, and which server components are included.
Build a custom application when you want to:
- Package Deephaven together with your own Java code and dependencies in a single application.
- Create static and ticking tables in Java when the server starts, and publish them to clients and the web UI.
- Replace or remove server components, such as the authorization rules.
If you only need to add JARs to a prebuilt Deephaven server, such as the production application or a Docker image, you don't need a custom application. See Install and use Java packages instead.
Prerequisites
- Java 17.0.5 or later. Deephaven's web server, Jetty 12, requires Java 17. Earlier Java 17 releases have a JVM bug (JDK-8287432), so by default the server refuses to start on them. If you can't upgrade, the startup error names a JVM workaround.
- A build tool that resolves Maven artifacts, such as Gradle or Maven. The examples in this guide use Gradle.
- Python 3.10 or later, for the server's Python console. See Set up the Python environment.
- Familiarity with Dagger, the dependency injection framework that assembles the Deephaven server. You need only the basics: components, modules, and the
@Provides,@Binds, and@IntoSetannotations.
How a Deephaven server starts
The production application's entry point, io.deephaven.server.jetty.JettyMain, is short. A custom application follows the same three steps:
- Call
MainHelper.init. It initializes logging, loads the Deephaven configuration, and installs process-wide handlers for shutdown and uncaught errors. Call it before anything else touches Deephaven classes. - Build a Dagger component. The component wires together every server service: the gRPC and Arrow Flight APIs, the web UI, authentication, the console, and Application Mode. A component factory, a subclass of
ComponentFactoryBase, builds the component from the configuration. - Get the server from the component and call
run, which starts the server and returns once it is listening. Calljointo block until the server shuts down.
The production application's component factory, CommunityComponentFactory, belongs to the server-jetty-app project, which isn't published to Maven Central. A custom application defines its own component and factory using the modules in the published deephaven-server-jetty artifact. The rest of this guide walks through how to do that.
Tip
The deephaven-core repository contains a complete example of a custom application in server/jetty-app-custom. It also replaces the authorization provider. If you have a local clone, run it with ./gradlew server-jetty-app-custom:run. Without the -Pgroovy flag, the example starts a Python console, which needs an active virtual environment with the locally built deephaven-core wheel, as described in Build and install the wheel.
Set up the build
The build asks for a Java 17 toolchain. If no Java 17 installation is on your machine, Gradle downloads one, but only if a toolchain download repository is configured. Create a project directory, and add a settings.gradle file at its root with the Foojay toolchain resolver plugin:
Without the plugin, a machine with no Java 17 installation fails the build with Cannot find a Java installation on your machine ... matching: {languageVersion=17 ...}.
The following build.gradle file creates an application named my-deephaven-app. Replace <version> with the Deephaven version you want, such as the latest release without its leading v. Replace <dagger-version> and <logback-version> as described in the list after the file.
Keep the following in mind:
- The Confluent repository is required.
deephaven-server-jettydepends on Deephaven's Kafka integration at runtime, and some of its dependencies are published only to Confluent's Maven repository. Without that repository, dependency resolution fails with errors such asCould not find org.apache.kafka:kafka-clients. - Match the Dagger version to Deephaven's. Use the Dagger version listed as a dependency in the
deephaven-serverPOM for your Deephaven version. Deephaven's published JARs contain Dagger-generated code that calls into Dagger's runtime (dagger.internal.*) classes, so a mismatched Dagger version can fail at runtime with a linkage error such asNoSuchMethodError. - Include the JSON extension for the Python console. The first time a Python API call such as
resolvewraps a Java object, thedeephavenpackage imports all of its modules, and one of them needs Deephaven's JSON classes. Withoutdeephaven-extensions-json-jackson, that call fails withValueError: Java class 'io.deephaven.json.Value' not found. - Pick a Logback version. Use the Logback version that Deephaven builds and tests against: look up the
logbackentry ingradle/libs.versions.tomlat the tag for your Deephaven version (for example,v<version>). For the Logback configuration file, see Configure logging. - Pass the JVM arguments that apply to your build. The first four are the same arguments that the production application's
startscript passes. If you launch the application another way, such as from your IDE or a container, pass them there too.--add-opens java.base/java.nio=ALL-UNNAMEDis always required, because Apache Arrow needs it.--add-exports java.management/sun.management=ALL-UNNAMEDis required only if you includedeephaven-hotspot-impl.--add-exports java.base/jdk.internal.misc=ALL-UNNAMEDis required only if you includedeephaven-clock-impl.-Dio.netty.noUnsafe=falseis required on Java 25 and later, and harmless on earlier versions.-Ddeephaven.application=my-deephaven-appsets the application name used for the data, cache, and config directories; see Configure the server for what it controls and why you should set it.
- The console is Python by default. The build sets no
deephaven.console.type, so the server starts a Python console, which needs the Python environment described in Set up the Python environment. Settingdeephaven.console.typetogroovygives you a Groovy console instead, with no Python setup. Setting it tononedisables the console, but only if the component also includesNoConsoleSessionModule; see Write the component factory.
Deephaven's optional integrations are separate artifacts. Add the ones you use as runtime dependencies. For example, the production application also includes deephaven-engine-sql, deephaven-extensions-s3, and deephaven-extensions-iceberg-s3.
Flight SQL needs an extra step, because its artifact doesn't register the service with the server by itself. Add deephaven-extensions-flight-sql as an implementation dependency, not runtimeOnly, so that your code can reference FlightSqlModule. Then include that module in your Dagger component, as described in Write the component factory.
Write the component factory
The component factory builds the Dagger component. Dagger generates the component's implementation at compile time, naming the generated class based on the interface's nesting: MyComponentFactory.MyComponent becomes DaggerMyComponentFactory_MyComponent.
Put each class in this guide in its own file under src/main/java/com/example/, for example src/main/java/com/example/MyComponentFactory.java.
MyModule also registers MyApplication, the class that publishes tables when the server starts. You write it in Publish tables at startup.
The included modules supply the server:
| Module | Provides |
|---|---|
JettyServerModule | The Jetty web server, which serves the gRPC APIs and the web UI |
CommunityDefaultsModule | The standard set of Deephaven services, such as sessions, tables, consoles, plugins, and Application Mode |
CommunityAuthorizationModule | The default authorization provider, which lets every authenticated user do everything |
TicketResolversFromServiceLoader | Ticket resolvers that plugins on the classpath register through Java's ServiceLoader |
ClientChannelFactoryModule | Outgoing connections from this server to other Deephaven servers |
SslConfigModule | The TLS configuration for those outgoing connections |
ClientChannelFactoryModule requires the @UserAgent string that MyModule provides.
Warning
CommunityAuthorizationModule lets every authenticated user do everything on the server. It binds CommunityAuthorizationProvider, which allows all operations in every service it covers, including consoles, tables, input tables, storage, partitioned and hierarchical tables, and applications. Authentication controls only who can connect. To restrict what users can do, replace CommunityAuthorizationModule with your own AuthorizationProvider binding, as the next paragraph describes.
To change a part of the server, replace the module that provides it. For example, the jetty-app-custom example leaves out CommunityAuthorizationModule and binds its own AuthorizationProvider, which disables the input table service. Don't include both: Dagger rejects two bindings for the same type.
To add an optional integration that registers its own services, add its module to the @Module(includes = {...}) list:
- For Flight SQL, include
io.deephaven.server.flightsql.FlightSqlModulefromdeephaven-extensions-flight-sql. - To support
-Ddeephaven.console.type=none, includeio.deephaven.server.console.NoConsoleSessionModulefrom thedeephaven-serverartifact.CommunityDefaultsModuleregisters only the Python and Groovy consoles.
Publish tables at startup
An ApplicationState.Factory creates objects when the server starts and exposes them to clients by name. It is the Java equivalent of an Application Mode script. The factory runs with the server's execution context already open.
Two details matter here:
- Keep the tables alive. Deephaven releases tables that nothing references. Creating the tables inside a liveness scope that the factory holds in a field keeps them alive for as long as the server runs.
- Lock the update graph when you create a refreshing table. Calling
updateon a time table requires the update graph's shared lock; without it, the server fails to start withIllegalStateException: May not initiate serial table operations. Create every refreshing table inside the locked block, and keep the block short, because update cycles wait while you hold the lock. Static tables, such as one fromemptyTable, don't need the lock.
The application's ID is the second constructor argument (after the listener), here com.example.MyApplication. Clients fetch the fields with an application ticket made from the ID and the field name.
Write the main class
The main class follows the three steps from How a Deephaven server starts:
Because run doesn't block, your application can do other work after the server starts, then call join when it has nothing left to do.
MainHelper.init accepts an optional single argument: the path of a properties file whose entries are loaded as system properties before anything else.
Configure logging
Deephaven logs through SLF4J. The build above uses Logback. Add a src/main/resources/logback.xml file that sends logs to the console and to the log buffer that the web UI's Log panel reads:
The LevelChangePropagator listener copies Logback's log levels to Java's built-in logging. Libraries that log through it, such as gRPC, then skip messages that Logback would discard instead of building them first.
Set up the Python environment
The Python console runs inside the JVM through jpy, so the JVM must find a Python interpreter that has the deephaven-core package installed. That package provides the Python side of the console and installs jpy. It requires Python 3.10 or later.
Create a virtual environment and install the deephaven-core version that matches deephavenVersion in your build:
The [autocomplete] suffix installs the optional packages that add code completion to the console.
Unless the configuration sets the explicit jpy.* properties, the server finds the interpreter for its Python console in this order:
- The interpreter named by the
deephaven.python.nameJVM system property, for example-Ddeephaven.python.name=/path/to/my-deephaven-venv/bin/python. Add it toapplicationDefaultJvmArgsinbuild.gradle, which both./gradlew runand the launch script use, or set it inJAVA_OPTSwhen you start the launch script. It must be a system property; an entry indeephaven.prophas no effect. bin/pythonin the directory named by theVIRTUAL_ENVenvironment variable. Activating the virtual environment sets this variable.python3on thePATH.
The simplest way to make this interpreter available is to start the application from a shell where the virtual environment is active, as the production application does. If the application starts some other way, such as from an IDE or a service manager, set the deephaven.python.name system property instead.
The JVM and the Python interpreter must have the same CPU architecture. For example, on a Mac with Apple silicon, an arm64 JVM can't load an Intel (x86_64) Python installation, and the server fails to start with Failed to load Python shared library.
Run the application
These commands use the Gradle wrapper. If your project doesn't have one, run gradle wrapper once, or use gradle in place of ./gradlew.
Run the application from a shell where the virtual environment is active. Start it with Gradle:
Or build a distribution with a launch script, and run that:
The generated launch script includes the JVM arguments from applicationDefaultJvmArgs and adds anything in the JAVA_OPTS environment variable.
When the server is ready, the log shows Server started on port 10000. By default, the server uses pre-shared key authentication with a generated key, and logs a URL that includes the key:
The generated key comes from java.util.Random, which isn't cryptographically secure. Unless you set http.host, the server listens on all network interfaces, so other machines can reach it as soon as it starts. Set your own strong key with -Dauthentication.psk=<key>, as described in Configure the server, or choose another authentication handler. To accept only local connections, set -Dhttp.host=localhost.
Warning
The pre-shared key is a superuser credential: anyone who presents it is authenticated as a superuser. The log line above, including the key and the clickable URL, goes to both stdout and the log buffer that the web UI's Log panel reads from (see Configure logging). Restrict access to these logs, whether on disk, in a collection system, or in the running server's own UI, to avoid leaking superuser access.
Open that URL to use the web UI. The Python console works as it does in any Deephaven server, and the Panels menu lists the ticking and staticTable fields from MyApplication.
The application's fields aren't variables in the console. To use them in Python code, resolve them by their application URI, which combines the application ID and the field name:
Configure the server
A custom application reads the same configuration as the production application:
- Configuration properties, set as JVM system properties or in a configuration file. For example,
-Dhttp.port=8080changes the port, and-Dauthentication.psk=<key>sets the pre-shared key. See Configuration properties for more. - The bootstrap settings for the application name and the data, cache, and config directories, described in Configure the production application. The example build sets
deephaven.applicationtomy-deephaven-app. Without it, the application name defaults todeephaven, so your application shares its data, cache, and config directories, including anydeephaven.propfile, with other Deephaven servers on the same machine. - The authentication handlers, chosen with the
AuthHandlersproperty. See Available authentication handlers for the built-in options, and the authentication guides for how to configure each one.
For example, to set the key and port when using the launch script:
./gradlew run doesn't read JAVA_OPTS, and -D flags on its command line set properties for Gradle, not for the server. To set properties for ./gradlew run, add them to applicationDefaultJvmArgs in build.gradle.
Related documentation
- Liveness scope concept
- Execution Context
- Install and run the Deephaven production application
- Build and run Deephaven from source code
- Configure the production application
- Configure the Deephaven Docker application
- Deephaven configuration files
- Configuration properties
- Application Mode
- Use Application Mode scripts
- Pre-shared key authentication
- Install and use Java packages
- Use jpy
- Use URIs to share tables
- Connecting to a Deephaven Flight SQL server
- Kafka overview
- Arrow Flight and Deephaven
- What is an UpdateGraphConflictException and how do I fix it?
resolveApplicationState.FactoryJavadocAuthorizationProviderJavadocClientChannelFactoryModuleJavadocCommunityAuthorizationModuleJavadocCommunityAuthorizationProviderJavadocCommunityDefaultsModuleJavadocComponentFactoryBaseJavadocDeephavenApiServerJavadocFlightSqlModuleJavadocJettyServerModuleJavadocMainHelperJavadocNoConsoleSessionModuleJavadocSslConfigModuleJavadocTableOperationsJavadocTableToolsJavadocTicketResolversFromServiceLoaderJavadoc