Ray runtime implementation for Java - the core distributed runtime component of Ray framework for scaling AI and Python applications
—
Core runtime lifecycle management including initialization, shutdown, and access to runtime context information.
Initialize the Ray runtime with default or custom configuration.
/**
* Initialize Ray runtime with the default runtime implementation.
* Must be called before any other Ray operations.
*/
public static void init();
/**
* Check if Ray has been initialized.
* @return True if init() has been called, false otherwise
*/
public static boolean isInitialized();Usage Example:
import io.ray.api.Ray;
// Initialize Ray runtime
Ray.init();
// Check initialization status
if (Ray.isInitialized()) {
// Perform Ray operations
ObjectRef<String> ref = Ray.put("Hello Ray!");
}Cleanly shutdown the Ray runtime and release resources.
/**
* Shutdown Ray runtime and release all resources.
* Should be called when Ray operations are complete.
*/
public static void shutdown();Usage Example:
try {
Ray.init();
// Perform Ray operations...
} finally {
// Always shutdown Ray when done
Ray.shutdown();
}Access runtime information including job IDs, node information, and actor context.
/**
* Get the runtime context for accessing runtime information.
* @return RuntimeContext instance
*/
public static RuntimeContext getRuntimeContext();
public interface RuntimeContext {
/** Get current job ID */
JobId getCurrentJobId();
/** Get current task ID */
TaskId getCurrentTaskId();
/** Get current actor ID (null if not in actor) */
ActorId getCurrentActorId();
/** Check if current actor was restarted */
boolean wasCurrentActorRestarted();
/** Check if running in local mode */
boolean isLocalMode();
/** Get information about all cluster nodes */
List<NodeInfo> getAllNodeInfo();
/** Get information about all actors */
List<ActorInfo> getAllActorInfo();
/** Get current actor handle (null if not in actor) */
BaseActorHandle getCurrentActorHandle();
/** Get GPU IDs available to current process */
List<Integer> getGpuIds();
/** Get current namespace */
String getNamespace();
/** Get current node ID */
String getCurrentNodeId();
/** Get current runtime environment */
RuntimeEnv getCurrentRuntimeEnv();
}Usage Example:
RuntimeContext context = Ray.getRuntimeContext();
// Get current job information
JobId jobId = context.getCurrentJobId();
System.out.println("Current job: " + jobId);
// Check if running in actor
ActorId actorId = context.getCurrentActorId();
if (actorId != null) {
System.out.println("Running in actor: " + actorId);
// Check if actor was restarted
if (context.wasCurrentActorRestarted()) {
System.out.println("Actor was restarted - handle recovery logic");
}
}
// Get cluster information
List<NodeInfo> nodes = context.getAllNodeInfo();
System.out.println("Cluster has " + nodes.size() + " nodes");
// Get GPU information
List<Integer> gpus = context.getGpuIds();
if (!gpus.isEmpty()) {
System.out.println("Available GPUs: " + gpus);
}Intentionally exit the current actor process.
/**
* Intentionally exit the current actor.
* Can only be called from within an actor.
* @throws RuntimeException if not called from within an actor
*/
public static void exitActor();Usage Example:
public class MyActor {
private boolean shouldExit = false;
public void processData(String data) {
// Process data...
if (shouldExit) {
// Clean shutdown of actor
Ray.exitActor();
}
}
public void shutdown() {
shouldExit = true;
}
}public class NodeInfo {
/** Get node ID */
public String getNodeId();
/** Check if node is alive */
public boolean isAlive();
/** Get node resources */
public Map<String, Double> getResources();
}public class ActorInfo {
/** Get actor ID */
public ActorId getActorId();
/** Get actor class name */
public String getClassName();
/** Get actor state */
public ActorState getState();
/** Get node ID where actor is running */
public String getNodeId();
}
public enum ActorState {
DEPENDENCIES_UNREADY,
PENDING_CREATION,
ALIVE,
RESTARTING,
DEAD
}public interface RuntimeEnv {
/** Get runtime environment configuration */
RuntimeEnvConfig getConfig();
}
public class RuntimeEnvConfig {
/** Get working directory */
public String getWorkingDir();
/** Get environment variables */
public Map<String, String> getEnvVars();
/** Get Python dependencies */
public List<String> getPipPackages();
/** Get Conda dependencies */
public String getCondaEnv();
}Install with Tessl CLI
npx tessl i tessl/maven-io-ray--ray-runtime