Create Channel
Programmatically create a KubeMQ channel of a given type using the Java SDK administration API.
Overview
KubeMQ auto-creates a channel the first time a client publishes or subscribes to it — convenient for prototyping, but a liability once channels are infrastructure you need to reason about. Pre-creating channels with the management API lets you provision topology before any producer or consumer connects: enforce naming conventions in a startup script, stand up the channels a service depends on as part of deployment, or fail fast if a required channel is missing instead of it silently springing into existence.
Channel creation is pattern-specific: createEventsChannel and createEventsStoreChannel live on PubSubClient, createQueuesChannel on QueuesClient, and createCommandsChannel / createQueriesChannel on CQClient — each registering the channel directly with the broker.
Gotchas: the call is idempotent for a matching name and type, so it's safe to call on every startup — but a channel's type is fixed at creation, and reusing the name with a different type fails rather than migrating it. Creation only registers the channel; it does not start a consumer, so a freshly created queue or events channel happily accepts messages with nothing yet reading them.
Prerequisites
- KubeMQ server running on
localhost:50000 - Java SDK installed (
implementation 'io.kubemq.sdk:kubemq-sdk-Java:3.1.1'(Gradle) or Maven dependency from Getting Started)
Code
package io.kubemq.example.management;
import io.kubemq.sdk.cq.CQClient;
import io.kubemq.sdk.pubsub.PubSubClient;
import io.kubemq.sdk.queues.QueuesClient;
public class CreateChannelExample {
private static final String ADDRESS = "localhost:50000";
private static final String CLIENT_ID = "java-management-create-channel-client";
public static void main(String[] args) {
System.out.println("=== Create Channels ===\n");
// Create PubSub channels (events and events-store)
try (PubSubClient pubsub = PubSubClient.builder().address(ADDRESS).clientId(CLIENT_ID + "-pubsub").build()) {
pubsub.createEventsChannel("java-management.events-test");
System.out.println("Events channel created.");
pubsub.createEventsStoreChannel("java-management.store-test");
System.out.println("EventsStore channel created.");
// Clean up: delete the created channels
pubsub.deleteEventsChannel("java-management.events-test");
pubsub.deleteEventsStoreChannel("java-management.store-test");
}
// Create Queues channel
try (QueuesClient queues = QueuesClient.builder().address(ADDRESS).clientId(CLIENT_ID + "-queues").build()) {
queues.createQueuesChannel("java-management.queues-test");
System.out.println("Queues channel created.");
// Clean up: delete the created channel
queues.deleteQueuesChannel("java-management.queues-test");
}
// Create Commands and Queries channels
try (CQClient cq = CQClient.builder().address(ADDRESS).clientId(CLIENT_ID + "-cq").build()) {
cq.createCommandsChannel("java-management.commands-test");
System.out.println("Commands channel created.");
cq.createQueriesChannel("java-management.queries-test");
System.out.println("Queries channel created.");
cq.deleteCommandsChannel("java-management.commands-test");
cq.deleteQueriesChannel("java-management.queries-test");
}
System.out.println("\nAll channels created and cleaned up.");
}
}
How It Works
- Channel creation is pattern-specific:
createEventsChannel/createEventsStoreChannellive onPubSubClient;createQueuesChannelonQueuesClient; andcreateCommandsChannel/createQueriesChannelonCQClient. - Creating a channel that already exists is idempotent — the broker returns success without duplicating the channel.
- The example creates and immediately deletes each channel to leave the broker clean after the demo; in production you would omit the delete calls.
- Each client is constructed in its own
try-with-resourcesblock so the gRPC connection is released as soon as the management call completes.
Related
Was this page helpful?