KubeMQ
Client SDKsJavaTutorials

Send Your First Message

Connect the Java client to KubeMQ and publish and receive your first message end to end.

This is your first hands-on lesson with the Java SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the Java SDK overview).

Create a Client

The Java SDK provides pattern-specific client classes:

Connect.java
import io.kubemq.sdk.pubsub.PubSubClient;

PubSubClient client = PubSubClient.builder()
    .address("localhost:50000")
    .clientId("my-service")
    .build();

System.out.println("Connected to KubeMQ");
client.close();
Client ClassUse For
PubSubClientEvents, Events Store
QueuesClientQueues
CQClientCommands, Queries (RPC)

Send Your First Event

SendEvent.java
PubSubClient client = PubSubClient.builder()
    .address("localhost:50000")
    .clientId("events-sender")
    .build();

client.publishEvent(EventMessage.builder()
    .channel("notifications")
    .body("hello kubemq".getBytes())
    .build());
System.out.println("Event sent to 'notifications'");
client.close();

Receive Events

ReceiveEvents.java
PubSubClient client = PubSubClient.builder()
    .address("localhost:50000")
    .clientId("events-receiver")
    .build();

client.subscribeToEvents(EventsSubscription.builder()
    .channel("notifications")
    .onReceiveEventCallback(event ->
        System.out.println("Received: " + new String(event.getBody())))
    .onErrorCallback(err ->
        System.err.println("Error: " + err.getMessage()))
    .build());

try {
    Thread.sleep(30000); // Keep listening
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
}
client.close();

Configuration Options

ParameterTypeDefaultDescription
addressStringlocalhost:50000KubeMQ server address
clientIdStringAuto-generated UUIDUnique client identifier
authTokenStringnullJWT authentication token
tlsbooleanfalseEnable TLS encryption
tlsCertFileStringnullTLS certificate file (PEM)
tlsKeyFileStringnullTLS private key file (PEM)
caCertFileStringnullCA certificate file
maxReceiveSizeint104857600Max inbound message size (100MB)
reconnectIntervalSecondsint1Reconnection interval
logLevelLevelINFOLogging level

TLS setup is covered on its own page — see TLS Setup for a full example configuring tls, tlsCertFile, tlsKeyFile, and caCertFile.

Error Handling

The SDK uses a typed exception hierarchy rooted at KubeMQException:

ErrorHandling.java
try {
    client.publishEvent(message);
} catch (ConnectionException e) {
    System.err.println("Connection failed (retryable): " + e.getMessage());
} catch (AuthenticationException e) {
    System.err.println("Auth failed: " + e.getMessage());
} catch (ValidationException e) {
    System.err.println("Invalid request: " + e.getMessage());
} catch (KubeMQException e) {
    System.err.println("SDK error: " + e.getMessage());
}
ExceptionRetryableWhen
ConnectionExceptionYesServer unavailable
KubeMQTimeoutExceptionYesDeadline exceeded
AuthenticationExceptionNoInvalid credentials
AuthorizationExceptionNoInsufficient permissions
ValidationExceptionNoInvalid parameters

Next Steps

Was this page helpful?

On this page