politrons-reactive Examples

repository·master·Indexed 43 days ago

https://github.com/politrons/reactive

A collection of practical examples covering reactive programming libraries (ReactiveX, Spring Reactor, Akka, RSocket), distributed systems patterns (Kafka, ZooKeeper), and Java language evolution from Java 8 through Java 22. Includes benchmarks for TigerBeetle with Project Loom virtual threads, memory footprint comparisons between HashMap and Chronicle Map, a replica of Netflix's GraphQL Federation architecture using Spring Boot 3, and Quarkus implementation guides.

Tokens
3.4K
Snippets
10
Records
21
Agent score
56%

What's inside politrons-reactive

  1. Compare HashMap vs Chronicle Map memory footprint

    master

    This document provides a benchmark comparison between a standard on-heap java.util.HashMap and an off-heap Chronicle Map using JOL (Java Object Layout) to measure memory overhead.

    Key Findings

    • Java HashMap (on-heap): High overhead due to per-entry HashMap.Node objects, object headers, padding, and GC pressure. Average overhead is approximately 40 B per entry.
    • Chronicle Map (off-heap): Significantly lower overhead by serializing data contiguously into a direct-memory slab. Average overhead is approximately 15 B per entry.

    Comparison Summary (for 1 million String → String pairs)

    MetricJava HashMapChronicle Map
    Total Size~130 MiB~28 MiB
    Avg. Overhead / Entry40 B15 B
    | Map implementation | Total size | Logical data¹ | Structural overhead | Avg. overhead / entry |
    |--------------------|-----------:|--------------:|---------------------:|----------------------:|
    | **Java `HashMap` (on‑heap)** | **136 388 672 B** (≈ 130 MiB) | 96 000 000 B | 40 388 672 B | 40 B |
    | **OpenHFT Chronicle Map (off‑heap)** | **29 396 992 B** (≈ 28 MiB) | 13 777 812 B | 15 619 180 B | 15 B |
  2. TigerBeetle Virtual Threads Benchmark Logic

    master

    The benchmark simulates high-concurrency financial transfers using the following workflow:

    1. Connection: Connects to a single-node TigerBeetle cluster on port 3000.
    2. Concurrency Model: Spawns a pool of 10,000 Java virtual threads.
    3. Task Execution: Each of the 10,000 tasks performs:
      • Generation of random IDs for debit accounts, credit accounts, and transfers.
      • Idempotent account creation (creating accounts if they do not exist).
      • Issuing a transfer with amount = 10.
      • Verifying account balances for both debit and credit accounts to ensure correctness.
    4. Lifecycle: Shuts down the thread pool and waits up to 10 seconds for task completion before printing the total elapsed time.
  3. Understand the Netflix GraphQL Federation Replica Architecture

    master

    This project implements a simplified replica of Netflix's GraphQL Federation architecture using Spring Boot 3. It allows multiple microservices to maintain their own schemas while providing a single, unified GraphQL API to clients.

    Core Components

    • Federation Service (federation): The central GraphQL gateway. It integrates schemas from the sub-services, handles complex queries by delegating to the appropriate services, and aggregates/transforms data from multiple sources into a single response.
    • Actor Service (actor-DGS): An independent microservice managing actor data (e.g., names and surnames) via its own GraphQL endpoint.
    • Director Service (director-DGS): An independent microservice managing director data via its own GraphQL endpoint.

    Data Flow

    1. Client Query: The client sends a single GraphQL query to the Federation Service.
    2. Schema Integration: The Federation Service uses the integrated schemas from the Actor and Director services.
    3. Data Retrieval: The Federation Service makes HTTP GraphQL requests to the Actor and Director services to fetch required data.
    4. Aggregation: The Federation Service aggregates the results into a unified response and returns it to the client.
  4. Explore ReactiveX (RxJava) examples

    master

    The repository provides practical Java examples for the ReactiveX platform, covering common use cases that go beyond simple marble diagrams. Key categories include:

    • Contactable: Examples of HotObservable.
    • Combining: Operations like Chain, Concat, Merge, Zip, and Switch.
    • Creating: Methods such as Create, Defer, Interval, and Subscription.
    • Filtering: Operators like Debounce, Distinct, Skip, Take, and First.
    • Transforming: Operators like Map, FlatMap, GroupBy, Scan, Collect, Buffer, Window, and Compose.
    • Scheduler: Asynchronous execution examples.
    • Errors: Handling exceptions within the pipeline.
    • Utils: Delay, AmbConditional, Cache, and ToBlocking.
    • Single: Using Single for observables that emit exactly one item.
    • Relay: Using Relay for subjects that keep the pipeline open constantly.
  5. Add the SmallRye OpenAPI extension

    master

    To enable OpenAPI features (which allow you to register your API and generate an OpenAPI file with descriptions), add the smallrye-openapi extension using the following command. This extension is also required for the WebSocket and Vert.x examples in this repository.

    ./mvnw quarkus:add-extension -Dextensions="smallrye-openapi"
  6. Run a Quarkus application in dev mode

    master

    After creating your project, use the Maven wrapper to compile and start the application in development mode. Once running, the application container is available at http://localhost:8080/ and your specific endpoints will be accessible at http://localhost:8080/your_endpoint.

    ./mvnw compile quarkus:dev
  7. Configure Chronicle Map run-time flags for JDK 22

    master

    When running Chronicle Map on JDK 22, you must provide specific --add-opens flags to allow access to internal Java modules required for off-heap operations.

    --add-opens=java.base/java.lang.reflect=ALL-UNNAMED \
    --add-opens=java.base/sun.nio.ch=ALL-UNNAMED
  8. Explore Java language feature evolution

    master

    The repository tracks and demonstrates features introduced in various Java versions through code examples:

    • Java 8: Stream, Functions, and CompletableFuture.
    • Java 9: Flow API, Optional improvements, and the Module system.
    • Java 10: Collections and Local variable type inference.
    • Java 11: HttpClient, String enhancements, File API, and Collection improvements.
    • Java 12: Switch Expressions, String API, and JVM Constants API.
    • Java 14: Pattern matching, Multiline text, and Record types.
    • Java 15: Sealed classes.
    • Java 16/17: General feature updates.
    • Java 18: Project Loom features.
    • Java 19: Pattern matching improvements and Virtual Threads.
    • Java 20, 21, 22: Latest language features.
  9. Create a Quarkus project using Maven

    master

    You can initialize a new Quarkus project using the quarkus-maven-plugin. You must provide a group ID, artifact ID, the fully qualified name of your first Resource class, and the endpoint path.

    mvn io.quarkus:quarkus-maven-plugin:0.13.1:create \
        -DprojectGroupId=your.group.id \
        -DprojectArtifactId=Your_artifact_id \
        -DclassName="package.and.class.name.of.your.first.Resource" \
        -Dpath="/your_endpoint"
  10. Explore Akka and Akka Stream examples

    master

    The repository contains implementations of Akka patterns and the Akka Reactive Stream API:

    • Akka: Uses Akka Typed with a Java DSL to demonstrate various patterns.
    • Akka Stream: Demonstrates the reactive stream API including Source, Flow, Sink, Subscriber, Back-pressure, and GraphDSL.