You need to enable JavaScript to run this app.
导航
使用 Java SDK 收发消息
最近更新时间:2024.10.31 16:55:54首次发布时间:2024.06.28 18:54:08

本文以调用 Java SDK 为例,介绍如何通过 SDK 接入 BMQ 实例并收发消息。

准备工作

  • 您应提前安装 Java 环境,包括安装 1.8 或以上版本 JDK,安装 3.5 或以上版本 Maven。具体操作,请参见安装JDK安装Maven
  • 您需要提前在实例所属安全组中放开 9092 端口。具体操作,请参见添加安全组访问规则
  • (可选)您如果需要通过 SASL 用户名和密码进行鉴权,还需提前创建用户并获取密码。具体操作,请参见创建 SASL 用户

添加依赖

pom.xml 文件中添加以下依赖。

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>2.2.0</version>
</dependency>

发送消息

编写并运行BmqProducerDemo.java发送消息。

说明

  • 需要提前获取实例的接入点地址,请参见查看实例详情
  • 此处以PLAINTEXT协议接入点地址连接 BMQ 实例时,无需鉴权。如需了解更多,请参见Java SDK
//在控制台查看对应接入点信息
String server = "xxx.";
//在控制台申请的消息所属Topic
String topic = "this is your topic.";
//测试消息内容
String value = "this is test message value.";
//发送消息条数
int count = 100;
Properties properties = new Properties();
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, server);
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
try {
    for (int i = 0; i < count; i++) {
        RecordMetadata recordMetadata = producer.send(new ProducerRecord<>(topic, value + i++))
                .get(5, TimeUnit.SECONDS);
        logger.info("recordMetadata topic={}, partition={}, offset={}, count = {}.",
                recordMetadata.topic(),
                recordMetadata.partition(),
                recordMetadata.offset(),
                i);
    }
} catch (Throwable e) {
    logger.error("produce error", e);
}
producer.flush();
producer.close();

消费消息

编写并运行BmqConsumerDemo.java消费消息。

说明

  • 需要提前获取实例的接入点地址,请参见查看实例详情
  • 此处以PLAINTEXT协议接入点地址连接 BMQ 实例时,无需鉴权。如需了解更多,请参见Java SDK
//在控制台查看对应接入点信息
String server = "xxx.";
//在控制台申请的消息所属Topic
String topic = "this is your topic.";
//在控制台申请消费消息的consumerGroup
String group = "this is your group.";
//消费offset策略:earliest, latest, none
String offsetReset = "earliest";
Properties properties = new Properties();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, server);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, group);
properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, offsetReset);
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
List<String> topicList = Lists.newArrayList(topic);
consumer.subscribe(topicList);
while (true) {
    try {
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500));
        for (ConsumerRecord<String, String> record : records) {
            logger.info("consumed record, topic={}, partition={}, offset={}, key={}, value={}",
                    record.topic(),
                    record.partition(),
                    record.offset(),
                    record.key(),
                    record.value());
        }
    } catch (Exception e) {
        logger.error("consume error", e);
    }
}