Guides by role

Four kinds of people deal with the broker client. Most of them never open this repository; this page tells each one what they touch and where the detail is.

For an integration developer on the Quarkus extension

You write routes; the extension creates the broker clients for them. You configure, you do not construct.

  1. The connection comes from the extension's klok.broker.* keys (klok.broker.service.url, klok.broker.auth.url, klok.broker.auth.client-id, klok.broker.auth.client-secret, klok.broker.tls-trust-certs-file-path, klok.broker.listener-threads), which MicroProfile fills from the KLOK_BROKER_* variables of the service's Vault env secret. Nothing per route. See Configuration.
  2. A subscription is named after the route id and is Exclusive, one message at a time, in order. A route changes that with its own keys: <RouteId>.subscription.concurrency for several receive loops (and with it a Shared subscription unless <RouteId>.subscription.type says otherwise), <RouteId>.subscription.type, <RouteId>.subscription.nack-redelivery-delay-ms, <RouteId>.subscription.receiver-queue-size, and <RouteId>.subscription.batch.max-messages / .max-wait-ms for a batch route. Any concurrency above one gives up the order. See Concurrency and Batches.
  3. At start the extension checks that every topic the service needs exists and is granted (klok.broker.verify-topics, on by default). A missing one stops the start with TOPIC_MISSING or TOPIC_ACCESS_DENIED and a message that names the topic, the klok.dev request link and the line to add by hand. Do what it says; see Missing topics and grants.
  4. Request/reply, for example the ODL calls of KlokOdl, runs on KlokBrokerClientSync. The reply is addressed to your instance and arrives there alone; a timeout or broker failure is a KlokRequestReplyException, which KlokOdl reports as UPSTREAM_UNAVAILABLE. See Request/reply.
  5. In the logs, a line with KLOK_BROKER_CONNECTION means a producer or consumer of your service lost the broker, or a subscription stopped reading; see Connection health. The extension's readiness and liveness checks report the same state (Health checks).
  6. In your tests there is no broker: the extension's test support replaces the client factory with a recording one (see Testing).

For a developer of a service outside the extension

You write a plain Java service, a Node process or a Python worker that takes part in Klok chains.

  • Identity first. The service needs a Keycloak client of its own and publish or consume grants on its topics; its credentials reach it as KLOK_BROKER_AUTH_CLIENT_ID and KLOK_BROKER_AUTH_CLIENT_SECRET from its Vault env object. Who creates them is in the framework development guide, Service Identities.
  • Java. Take klokLibs.klokBrokerclientJava from the dependency catalog and add an SLF4J provider. Build the configuration with KlokBrokerClientConfig.buildFromEnv(), name every topic in full with KlokNamingConventions.fullTopicName, check your topics at start with verifyPublish and verifyConsume, subscribe with KlokSubscriptionOptions.builder("<your route id>"), and close every client when the service stops. A handler that returns acknowledges, one that throws negatively acknowledges, and subscribeWithOutcome lets it decide. Unlike the extension, the plain client does not retry or dead letter a failed message for you, and it sets only klok-instance, klok-sent-time and klok-body-type: pass the chain context (klok-corr-global, klok-corr-platform and the rest) in the properties of what you publish, and the client keys the message by the global id and writes it as klok-correlation-id too for services built before the three ids. Serve requests with KlokBrokerClientSync.handleRequests. Report connectionHealth() and subscriptionHealth() from your health endpoint. The pages: Configuration, Authentication and subscriptions, Properties and CloudEvents, Connection health. A service with its own payload encoding uses KlokBytesBrokerClient (Bytes client).
  • TypeScript. npm install @klok-integration/brokerclient, then new KlokBrokerClient() from the environment, publish, subscribe and runUntilSignal(). The client derives the chain context, retries, dead letters and logs every invocation like a Java route. See TypeScript client.
  • Python. pip install klok-brokerclient from the GitLab PyPI registry, then the same shape as TypeScript. See Python client and workers.
  • Payloads are always the generated datamodel classes (Java), packages (TypeScript, Python) of a Klok datamodel, never a hand-written class: only those carry the schema the Java side registers (Publishing from TypeScript).
  • A payload too large for a message goes to the claim check store with KlokClaimCheckStore (Java and TypeScript), and the message carries its klokcc:// reference; the service gets a RustFS key of its own as KLOK_BLOBSTORE_*. See Claim check.

