Best Testcontainers-java code snippet using org.testcontainers.containers.KafkaContainer.brokerAdvertisedListener
Source:KafkaContainer.java
...85 }86 @Override87 @SneakyThrows88 protected void containerIsStarted(InspectContainerResponse containerInfo) {89 String brokerAdvertisedListener = brokerAdvertisedListener(containerInfo);90 ExecResult result = execInContainer(91 "kafka-configs",92 "--alter",93 "--bootstrap-server", brokerAdvertisedListener,94 "--entity-type", "brokers",95 "--entity-name", getEnvMap().get("KAFKA_BROKER_ID"),96 "--add-config",97 "advertised.listeners=[" + String.join(",", getBootstrapServers(), brokerAdvertisedListener) + "]"98 );99 if (result.getExitCode() != 0) {100 throw new IllegalStateException(result.toString());101 }102 }103 protected String brokerAdvertisedListener(InspectContainerResponse containerInfo) {104 return String.format("BROKER://%s:%s", containerInfo.getConfig().getHostName(), "9092");105 }106}...
Source:KafkaContainers.java
...26 * Created 2022/7/2627 */28public class KafkaContainers extends KafkaContainer {29 private static final Logger LOG = LoggerFactory.getLogger(KafkaContainers.class);30 private String brokerAdvertisedListener;31 private String zkHost;32 public KafkaContainers(DockerImageName dockerImageName) {33 super(dockerImageName);34 }35 @Override36 protected String brokerAdvertisedListener(InspectContainerResponse containerInfo) {37 return String.format("%s:%s", containerInfo.getConfig().getHostName(), "9092");38 }39 @Override40 @SneakyThrows41 public void containerIsStarted(InspectContainerResponse containerInfo) {42 brokerAdvertisedListener = brokerAdvertisedListener(containerInfo);43 zkHost = brokerAdvertisedListener.replace("9092", "2181");44 String advertisedListeners = String.join(",",45 getBootstrapServers(),46 brokerAdvertisedListener);47 LOG.info("old brokerAdvertisedListener: {}", brokerAdvertisedListener);48 LOG.info("new brokerAdvertisedListener: {}", zkHost);49 LOG.info("entity-name: {}", getEnvMap().get("KAFKA_BROKER_ID"));50 LOG.info("BootstrapServers: {}", getBootstrapServers());51 LOG.info("advertised.listeners: {}", advertisedListeners);52 Container.ExecResult result = execInContainer(53 "kafka-configs",54 "--alter",55 "--zookeeper", zkHost,56 "--entity-type", "brokers",57 "--entity-name", getEnvMap().get("KAFKA_BROKER_ID"),58 "--add-config",59 "advertised.listeners=[" + advertisedListeners + "]"60 );61 if (result.getExitCode() != 0) {62 throw new IllegalStateException(result.toString());...
brokerAdvertisedListener
Using AI Code Generation
1package org.testcontainers.containers;2import org.apache.kafka.clients.admin.AdminClient;3import org.apache.kafka.clients.admin.AdminClientConfig;4import org.apache.kafka.clients.admin.NewTopic;5import org.apache.kafka.clients.producer.KafkaProducer;6import org.apache.kafka.clients.producer.ProducerRecord;7import org.apache.kafka.common.serialization.StringSerializer;8import org.junit.Test;9import java.util.Collections;10import java.util.Properties;11import java.util.concurrent.ExecutionException;12import static org.junit.Assert.assertEquals;13public class KafkaContainerTest {14 public void testKafkaContainer() throws ExecutionException, InterruptedException {15 try (KafkaContainer kafka = new KafkaContainer()) {16 kafka.start();17 String topicName = "test-topic";18 AdminClient adminClient = AdminClient.create(Collections.singletonMap(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers()));19 adminClient.createTopics(Collections.singleton(new NewTopic(topicName, 1, (short) 1))).all().get();20 Properties producerProperties = new Properties();21 producerProperties.put("bootstrap.servers", kafka.getBootstrapServers());22 producerProperties.put("key.serializer", StringSerializer.class.getName());23 producerProperties.put("value.serializer", StringSerializer.class.getName());24 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);25 producer.send(new ProducerRecord<>(topicName, "key", "value")).get();26 assertEquals(kafka.getBootstrapServers(), kafka.getBrokers().get(0).getAdvertisedListeners().get(0).toString());27 }28 }29}30package org.testcontainers.containers;31import org.apache.kafka.clients.admin.AdminClient;32import org.apache.kafka.clients.admin.AdminClientConfig;33import org.apache.kafka.clients.admin.NewTopic;34import org.apache.kafka.clients.producer.KafkaProducer;35import org.apache.kafka.clients.producer.ProducerRecord;36import org.apache.kafka.common.serialization.StringSerializer;37import org.junit.Test;38import java.util.Collections;39import java.util.Properties;40import java.util.concurrent.ExecutionException;41import static org.junit.Assert.assertEquals;42public class KafkaContainerTest {43 public void testKafkaContainer() throws ExecutionException, InterruptedException {44 try (KafkaContainer kafka = new KafkaContainer()) {45 kafka.start();46 String topicName = "test-topic";47 AdminClient adminClient = AdminClient.create(Collections.singletonMap
brokerAdvertisedListener
Using AI Code Generation
1package org.testcontainers.containers;2import org.apache.kafka.clients.admin.AdminClient;3import org.apache.kafka.clients.admin.NewTopic;4import org.junit.Test;5import java.util.Collections;6import java.util.Properties;7import java.util.concurrent.ExecutionException;8public class KafkaContainerTest {9 public void testKafkaContainer() throws ExecutionException, InterruptedException {10 KafkaContainer kafkaContainer = new KafkaContainer();11 kafkaContainer.start();12 String brokerAdvertisedListener = kafkaContainer.getBootstrapServers();13 Properties properties = new Properties();14 properties.put("bootstrap.servers", brokerAdvertisedListener);15 AdminClient adminClient = AdminClient.create(properties);16 NewTopic newTopic = new NewTopic("testTopic", 1, (short) 1);17 adminClient.createTopics(Collections.singletonList(newTopic)).all().get();18 }19}20package org.testcontainers.containers;21import org.apache.kafka.clients.admin.AdminClient;22import org.apache.kafka.clients.admin.NewTopic;23import org.junit.Test;24import java.util.Collections;25import java.util.Properties;26import java.util.concurrent.ExecutionException;27public class KafkaContainerTest {28 public void testKafkaContainer() throws ExecutionException, InterruptedException {29 KafkaContainer kafkaContainer = new KafkaContainer();30 kafkaContainer.start();31 String bootstrapServers = kafkaContainer.getBootstrapServers();32 Properties properties = new Properties();33 properties.put("bootstrap.servers", bootstrapServers);34 AdminClient adminClient = AdminClient.create(properties);35 NewTopic newTopic = new NewTopic("testTopic", 1, (short) 1);36 adminClient.createTopics(Collections.singletonList(newTopic)).all().get();37 }38}39 at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:326)40 at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311)41 at org.testcontainers.containers.KafkaContainer.start(KafkaContainer.java:108)42 at org.testcontainers.containers.KafkaContainerTest.testKafkaContainer(KafkaContainerTest.java:21)43 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)44 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)45 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
brokerAdvertisedListener
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2public class KafkaContainerBrokerAdvertisedListenerTest {3 public static void main(String[] args) {4 KafkaContainer kafkaContainer = new KafkaContainer();5 kafkaContainer.start();6 System.out.println("Kafka container is running: " + kafkaContainer.isRunning());7 System.out.println("Kafka broker advertised listener: " + kafkaContainer.getBootstrapServers());8 kafkaContainer.stop();9 }10}11import org.testcontainers.containers.KafkaContainer;12import org.testcontainers.containers.Network;13import org.testcontainers.containers.ZooKeeperContainer;14public class KafkaContainerWithZookeeperTest {15 public static void main(String[] args) {16 Network network = Network.newNetwork();17 ZooKeeperContainer zooKeeperContainer = new ZooKeeperContainer().withNetwork(network);18 KafkaContainer kafkaContainer = new KafkaContainer().withNetwork(network).withZookeeper(zooKeeperContainer);19 zooKeeperContainer.start();20 kafkaContainer.start();21 System.out.println("Kafka container is running: " + kafkaContainer.isRunning());22 System.out.println("Kafka broker advertised listener: " + kafkaContainer.getBootstrapServers());23 kafkaContainer.stop();24 zooKeeperContainer.stop();25 }26}27import org.testcontainers.containers.KafkaContainer;28import org.testcontainers.containers.Network;29public class KafkaContainerWithNetworkAliasesTest {30 public static void main(String[] args) {31 Network network = Network.newNetwork();32 KafkaContainer kafkaContainer = new KafkaContainer().withNetwork(network).withNetworkAliases("kafka");33 kafkaContainer.start();34 System.out.println("Kafka container is running: " + kafkaContainer.isRunning());35 System.out.println("Kafka broker advertised listener: " + kafkaContainer.getBootstrapServers());36 kafkaContainer.stop();37 }38}39import org.testcontainers.containers.KafkaContainer;40public class KafkaContainerWithExternalZookeeperTest {41 public static void main(String[] args) {42 KafkaContainer kafkaContainer = new KafkaContainer().withExternalZookeeper("localhost:2181");43 kafkaContainer.start();
brokerAdvertisedListener
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.containers.Network;3import org.testcontainers.containers.wait.strategy.Wait;4public class TestKafkaContainer {5 public static void main(String[] args) {6 Network network = Network.newNetwork();7 KafkaContainer kafka = new KafkaContainer("5.3.0")8 .withNetwork(network)9 .withNetworkAliases("kafka")10 .withEmbeddedZookeeper()11 .withExposedPorts(9092)12 .waitingFor(Wait.forLogMessage(".*started.*", 1));13 kafka.start();14 String brokerAdvertisedListener = kafka.getBrokerAdvertisedListener();15 System.out.println("Broker advertised listener: " + brokerAdvertisedListener);16 kafka.stop();17 }18}19import org.testcontainers.containers.KafkaContainer;20import org.testcontainers.containers.Network;21import org.testcontainers.containers.wait.strategy.Wait;22public class TestKafkaContainer {23 public static void main(String[] args) {24 Network network = Network.newNetwork();25 KafkaContainer kafka = new KafkaContainer("5.3.0")26 .withNetwork(network)27 .withNetworkAliases("kafka")28 .withEmbeddedZookeeper()29 .withExposedPorts(9092)30 .waitingFor(Wait.forLogMessage(".*started.*", 1));31 kafka.start();32 String brokerAdvertisedListener = kafka.getBootstrapServers();33 System.out.println("Broker advertised listener: " + brokerAdvertisedListener);34 kafka.stop();35 }36}37import org.testcontainers.containers.KafkaContainer;38import org.testcontainers.containers.Network;39import org.testcontainers.containers.wait.strategy.Wait;40public class TestKafkaContainer {41 public static void main(String[] args) {
brokerAdvertisedListener
Using AI Code Generation
1package org.testcontainers.containers;2import org.junit.Test;3import org.testcontainers.containers.KafkaContainer;4import org.testcontainers.containers.Network;5import java.util.List;6public class KafkaTest {7 public void testKafka() {8 Network network = Network.newNetwork();9 KafkaContainer kafka = new KafkaContainer()10 .withNetwork(network)11 .withNetworkAliases("kafka1", "kafka2", "kafka3");12 kafka.start();13 List<String> brokerList = kafka.getBootstrapServers();14 System.out.println(brokerList);15 String advertisedListener = kafka.getAdvertisedListener();16 System.out.println(advertisedListener);17 kafka.stop();18 }19}
brokerAdvertisedListener
Using AI Code Generation
1import org.testcontainers.containers.KafkaContainer;2import org.testcontainers.utility.DockerImageName;3public class TestContainerKafka {4 public static void main(String[] args) {5 KafkaContainer container = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"));6 container.start();7 System.out.println(container.getBootstrapServers());8 System.out.println(container.getNetworkAliases());9 System.out.println(container.getExposedPorts());10 System.out.println(container.getContainerIpAddress());11 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings());12 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp"));13 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPortSpec());14 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostIp());15 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());16 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());17 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());18 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());19 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());20 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());21 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());22 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHost
brokerAdvertisedListener
Using AI Code Generation
1package com.testcontainers.kafka;2import java.util.HashMap;3import java.util.Map;4import java.util.Properties;5import java.util.concurrent.ExecutionException;6import org.apache.kafka.clients.admin.AdminClient;7import org.apache.kafka.clients.admin.AdminClientConfig;8import org.apache.kafka.clients.admin.CreateTopicsResult;9import org.apache.kafka.clients.admin.NewTopic;10import org.apache.kafka.clients.consumer.ConsumerConfig;11import org.apache.kafka.clients.producer.KafkaProducer;12import org.apache.kafka.clients.producer.ProducerConfig;13import org.apache.kafka.clients.producer.ProducerRecord;14import org.apache.kafka.clients.producer.RecordMetadata;15import org.apache.kafka.common.serialization.StringDeserializer;16import org.apache.kafka.common.serialization.StringSerializer;17import org.junit.jupiter.api.Test;18import org.testcontainers.containers.KafkaContainer;19import org.testcontainers.utility.DockerImageName;20public class KafkaContainerTest {21 public void testKafkaContainer() throws ExecutionException, InterruptedException {22 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.0"));23 kafkaContainer.start();24 Map<String, Object> adminClientConfig = new HashMap<>();25 adminClientConfig.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());26 AdminClient adminClient = AdminClient.create(adminClientConfig);27 NewTopic topic = new NewTopic("test", 1, (short) 1);28 CreateTopicsResult result = adminClient.createTopics(List.of(topic));29 result.all().get();30 Properties producerProperties = new Properties();31 producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());32 producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());33 producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());34 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);35 ProducerRecord<String, String> record = new ProducerRecord<>("test", "Hello World");36 producer.send(record, (metadata, exception)
brokerAdvertisedListener
Using AI Code Generation
1KafkaContainer container = new KafkaContainer()2 .withEmbeddedZookeeper()3 .withExposedPorts(9092)4 .withBrokerAdvertisedListener(new ListenerAddress("PLAINTEXT", "localhost", 9092));5String bootstrapServer = container.getBootstrapServers();6String zookeeperServer = container.getZookeeperConnect();7List<Integer> exposedPorts = container.getExposedPorts();8List<Integer> mappedPorts = container.getMappedPort(9092);9List<Integer> exposedHostPorts = container.getExposedHostPorts(9092);10List<Integer> mappedHostPorts = container.getMappedHostPort(9092);11container.stop();12KafkaContainer container = new KafkaContainer()13 .withEmbeddedZookeeper()14 .withExposedPorts(9092)15 .withBrokerAdvertisedListener(new ListenerAddress("PLAINTEXT", "localhost", 9092));16String bootstrapServer = container.getBootstrapServers();17String zookeeperServer = container.getZookeeperConnect();18List<Integer> exposedPorts = container.getExposedPorts();19List<Integer> mappedPorts = container.getMappedPort(9092);20List<Integer> exposedHostPorts = container.getExposedHostPorts(9092);21List<Integer> mappedHostPorts = container.getMappedHostPort(9092);22container.stop();23KafkaContainer container = new KafkaContainer()24 .withEmbeddedZookeeper()25 .withExposedPorts(9092)26 .withBrokerAdvertisedListener(new ListenerAddress("PLAINTEXT", "localhost", 9092));27String bootstrapServer = container.getBootstrapServers();28String zookeeperServer = container.getZookeeperConnect();29List<Integer> exposedPorts = container.getExposedPorts();
brokerAdvertisedListener
Using AI Code Generation
1import org.apache.kafka.clients.consumer.ConsumerConfig;2import org.apache.kafka.clients.producer.KafkaProducer;3import org.apache.kafka.clients.producer.ProducerConfig;4import org.apache.kafka.clients.producer.ProducerRecord;5import org.apache.kafka.common.serialization.StringDeserializer;6import org.apache.kafka.common.serialization.StringSerializer;7import org.testcontainers.containers.KafkaContainer;8import org.testcontainers.utility.DockerImageName;9import java.util.Properties;10public class 1 {11 public static void main(String[] args) {12 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"));13 kafka.start();14 String bootstrapServers = kafka.getBootstrapServers();15 String advertisedListener = kafka.getDynamicHostPortMap().get("9092");16 kafka.setBrokerAdvertisedListener(advertisedListener);17 Properties producerProperties = new Properties();18 producerProperties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);19 producerProperties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());20 producerProperties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());21 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);22 ProducerRecord<String, String> producerRecord = new ProducerRecord<>("my_topic", "hello world");23 producer.send(producerRecord);24 producer.flush();25 producer.close();26 Properties consumerProperties = new Properties();27 consumerProperties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);28 consumerProperties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());29 consumerProperties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());30 consumerProperties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "my_group");31 consumerProperties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "
brokerAdvertisedListener
Using AI Code Generation
1package org.testcontainers.containers;2import org.testcontainers.utility.DockerImageName;3import java.util.ArrayList;4import java.util.List;5import java.util.Map;6import java.util.UUID;7import java.util.stream.Collectors;8import org.slf4j.Logger;9import org.slf4j.LoggerFactory;10public class KafkaContainer extends GenericContainer<KafkaContainer> {11 private static final Logger LOGGER = LoggerFactory.getLogger(KafkaContainer.class);12 public static final int KAFKA_PORT = 9092;13 private static final int ZOOKEEPER_PORT = 2181;14 private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("confluentinc/cp-kafka");15 private static final String DEFAULT_KAFKA_VERSION = "5.4.1";16 private static final String INTERNAL_HOST_NAME = "broker";17 private String externalZookeeperConnect;18 private String externalKafkaBootstrapServers;19 private Map<String, String> externalKafkaConfiguration;20 private boolean externalZookeeperConnectSet = false;21 private boolean externalKafkaBootstrapServersSet = false;22 private boolean externalKafkaConfigurationSet = false;23 private boolean internalZookeeperConnectSet = false;24 private boolean internalKafkaBootstrapServersSet = false;25 private boolean internalKafkaConfigurationSet = false;26 private final List<String> networkAliases = new ArrayList<>();27 public KafkaContainer() {28 this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_KAFKA_VERSION));29 }30 public KafkaContainer(final String dockerImageName) {31 this(DockerImageName.parse(dockerImageName));32 }33 public KafkaContainer(final DockerImageName dockerImageName) {34 super(dockerImageName);35 withNetwork(Network.newNetwork());36 withNetworkAliases(INTERNAL_HOST_NAME);37 withEnv("KAFKA_BROKER_ID", "1");38 withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "1");39 withEnv("KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS", "0");40 withEnv("KAFKA_ZOOKEEPER_CONNECT", "zookeeper:" + ZOOKEEPER_PORT);41 withEnv("42 NewTopic topic = new NewTopic("test", 1, (short) 1);43 CreateTopicsResult result = adminClient.createTopics(List.of(topic));44 result.all().get();45 Properties producerProperties = new Properties();46 producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());47 producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());48 producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());49 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);50 ProducerRecord<String, String> record = new ProducerRecord<>("test", "Hello World");51 producer.send(record, (metadata, exception)
brokerAdvertisedListener
Using AI Code Generation
1KafkaContainer container = new KafkaContainer()2 .withEmbeddedZookeeper()3 .withExposedPorts(9092)4 .withBrokerAdvertisedListener(new ListenerAddress("PLAINTEXT", "localhost", 9092));5String bootstrapServer = container.getBootstrapServers();6String zookeeperServer = container.getZookeeperConnect();7List<Integer> exposedPorts = container.getExposedPorts();8List<Integer> mappedPorts = container.getMappedPort(9092);9List<Integer> exposedHostPorts = container.getExposedHostPorts(9092);10List<Integer> mappedHostPorts = container.getMappedHostPort(9092);11container.stop();12KafkaContainer container = new KafkaContainer()13 .withEmbeddedZookeeper()14 .withExposedPorts(9092)15 .withBrokerAdvertisedListener(new ListenerAddress("PLAINTEXT", "localhost", 9092));16String bootstrapServer = container.getBootstrapServers();17String zookeeperServer = container.getZookeeperConnect();18List<Integer> exposedPorts = container.getExposedPorts();19List<Integer> mappedPorts = container.getMappedPort(9092);20List<Integer> exposedHostPorts = container.getExposedHostPorts(9092);21List<Integer> mappedHostPorts = container.getMappedHostPort(9092);22container.stop();23KafkaContainer container = new KafkaContainer()24 .withEmbeddedZookeeper()25 .withExposedPorts(9092)26 .withBrokerAdvertisedListener(new ListenerAddress("PLAINTEXT", "localhost", 9092));27String bootstrapServer = container.getBootstrapServers();28String zookeeperServer = container.getZookeeperConnect();29List<Integer> exposedPorts = container.getExposedPorts();
brokerAdvertisedListener
Using AI Code Generation
1import org.apache.kafka.clients.consumer.ConsumerConfig;2import org.apache.kafka.clients.producer.KafkaProducer;3import org.apache.kafka.clients.producer.ProducerConfig;4import org.apache.kafka.clients.producer.ProducerRecord;5import org.apache.kafka.common.serialization.StringDeserializer;6import org.apache.kafka.common.serialization.StringSerializer;7import org.testcontainers.containers.KafkaContainer;8import org.testcontainers.utility.DockerImageName;9import java.util.Properties;10public class 1 {11 public static void main(String[] args) {12 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"));13 kafka.start();14 String bootstrapServers = kafka.getBootstrapServers();15 String advertisedListener = kafka.getDynamicHostPortMap().get("9092");16 kafka.setBrokerAdvertisedListener(advertisedListener);17 Properties producerProperties = new Properties();18 producerProperties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);19 producerProperties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());20 producerProperties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());21 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);22 ProducerRecord<String, String> producerRecord = new ProducerRecord<>("my_topic", "hello world");23 producer.send(producerRecord);24 producer.flush();25 producer.close();26 Properties consumerProperties = new Properties();27 consumerProperties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);28 consumerProperties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());29 consumerProperties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());30 consumerProperties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "my_group");31 consumerProperties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "32 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());33 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHostPort());34 System.out.println(container.getContainerInfo().getNetworkSettings().getPorts().getBindings().get("9092/tcp").get(0).getHost
brokerAdvertisedListener
Using AI Code Generation
1package com.testcontainers.kafka;2import java.util.HashMap;3import java.util.Map;4import java.util.Properties;5import java.util.concurrent.ExecutionException;6import org.apache.kafka.clients.admin.AdminClient;7import org.apache.kafka.clients.admin.AdminClientConfig;8import org.apache.kafka.clients.admin.CreateTopicsResult;9import org.apache.kafka.clients.admin.NewTopic;10import org.apache.kafka.clients.consumer.ConsumerConfig;11import org.apache.kafka.clients.producer.KafkaProducer;12import org.apache.kafka.clients.producer.ProducerConfig;13import org.apache.kafka.clients.producer.ProducerRecord;14import org.apache.kafka.clients.producer.RecordMetadata;15import org.apache.kafka.common.serialization.StringDeserializer;16import org.apache.kafka.common.serialization.StringSerializer;17import org.junit.jupiter.api.Test;18import org.testcontainers.containers.KafkaContainer;19import org.testcontainers.utility.DockerImageName;20public class KafkaContainerTest {21 public void testKafkaContainer() throws ExecutionException, InterruptedException {22 KafkaContainer kafkaContainer = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.0"));23 kafkaContainer.start();24 Map<String, Object> adminClientConfig = new HashMap<>();25 adminClientConfig.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());26 AdminClient adminClient = AdminClient.create(adminClientConfig);27 NewTopic topic = new NewTopic("test", 1, (short) 1);28 CreateTopicsResult result = adminClient.createTopics(List.of(topic));29 result.all().get();30 Properties producerProperties = new Properties();31 producerProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaContainer.getBootstrapServers());32 producerProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());33 producerProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());34 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);35 ProducerRecord<String, String> record = new ProducerRecord<>("test", "Hello World");36 producer.send(record, (metadata, exception)
brokerAdvertisedListener
Using AI Code Generation
1import org.apache.kafka.clients.consumer.ConsumerConfig;2import org.apache.kafka.clients.producer.KafkaProducer;3import org.apache.kafka.clients.producer.ProducerConfig;4import org.apache.kafka.clients.producer.ProducerRecord;5import org.apache.kafka.common.serialization.StringDeserializer;6import org.apache.kafka.common.serialization.StringSerializer;7import org.testcontainers.containers.KafkaContainer;8import org.testcontainers.utility.DockerImageName;9import java.util.Properties;10public class 1 {11 public static void main(String[] args) {12 KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.5.1"));13 kafka.start();14 String bootstrapServers = kafka.getBootstrapServers();15 String advertisedListener = kafka.getDynamicHostPortMap().get("9092");16 kafka.setBrokerAdvertisedListener(advertisedListener);17 Properties producerProperties = new Properties();18 producerProperties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);19 producerProperties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());20 producerProperties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());21 KafkaProducer<String, String> producer = new KafkaProducer<>(producerProperties);22 ProducerRecord<String, String> producerRecord = new ProducerRecord<>("my_topic", "hello world");23 producer.send(producerRecord);24 producer.flush();25 producer.close();26 Properties consumerProperties = new Properties();27 consumerProperties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);28 consumerProperties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());29 consumerProperties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());30 consumerProperties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "my_group");31 consumerProperties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!