# Send Your First Message (/sdks/java/tutorials/first-message)



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](/sdks/java)).

## Create a Client [#create-a-client]

The Java SDK provides pattern-specific client classes:

```java title="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 Class   | Use For                 |
| -------------- | ----------------------- |
| `PubSubClient` | Events, Events Store    |
| `QueuesClient` | Queues                  |
| `CQClient`     | Commands, Queries (RPC) |

## Send Your First Event [#send-your-first-event]

```java title="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 [#receive-events]

```java title="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 [#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](/sdks/java/how-to/tls/tls-setup) for a full example configuring `tls`, `tlsCertFile`, `tlsKeyFile`, and `caCertFile`.

## Error Handling [#error-handling]

The SDK uses a typed exception hierarchy rooted at `KubeMQException`:

```java title="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());
}
```

| 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 [#next-steps]

* [Java SDK Reference](/sdks/java/reference) — full API documentation
* [Java SDK Examples](/sdks/java/how-to) — complete examples for all patterns
* [GitHub Repository](https://github.com/kubemq-io/kubemq-java-v2) — source code and issues
