# RPC (/sdks/java/reference/rpc)



`CQClient` handles request–reply workloads. Commands embed timeouts in `CommandMessage`; queries extend `Message` with cache hints.

## Commands [#commands]

### sendCommand [#sendcommand]

```java
CommandResponseMessage sendCommand(CommandMessage message) throws KubeMQException
```

Sends a command and blocks until a worker responds or the timeout elapses.

**Parameters:**

| Name      | Type             | Required | Description                                      |
| --------- | ---------------- | -------- | ------------------------------------------------ |
| `message` | `CommandMessage` | Yes      | Channel, body/metadata, `timeoutInSeconds`, tags |

**Returns:** `CommandResponseMessage` — execution metadata and payload.

> **Note:** `sendCommandRequest` is a deprecated alias (deprecated since 2.2.0, `forRemoval=true` at v3.0). Prefer `sendCommand`.

### subscribeToCommands [#subscribetocommands]

```java
void subscribeToCommands(CommandsSubscription subscription) throws KubeMQException
```

Registers a consumer that returns `CommandResponseMessage` from the handler callback.

**CommandsSubscription fields:**

| Field                      | Type                               | Description          |
| -------------------------- | ---------------------------------- | -------------------- |
| `channel`                  | `String`                           | Channel to listen on |
| `group`                    | `String`                           | Consumer group       |
| `onReceiveCommandCallback` | `Consumer<CommandMessageReceived>` | Handler              |
| `onErrorCallback`          | `Consumer<KubeMQException>`        | Error hook           |

## Queries [#queries]

### sendQuery [#sendquery]

```java
QueryResponseMessage sendQuery(QueryMessage message) throws KubeMQException
```

**QueryMessage** extends CommandMessage with:

| Field               | Type     | Description                  |
| ------------------- | -------- | ---------------------------- |
| `cacheKey`          | `String` | Cache key for response reuse |
| `cacheTtlInSeconds` | `int`    | TTL in seconds               |

> **Note:** `sendQueryRequest` is a deprecated alias (deprecated since 2.2.0, `forRemoval=true` at v3.0). Prefer `sendQuery`.

### subscribeToQueries [#subscribetoqueries]

```java
void subscribeToQueries(QueriesSubscription subscription) throws KubeMQException
```

Same pattern as commands with query-specific callbacks.

## Quick Usage [#quick-usage]

```java title="Rpc.java"
CommandMessage cmd = CommandMessage.builder()
    .channel("svc.commands")
    .body(payload)
    .timeoutInSeconds(10)
    .build();

client.sendCommand(cmd);
```

## See Also [#see-also]

* [RPC Examples](/sdks/java/how-to/rpc/)
* [RPC pattern](/learn/rpc/getting-started)
* [Java SDK Getting Started](/sdks/java)
