Policy & Community

Eclipse tutorial shows LangChain4j CDI bringing LLM orchestration to Jakarta EE 11 apps

Key Takeaway

LangChain4j's CDI portable extension lets Jakarta EE 11 developers declare AI-backed services as annotated interfaces, keeping provider-specific code out of application logic.

Summary

The Eclipse Foundation has published a tutorial demonstrating how to integrate LangChain4j, a declarative AI orchestration API, into a Jakarta EE 11 application running on Open Liberty with Java 21. The walkthrough builds a REST service that accepts a book title and returns up to three related recommendations generated by an LLM, using Java records for the request and response domain model.

The integration relies on the langchain4j-cdi-portable-ext and langchain4j-cdi-config modules, the langchain4j-open-ai provider module at version 1.16.0, and SmallRye Config 3.17.2 for MicroProfile Config support. Developers annotate an interface with @RegisterAIService and @SystemMessage, and the CDI extension supplies the implementation at runtime, while the model provider and API key are supplied through microprofile-config.properties and environment variables rather than hardcoded source.

How LangChain4j CDI wires AI services into Jakarta EE

  • Declarative AI Services: An interface annotated with @RegisterAIService is registered as a CDI bean, and the framework generates the implementation at runtime, mirroring the programming model of Jakarta Data or Spring Data repositories.
  • Prompt definition in code: The @SystemMessage annotation carries the model instructions directly on the interface method, removing manual prompt construction, provider API calls, and response parsing from application code.
  • Provider abstraction: The langchain4j-open-ai module supplies the OpenAI-backed chat model in this example, but the tutorial states that switching providers requires only a dependency and configuration change, not application logic changes.
  • Configuration via MicroProfile Config: The chat model class, API key, and model name are set in META-INF/microprofile-config.properties, with the key injected from the OPENAI_API_KEY environment variable to follow Twelve-Factor App practice.
  • Stack baseline: The project targets Jakarta EE 11 Core Profile, Java 21, and Open Liberty, with SmallRye Config 3.17.2 providing the MicroProfile Config implementation.

Why it matters

Enterprise Java teams evaluating LLM features now have a documented path that keeps AI calls inside familiar CDI and Jakarta EE conventions instead of introducing a separate framework or bespoke HTTP client code. Because provider selection is confined to configuration and a single dependency, applications can move between model backends without rewriting service interfaces, which matters for organizations with procurement or data-residency constraints. The tutorial also reinforces environment-variable-based secret handling, a practice that reduces credential leakage in containerized deployments.

Original reference: newsroom.eclipse.org →