KubeMQ
Client SDKsKotlinHow-to guidesManagement

List Channels

List KubeMQ channels by type with optional prefix filtering using the Kotlin SDK management API.

Overview

Listing channels turns the broker into a discoverable inventory instead of a black box — instead of hardcoding channel names everywhere, you ask the server what actually exists right now. That's exactly what monitoring dashboards, cleanup scripts, and "did my deployment create the channels it should have" checks need. It's read-only and has no effect on message flow, so it's safe to run against production at any time.

Under the hood, listEventsChannels("prefix") and its siblings (listQueuesChannels, listCommandsChannels, listQueriesChannels) query channels of one type, narrowed server-side to names matching the prefix. Each result carries name and lastActivity metadata; an empty string lists every channel of that type.

Gotchas: the filter is a prefix match, not a glob or regex — there's no way to match a suffix or exclude a pattern. Listing is scoped per client — pubSub for events/events-store, queues for queues, cq for commands/queries — so a full inventory means querying each one. And an empty prefix on a broker with many channels can return a large result set; scope the prefix whenever you know the naming convention you're targeting.

Prerequisites

  • KubeMQ server running on localhost:50000
  • Kotlin SDK installed (implementation("io.kubemq.sdk:kubemq-sdk-kotlin:1.0.1"))

Code

ListChannelsExample.kt
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-list-channels"

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 some channels for listing
                    ps.createEventsChannel("kotlin-management.list-events-1")
                    ps.createEventsChannel("kotlin-management.list-events-2")
                    q.createQueuesChannel("kotlin-management.list-queues-1")

                    // List channels by type with prefix filter
                    val eventsChannels = ps.listEventsChannels("kotlin-management.list")
                    println("Events channels (prefix 'kotlin-management.list'): ${eventsChannels.size}")
                    eventsChannels.forEach { println("  ${it.name}") }

                    val eventsStoreChannels = ps.listEventsStoreChannels("kotlin-management.list")
                    println("Events store channels: ${eventsStoreChannels.size}")

                    val queuesChannels = q.listQueuesChannels("kotlin-management.list")
                    println("Queues channels: ${queuesChannels.size}")
                    queuesChannels.forEach { println("  ${it.name}") }

                    val commandsChannels = c.listCommandsChannels("kotlin-management.list")
                    println("Commands channels: ${commandsChannels.size}")

                    val queriesChannels = c.listQueriesChannels("kotlin-management.list")
                    println("Queries channels: ${queriesChannels.size}")
                } finally {
                    try { ps.deleteEventsChannel("kotlin-management.list-events-1") } catch (_: Exception) {}
                    try { ps.deleteEventsChannel("kotlin-management.list-events-2") } catch (_: Exception) {}
                    try { q.deleteQueuesChannel("kotlin-management.list-queues-1") } catch (_: Exception) {}
                }
                println("Done.")
            }
        }
    }
}

How It Works

  • listEventsChannels("prefix") lists events channels matching the prefix filter.
  • Each channel in the result includes name and lastActivity metadata.
  • Similar list*Channels() methods exist for all channel types.
  • Pass an empty string to list all channels of that type.

Was this page helpful?

On this page