mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring boot kafka 示例
This commit is contained in:
@@ -22,4 +22,13 @@ public class Demo06Producer {
|
||||
return kafkaTemplate.send(Demo06Message.TOPIC, message).get();
|
||||
}
|
||||
|
||||
public SendResult syncSendOrderly(Integer id) throws ExecutionException, InterruptedException {
|
||||
// 创建 Demo01Message 消息
|
||||
Demo06Message message = new Demo06Message();
|
||||
message.setId(id);
|
||||
// 同步发送消息
|
||||
// 因为我们使用 String 的方式序列化 key ,所以需要将 id 转换成 String
|
||||
return kafkaTemplate.send(Demo06Message.TOPIC, String.valueOf(id), message).get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,9 +24,23 @@ public class Demo06ProducerTest {
|
||||
|
||||
@Test
|
||||
public void testSyncSend() throws ExecutionException, InterruptedException {
|
||||
int id = (int) (System.currentTimeMillis() / 1000);
|
||||
SendResult result = producer.syncSend(id);
|
||||
logger.info("[testSyncSend][发送编号:[{}] 发送结果:[{}]]", id, result);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int id = (int) (System.currentTimeMillis() / 1000);
|
||||
SendResult result = producer.syncSend(id);
|
||||
// logger.info("[testSyncSend][发送编号:[{}] 发送结果:[{}]]", id, result);
|
||||
}
|
||||
|
||||
// 阻塞等待,保证消费
|
||||
new CountDownLatch(1).await();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSyncSendOrderly() throws ExecutionException, InterruptedException {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int id = 1;
|
||||
SendResult result = producer.syncSendOrderly(id);
|
||||
logger.info("[testSyncSend][发送编号:[{}] 发送队列:[{}]]", id, result.getRecordMetadata().partition());
|
||||
}
|
||||
|
||||
// 阻塞等待,保证消费
|
||||
new CountDownLatch(1).await();
|
||||
|
||||
Reference in New Issue
Block a user