Create Channel
Create KubeMQ channels for all five messaging types using the Kotlin SDK management 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.
Each client exposes typed create*Channel() methods for the patterns it owns — events and events-store on the pub/sub client, queues on the queues client, commands and queries on the CQ client — 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 - Kotlin SDK installed (
implementation("io.kubemq.sdk:kubemq-sdk-kotlin:1.0.1"))
Code
package io.kubemq.sdk.examples.management
import io.kubemq.sdk.client.KubeMQClient
import kotlinx.coroutines.runBlocking
private const val ADDRESS = "localhost:50000"
private const val CLIENT_ID = "kotlin-management-create-channel"
fun main() = runBlocking {
val pubSub = KubeMQClient.pubSub {
address = ADDRESS
clientId = CLIENT_ID
}
val queues = KubeMQClient.queues {
address = ADDRESS
clientId = CLIENT_ID
}
val cq = KubeMQClient.cq {
address = ADDRESS
clientId = CLIENT_ID
}
pubSub.use { ps ->
queues.use { q ->
cq.use { c ->
try {
// Create channels for all types
ps.createEventsChannel("kotlin-management.create-events")
println("Created events channel")
ps.createEventsStoreChannel("kotlin-management.create-events-store")
println("Created events store channel")
q.createQueuesChannel("kotlin-management.create-queues")
println("Created queues channel")
c.createCommandsChannel("kotlin-management.create-commands")
println("Created commands channel")
c.createQueriesChannel("kotlin-management.create-queries")
println("Created queries channel")
println("\nAll 5 channel types created successfully.")
} finally {
// Cleanup
try { ps.deleteEventsChannel("kotlin-management.create-events") } catch (_: Exception) {}
try { ps.deleteEventsStoreChannel("kotlin-management.create-events-store") } catch (_: Exception) {}
try { q.deleteQueuesChannel("kotlin-management.create-queues") } catch (_: Exception) {}
try { c.deleteCommandsChannel("kotlin-management.create-commands") } catch (_: Exception) {}
try { c.deleteQueriesChannel("kotlin-management.create-queries") } catch (_: Exception) {}
}
println("Done.")
}
}
}
}How It Works
- Each client type has typed
create*Channel()methods for its supported channel types. PubSubClientcreates events and events store channels.QueuesClientcreates queue channels.CQClientcreates commands and queries channels.- Channels are cleaned up in the
finallyblock usingdelete*Channel()methods.
Related
Was this page helpful?