0
# Kafka Helper
1
2
The `org.testcontainers.kafka.KafkaHelper` class provides shared constants and utility methods for modern Kafka containers (`KafkaContainer` and `ConfluentKafkaContainer`). This class is package-private and used internally by the container implementations.
3
4
## Constants
5
6
### KAFKA_PORT
7
8
Default Kafka port used by modern containers.
9
10
```java { .api }
11
static final int KAFKA_PORT = 9092;
12
```
13
14
### STARTER_SCRIPT
15
16
Path to the startup script used internally by containers.
17
18
```java { .api }
19
static final String STARTER_SCRIPT = "/tmp/testcontainers_start.sh";
20
```
21
22
### COMMAND
23
24
Default command array used to start containers.
25
26
```java { .api }
27
static final String[] COMMAND = {
28
"sh",
29
"-c",
30
"while [ ! -f " + STARTER_SCRIPT + " ]; do sleep 0.1; done; " + STARTER_SCRIPT
31
};
32
```
33
34
### WAIT_STRATEGY
35
36
Default wait strategy for container readiness.
37
38
```java { .api }
39
static final WaitStrategy WAIT_STRATEGY = Wait.forLogMessage(".*Transitioning from RECOVERY to RUNNING.*", 1);
40
```
41
42
## Utility Methods
43
44
### envVars()
45
46
Returns the default environment variables map for KRaft mode configuration.
47
48
```java { .api }
49
static Map<String, String> envVars();
50
```
51
52
**Returns**: Map containing default Kafka environment variables including:
53
- `CLUSTER_ID`: Default cluster ID for KRaft mode
54
- `KAFKA_LISTENERS`: Default listener configuration
55
- `KAFKA_LISTENER_SECURITY_PROTOCOL_MAP`: Protocol mapping
56
- `KAFKA_INTER_BROKER_LISTENER_NAME`: Inter-broker communication listener
57
- `KAFKA_PROCESS_ROLES`: Process roles (broker,controller)
58
- `KAFKA_CONTROLLER_LISTENER_NAMES`: Controller listener names
59
- `KAFKA_NODE_ID`: Node identifier
60
- `KAFKA_CONTROLLER_QUORUM_VOTERS`: Quorum voter configuration
61
- Various replication and topic configuration settings
62
63
### resolveListeners(GenericContainer<?> kafkaContainer, Set<String> listenersSuppliers)
64
65
Resolves custom listeners and updates container configuration.
66
67
```java { .api }
68
static void resolveListeners(GenericContainer<?> kafkaContainer, Set<String> listenersSuppliers);
69
```
70
71
**Parameters**:
72
- `kafkaContainer` (GenericContainer<?>): The Kafka container instance
73
- `listenersSuppliers` (Set<String>): Set of listener strings in format "host:port"
74
75
This method processes custom listeners, adds network aliases, and updates the container's `KAFKA_LISTENERS` and `KAFKA_LISTENER_SECURITY_PROTOCOL_MAP` environment variables.
76
77
### resolveAdvertisedListeners(Set<Supplier<String>> listenerSuppliers)
78
79
Resolves advertised listeners from supplier functions.
80
81
```java { .api }
82
static List<String> resolveAdvertisedListeners(Set<Supplier<String>> listenerSuppliers);
83
```
84
85
**Parameters**:
86
- `listenerSuppliers` (Set<Supplier<String>>): Set of supplier functions providing listener strings
87
88
**Returns**: List of advertised listener strings with protocol prefixes
89
90
## Default Configuration
91
92
The KafkaHelper provides these default configurations for modern containers:
93
94
- **Default Cluster ID**: `"4L6g3nShT-eMCtK--X86sw"`
95
- **Default Listeners**:
96
- `PLAINTEXT://0.0.0.0:9092` - External connections
97
- `BROKER://0.0.0.0:9093` - Inter-broker communication
98
- `CONTROLLER://0.0.0.0:9094` - KRaft controller
99
- **Protocol Mapping**: Maps PLAINTEXT, BROKER, and CONTROLLER protocols
100
- **Single Node Setup**: Configured for single-node testing scenarios with appropriate replication factors
101
- **KRaft Mode**: Enabled by default with process roles set to "broker,controller"
102
103
## Internal Usage
104
105
This class is used internally by:
106
- `org.testcontainers.kafka.KafkaContainer` - Apache Kafka containers
107
- `org.testcontainers.kafka.ConfluentKafkaContainer` - Confluent Platform containers
108
109
The helper ensures consistent configuration and behavior across both modern container implementations while providing shared functionality for listener resolution and environment variable setup.