Kora Framework

repository·master·Indexed 41 days ago

https://github.com/kora-projects/kora

A high-performance, compile-time JVM framework for Java and Kotlin that replaces runtime reflection and dynamic proxies with generated source code. Optimized for cloud-native services and AI-assisted development, Kora provides a full-stack ecosystem including HTTP servers, SQL-first data repositories, messaging (Kafka, gRPC), and observability following the OpenTelemetry standard.

Tokens
4.2K
Snippets
11
Records
20
Agent score
43%

What's inside Kora

  1. Overview of Kora modules and capabilities

    master

    Kora is a modular framework that provides high-performance implementations for various service tasks. You should only include the modules required for your specific service to keep it lightweight.

    Key modules include:

    • HTTP: Includes an HTTP server, a declarative HTTP client, request mapping, interceptors, and service endpoints. Supports strictly typed code generation from OpenAPI and a SOAP client.
    • Data: SQL-first repositories with compile-time @Query validation. Supports JDBC, R2DBC, Vert.x, and Cassandra. Includes column macros, generated converters, batch queries, and migrations.
    • Messaging & RPC: Kafka consumers/producers, gRPC server and client, and an S3 client.
    • Aspects: Compile-time generation for resilience (@Retry, @Timeout, @CircuitBreaker, @Fallback), caching (Caffeine / Redis), validation, and scheduling (no runtime proxies).
    • Core: Compile-time dependency injection, typed configuration (HOCON / YAML), reflection-free JSON, and concurrency using virtual threads.
    • Observability: Built-in support for metrics, tracing, structured logging, and probes following the OpenTelemetry standard.
  2. What is the Kora Framework?

    master

    Kora is a full-stack, cloud-oriented server framework for Java and Kotlin. It uses an annotation processor (or KSP for Kotlin) to transform high-level declarative code—such as controllers, repositories, and configuration—into ordinary, readable Java/Kotlin source code at compile time.

    Key characteristics include:

    • No Runtime Magic: It avoids runtime reflection, dynamic proxies, and classpath scanning, resulting in faster startup and higher performance.
    • Compile-Time Safety: Dependency graph issues, ambiguity, and even incorrect SQL placeholders are caught during the build process rather than at runtime.
    • Transparency: The framework generates human-readable source code (e.g., ApplicationGraph) that you can inspect to understand exactly how components are wired.
  3. What is Kora Framework

    master

    Kora is a cloud-oriented server-side framework for Java and Kotlin designed for simplicity, transparency, and high performance. Unlike traditional frameworks that rely on runtime reflection, dynamic proxies, or classpath scanning, Kora uses annotation processing (Java) or KSP (Kotlin) to generate plain, readable Java/Kotlin code during the compilation phase.

    Key characteristics include:

    • Compile-time safety: Dependency graphs, HTTP routing, and repository implementations are validated during compilation. Errors like missing dependencies or invalid SQL placeholders result in compiler errors rather than runtime exceptions.
    • High Performance: Since code is generated upfront, there is no Reflection API overhead at runtime, leading to TechEmpower-level performance.
    • Fast Startup: The dependency container is built at compile-time, allowing services to start in seconds, which is ideal for horizontal scaling and rolling deployments.
    • Transparency: The generated code is human-readable and can be inspected or debugged using standard tools.
  4. The Kora Mental Model: Declare → Compile → Run

    master

    Kora operates on a three-step lifecycle that shifts complexity from runtime to compile-time:

    1. Declare: You define your application's structure (controllers, repositories, listeners, configuration, and fault-tolerance policies) using standard Java/Kotlin idioms, constructors, interfaces, and specific annotations like @KoraApp, @Component, @Module, and @Tag.
    2. Compile: The Kora annotation processor (or KSP for Kotlin) validates the entire dependency graph. It checks for missing or ambiguous dependencies, dependency cycles, and even invalid SQL in @Query annotations. If issues are found, the build fails.
    3. Run: The application executes the generated source code. There is no reflection, no dynamic proxies, and no classpath scanning at runtime. This results in a predictable, debuggable, and high-performance service.
  5. Handle inline versions in `libs.versions.toml`

    master

    The updater supports inline versions in the catalog, formatted as:

    logback-classic = { module = "ch.qos.logback:logback-classic", version = "1.5.18" }

    Important Considerations:

    • Reporting: Inline dependencies are reported as inline:<lineNumber> because they lack a stable version key.
    • Policy: While you can apply policies to inline versions using quoted keys (e.g., "inline:42" = "patch"), this is fragile because line numbers change when the file is edited.
    • Best Practice: If an inline dependency requires a permanent version policy, move its version into the [versions] section and use a version.ref in the [libraries] section.
    # Recommended way to handle dependencies requiring policies
    [versions]
    logback = "1.5.18"
    
    [libraries]
    logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
    
    [version-levels]
    logback = "patch"
  6. Synchronize `buildSrc/build.gradle` with the version catalog

    master

    Because buildSrc is a separate build, it does not automatically share the main build's version catalog accessors. The updater scans buildSrc/build.gradle for literal Maven coordinates (e.g., implementation 'group:artifact:version').

    If a coordinate in buildSrc/build.gradle matches a group:artifact in libs.versions.toml, the updater will:

    1. Report the change in a dry run.
    2. Rewrite the coordinate in buildSrc/build.gradle to match the catalog version when -PwriteVersions=true is used.

    Note: The updater only synchronizes exact coordinates. It will not migrate a dependency to a different Maven group/artifact automatically.

  7. Configure Flyway for scheduling-db migrations

    master

    If you use Flyway, you can point it to the following classpath locations depending on your database dialect:

    • PostgreSQL: classpath:db/scheduling-db/flyway/postgresql
    • MySQL: classpath:db/scheduling-db/flyway/mysql
    • MariaDB: classpath:db/scheduling-db/flyway/mariadb
    • MS SQL Server: classpath:db/scheduling-db/flyway/mssql
    • Oracle: classpath:db/scheduling-db/flyway/oracle
    • HSQLDB: classpath:db/scheduling-db/flyway/hsql
  8. Test application graphs with @KoraAppTest

    master

    Use the @KoraAppTest annotation to spin up a real application graph for testing. Kora automatically prunes the graph to include only the declared @TestComponent nodes and their dependencies, ensuring you test exactly what is wired without unnecessary overhead.

    You can use @Mock alongside @TestComponent to replace specific nodes in the graph with mocks (e.g., using Mockito). This approach supports component tests, integration tests using Testcontainers, and black-box tests against a built service.

    @KoraAppTest(Application.class)
    class PetServiceTests {
    
        @Mock
        @TestComponent
        private PetRepository petRepository;   // replace node with a mock
    
        @TestComponent
        private PetService petService;         // inject any node from the graph
    
        @Test
        void findByID() {
            Mockito.when(petRepository.findById(1)).thenReturn(Optional.of(pet));
            assertTrue(petService.findByID(1).isPresent());
        }
    }