Klok Broker Client

klok-brokerclient-java (dev.klok.framework:klok-brokerclient-java) is the typed wrapper around the Apache Pulsar client that every Klok integration and platform service uses to publish, subscribe and do request/reply on the broker. The Quarkus extension builds on it. A Python client in the same repository (klok-brokerclient, see Python client and workers) puts the same bytes and properties on the broker, so Python workers take part in the same chains; the TypeScript client (@klok-integration/brokerclient, see TypeScript client) does the same for Node processes, such as a web backend, a worker or a command line tool.

What the client adds on top of Pulsar:

  • Typed messages. A KlokBrokerClient<T> publishes and consumes one datamodel class with the Avro schema generated from the datamodel project.
  • Bytes for high throughput. A KlokBytesBrokerClient is the same client with byte[] payloads that go on the wire untouched, for services that bring their own encoding and cannot spend the Avro serialization on every message (see Bytes client).
  • Naming. Topic names come from KlokNamingConventions in klok-common, so a publisher and a subscriber that name the same datamodel and purpose meet on the same full topic (persistent://<tenant>[_<env>]/<domain>/<Model>).
  • Context properties. The client stamps klok-instance, klok-sent-time and klok-body-type on every message and carries whatever else the caller gives: the chain context (klok-corr-global, klok-corr-platform, klok-invocation-id, klok-task-id, klok-route-id, ...) and the CloudEvents ce-* attributes, which the Quarkus extension sets for every route. Readers get them as typed getters on KlokBrokerMessage (see Properties and CloudEvents). The Python and TypeScript clients derive the context themselves, from the message a handler received.
  • Request/reply. KlokBrokerClientSync<T, U> implements the ReqRes_<Req>_<Res>_Request / _Response topic pair with a reply id, a reply topic and a timeout, on a non-durable reply subscription that the request names in klok-reply-subscription and the replier keys the reply with.
  • Authentication. OAuth2 client credentials against Keycloak, exchanged for the Pulsar token by the Pulsar client.
  • Missing topics and grants say what to do. Topics on the Klok broker are never created automatically. A topic that is missing, or a grant the service's role lacks, fails at once with KlokTopicMissingException or KlokTopicAccessDeniedException, whose message names the topic, the klok.dev request link, who approves it and the line to add to the tenant's Pulsar file; verifyPublish and verifyConsume check a topic at startup (see Missing topics and grants). The Python and TypeScript clients report the same with the same words.
  • A lost connection is loud. A watchdog checks every producer and consumer, logs one that stays disconnected at ERROR as KLOK_BROKER_CONNECTION on every check, creates producers and reply consumers again, and reports what it sees in KlokConnectionHealth for a health check (see Connection health).
  • Nothing a handler throws ends a consumer. An Error fails its message like an exception does, and the receive loop goes on. A fatal one, such as an OutOfMemoryError, and a loop that ends for any other reason are reported in KlokSubscriptionHealth (see An Error in a handler on Authentication and subscriptions).
  • Observability. MDC propagation of the context ids and OpenTelemetry metrics per route.
  • A claim check store. A payload too large for a message goes to the platform's object storage (RustFS) with the keys and klokcc:// references of the extension's claim check, in Java and TypeScript (see Claim check).

Who uses it, and how

  • An integration developer on the Quarkus extension never creates a client: the extension creates them for each route, from the klok.broker.* keys and the route's <RouteId>.subscription.* keys. What the developer meets is the client's behaviour: subscriptions named after the route id, concurrency, request/reply through the ODL, the TOPIC_MISSING message when a topic is not provisioned and the KLOK_BROKER_CONNECTION lines when the connection breaks.
  • A developer of a service outside the extension uses the Java client in a plain Java service, the TypeScript client in a Node process (klok-cli publishes its documentation payloads with it) or the Python client in a worker.
  • An operator reads the KLOK_BROKER_CONNECTION lines of a service and acts on them.
  • The maintainer changes and releases the three clients; the Java client sits between klok-common and the Quarkus extension in the release chain.

Guides by role walks each of them through it.

Versions

Published from main as <major.minor.patch>+<timestamp> (for example 0.3.0+260910035904) to the GitLab package registry and Maven Central; each release updates klokBrokerclient in the dependency catalog. The Quarkus extension picks it up with updateKlokLibsToml, and an integration runs the version its extension was built against. The Python and TypeScript packages are versioned per pipeline (<major.minor>.<pipeline>): the Python package goes to the project's GitLab PyPI registry, the TypeScript package publicly to registry.npmjs.org as @klok-integration/brokerclient, where the pipeline stages each version and a maintainer publishes it with 2FA (see Releases in the TypeScript client). Moving both to the catalog's timestamped versions is #20.

Pages