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:
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 Class | Use For |
|---|---|
PubSubClient | Events, Events Store |
QueuesClient | Queues |
CQClient | Commands, Queries (RPC) |
Send Your First Event
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
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
| Parameter | Type | Default | Description |
|---|---|---|---|
address | String | localhost:50000 | KubeMQ server address |
clientId | String | Auto-generated UUID | Unique client identifier |
authToken | String | null | JWT authentication token |
tls | boolean | false | Enable TLS encryption |
tlsCertFile | String | null | TLS certificate file (PEM) |
tlsKeyFile | String | null | TLS private key file (PEM) |
caCertFile | String | null | CA certificate file |
maxReceiveSize | int | 104857600 | Max inbound message size (100MB) |
reconnectIntervalSeconds | int | 1 | Reconnection interval |
logLevel | Level | INFO | Logging 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:
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());
}| Exception | Retryable | When |
|---|---|---|
ConnectionException | Yes | Server unavailable |
KubeMQTimeoutException | Yes | Deadline exceeded |
AuthenticationException | No | Invalid credentials |
AuthorizationException | No | Insufficient permissions |
ValidationException | No | Invalid parameters |
Next Steps
- Java SDK Reference — full API documentation
- Java SDK Examples — complete examples for all patterns
- GitHub Repository — source code and issues
Was this page helpful?