For an operator

You get an alert, or see a service that has stopped working although its pod is Running.

  1. Search the service's console logs (klok-logs-<namespace>-console-* in OpenSearch) for KLOK_BROKER_CONNECTION. Every state but RESTORED means broken; the fields name the topic, the kind of handle, the route and the instance. RECEIVE_LOOP_STOPPED is a subscription that no longer reads with all its loops, and FATAL_ERROR a JVM that ran out of memory: only a restart ends either.
  2. Follow When a service logs a broken connection on Connection health: check the topic's publishers and subscriptions on the broker, and a Failed to authenticate the client line, which points at the service's Keycloak client or its secret.
  3. A restart builds a new connection and a new token. Restart only with a person's yes.

For the maintainer who changes and releases the clients

  1. Work in a worktree. A Java change builds with ./gradlew build (Java 21, a GitLab token for the group registry) and is tested against a broker with ./gradlew integrationTest; the TypeScript client with npm test and npm run test:integration, the Python client with pytest. How to start the brokers they need is on Testing.
  2. Set the versions. Java: majorVersion, minorVersion, patchVersion in gradle.properties; the pipeline appends the timestamp. TypeScript and Python: <major.minor> in package.json and pyproject.toml; the pipeline sets the last part to its pipeline number.
  3. After a klok-common release there is nothing to raise: klok catalog release-dependents starts this repository's main pipeline, whose build_job refreshes gradle/klok-libs.versions.toml and builds against it. klok-common and pulsar-client come from that catalog; the other dependencies are still pinned in gradle/libs.versions.toml (#18). The wire contract copies in typescript/klok-brokerclient-typescript/wire-contract/ and python/klok-brokerclient-python/src/klok_brokerclient/wire_contract/ follow too: the job wire_contract_sync runs klok vendor sync --latest before the Python and TypeScript tests and packages, so a contract that breaks a client fails its tests, and the job klok vendor check fails when a committed copy differs from its release. To work against the new contract locally, or to move the committed copy, run klok vendor sync --latest and commit it. Never edit a copy by hand (Vendored copies).
  4. Open a merge request with a Conventional Commits title. Its pipeline runs the Java jobs (build, tests, java-integration-test, java-integration-test-auth) only when Java files changed (src/, build.gradle, settings.gradle, gradle/), the Python tests when python/ changed, and the TypeScript tests when typescript/ or python/ changed. A merge request typed docs that touches only klok-docs/ and the README runs only the documentation lane.
  5. After the merge, on main:
  • Java: release_and_publish_job releases to the GitLab registry and maven_central_release sends the same files to Maven Central, which may fail or wait for Central without holding anything back. Both run only when Java changed, like the build. Then update_klok_catalog writes klokBrokerclient into the dependency catalog and runs klok catalog release-dependents, which starts the Quarkus extension's pipeline when the extension was built against an older broker client. Wait for the catalog commit, not for the merge:
glab api --hostname git.klok.dev "projects/klok%2Fframework%2Fklok-gradle-dependency-catalog/repository/commits?ref_name=main&per_page=10" | jq -r '.[].title'
  • Python, when python/ changed: the package is built and uploaded to the project's GitLab PyPI registry.
  • TypeScript, when typescript/ changed: the tarball is staged on npmjs.org, and a maintainer approves it with 2FA under Staged Packages on npmjs.com or with npm stage approve <id> (Releases).
  1. The release order of a change that spans the framework libraries is klok-common, then this client, then the Quarkus extension, each one after the previous one's version is in the catalog.