Class DistributedApplication

java.lang.Object
com.azure.runtime.host.DistributedApplication

public class DistributedApplication extends Object
Represents the core of a distributed application within the Java Runtime for Azure framework. This class serves as the point for configuring and managing the various components and extensions that make up a distributed application.

The DistributedApplication class provides a fluent API for adding resources such as Docker containers, executables, and values to the application. It also supports loading and configuring extensions that enhance the application's capabilities beyond its core functionalities.

Instances of this class are typically created and configured within an AppHost implementation. The DistributedApplication is designed to be a singleton within the context of an application's lifecycle, ensuring a centralized configuration point. Usage example:

public class MyAppHost implements AppHost {

    public static void main(String[] args) {
        new MyAppHost().boot(args);
    }

    @Override public void configureApplication(DistributedApplication app) {
        app.printExtensions();
        // ... the rest of the configuration
    }
}
See Also:
  • Method Details

    • getInstance

      public static DistributedApplication getInstance()
    • addResource

      public <T extends Resource<?>> T addResource(T r)
      Adds a new resource to the distributed application. This method allows for the addition of various types of resources that are essential for the application's operation, such as Docker files, containers, executables, and values. Each resource added through this method is registered within the application's manifest, making it a part of the application's configuration.

      Usage example:

       import com.azure.runtime.host.resources.DockerFile;DockerFile<?> myDockerFile = new DockerFile<>("MyDockerFile", "./Dockerfile", ".");
       DockerFile<?> addedDockerFile = app.addResource(myDockerFile);
      
      Type Parameters:
      T - The type of the resource being added, extending the Resource class.
      Parameters:
      r - The resource to add to the application.
      Returns:
      The added resource, allowing for further configuration or chaining of operations.
    • substituteResource

      public void substituteResource(Resource<?> oldResource, Resource<?>... newResources)
      Substitutes an existing resource with one or more new resources. This method is useful in scenarios where a resource's configuration needs to be dynamically altered based on runtime conditions or other factors. Instead of mutating the existing resource, which could lead to inconsistencies, this method ensures a clean replacement by removing the old resource and adding the new ones.

      Usage example:

      app.substituteResource(oldResource, newResource1, newResource2);
      
      Parameters:
      oldResource - The resource to remove.
      newResources - The resource(s) to add in the place of the old resource.
    • addDockerFile

      public <T extends DockerFile<?>> T addDockerFile(T dockerFile)
      Adds a new DockerFile resource to the distributed application. DockerFiles are essential for defining the environment in which containers will run. This method simplifies the process of adding DockerFiles to the application's configuration.

      Usage example:

       import com.azure.runtime.host.resources.DockerFile;DockerFile<?> dockerFile = app.addDockerFile("myDockerFile", "./Dockerfile", ".");
      
      Type Parameters:
      T - The type of the DockerFile.
      Parameters:
      dockerFile - The DockerFile to add.
      Returns:
      The added DockerFile.
    • addDockerFile

      public DockerFile<?> addDockerFile(String name, String path, String context)
      Adds a new DockerFile resource to the distributed application using the specified parameters. This overload provides a convenient way to add a DockerFile without creating an instance beforehand.

      Usage example:

       import com.azure.runtime.host.resources.DockerFile;DockerFile<?> dockerFile = app.addDockerFile("myDockerFile", "./Dockerfile", ".");
      
      Parameters:
      name - The name of the DockerFile.
      path - The path to the DockerFile.
      context - The build context.
      Returns:
      The added DockerFile.
    • addContainer

      public <T extends Container<?>> T addContainer(T container)
      Adds a new container resource to the distributed application. Containers are fundamental to the deployment and execution of applications within a distributed system. This method facilitates the addition of container configurations to the application.

      Usage example:

       import com.azure.runtime.host.resources.Container;Container<?> container = app.addContainer("myContainer", "nginx:latest");
      
      Type Parameters:
      T - The type of the container.
      Parameters:
      container - The container to add.
      Returns:
      The added container.
    • addContainer

      public Container<?> addContainer(String name, String image)
      Adds a new container resource to the distributed application using the specified parameters. This method offers a straightforward way to add a container by specifying its name and image directly.

      Usage example:

       import com.azure.runtime.host.resources.Container;Container<?> container = app.addContainer("myContainer", "nginx:latest");
      
      Parameters:
      name - The name of the container.
      image - The Docker image for the container.
      Returns:
      The added container.
    • addExecutable

      public <T extends Executable<?>> T addExecutable(T executable)
      Adds a new executable resource to the distributed application. Executables represent command-line applications or scripts that are part of the application's operational requirements. This method allows for the inclusion of such executables in the application's configuration.

      Usage example:

       import com.azure.runtime.host.resources.Executable;Executable<?> executable = app.addExecutable("myExecutable", "echo", "/usr/bin", "Hello, World!");
      
      Type Parameters:
      T - The type of the executable.
      Parameters:
      executable - The executable to add.
      Returns:
      The added executable.
    • addExecutable

      public Executable<?> addExecutable(String name, String command, String workingDirectory, String... args)
      Adds a new executable resource to the distributed application using the specified parameters. This convenience method simplifies the process of adding executables by directly specifying their properties.

      Usage example:

       import com.azure.runtime.host.resources.Executable;Executable<?> executable = app.addExecutable("myExecutable", "echo", "/usr/bin", "Hello, World!");
      
      Parameters:
      name - The name of the executable.
      command - The command to execute.
      workingDirectory - The working directory for the executable.
      args - The arguments to pass to the executable.
      Returns:
      The added executable.
    • addValue

      public <T extends Value<?>> T addValue(T value)
      Adds a new value resource to the distributed application. Values are key-value pairs that can be used for configuration purposes or as parameters for other resources. This method enables the addition of such values to the application's configuration.

      Usage example:

       import com.azure.runtime.host.resources.Value;Value<?> value = app.addValue("myValue", "key", "value");
      
      Type Parameters:
      T - The type of the value.
      Parameters:
      value - The value to add.
      Returns:
      The added value.
    • addValue

      public Value<?> addValue(String name, String key, String value)
      Adds a new value resource to the distributed application using the specified parameters. This method provides a direct way to add a key-value pair to the application's configuration.

      Usage example:

       import com.azure.runtime.host.resources.Value;Value<?> value = app.addValue("myValue", "key", "value");
      
      Parameters:
      name - The name of the value.
      key - The key of the value.
      value - The value associated with the key.
      Returns:
      The added value.
    • printExtensions

      public void printExtensions()
      Prints the available extensions to the standard output. This method is useful for debugging and configuration purposes, allowing developers to see which extensions are available for use within the application.

      Usage example:

      app.printExtensions();
      
    • printExtensions

      public void printExtensions(PrintStream out)
      Prints the available extensions to a specified PrintStream. This method offers flexibility in directing the output to different destinations, facilitating integration with logging frameworks or custom output handling.

      Usage example:

      app.printExtensions(System.err);
      
      Parameters:
      out - The PrintStream to print the extensions to.
    • withExtension

      public <T extends Extension> T withExtension(Class<T> extension)
      Loads the specified extension and makes an instance of it available for configuration, but it does not add the extension to the distributed application. This will happen when the extension is configured. This method is key to the extensibility of the Java Runtime for Azure framework, allowing developers to dynamically add and configure extensions that enhance the application's functionality.

      Usage example:

      SpringExtension springExtension = app.withExtension(SpringExtension.class);
      
      Type Parameters:
      T - The type of the extension.
      Parameters:
      extension - The class of the extension to load.
      Returns:
      An instance of the specified extension class, ready for configuration.
      Throws:
      RuntimeException - If the extension class cannot be instantiated.