简易 Kafka 消息队列系统(Java)
1️⃣ 核心思路
Topic(主题):用来存储消息的队列。
Broker:管理多个 Topic,负责接收消息和分发给消费者。
Producer(生产者):发送消息到指定 Topic。
Consumer(消费者):从 Topic 拉取消息消费。
多线程安全:使用 BlockingQueue 来保证消息队列的线程安全。
2️⃣ Broker 实现
import java.util.concurrent.*;
import java.util.*;
public class SimpleBroker {
private Map<String, BlockingQueue<String>> topics = new ConcurrentHashMap<>();
// 创建主题
public void createTopic(String topicName) {
topics.putIfAbsent(topicName, new LinkedBlockingQueue<>());
System.out.println("Topic created: " + topicName);
}
// 发送消息
public void send(String topicName, String message) {
BlockingQueue<String> queue = topics.get(topicName);
if (queue != null) {
queue.offer(message);
System.out.println("Message sent to " + topicName + ": " + message);
} else {
System.out.println("Topic does not exist: " + topicName);
}
}
// 拉取消息
public String poll(String topicName) throws InterruptedException {
BlockingQueue<String> queue = topics.get(topicName);
if (queue != null) {
return queue.take(); // 阻塞直到有消息
}
return null;
}
}
3️⃣ Producer 实现
public class SimpleProducer {
private SimpleBroker broker;
public SimpleProducer(SimpleBroker broker) {
this.broker = broker;
}
public void send(String topic, String message) {
broker.send(topic, message);
}
}
4️⃣ Consumer 实现
public class SimpleConsumer implements Runnable {
private SimpleBroker broker;
private String topic;
public SimpleConsumer(SimpleBroker broker, String topic) {
this.broker = broker;
this.topic = topic;
}
@Override
public void run() {
try {
while (true) {
String msg = broker.poll(topic);
System.out.println(Thread.currentThread().getName() + " consumed from " + topic + ": " + msg);
}
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " stopped.");
}
}
}
5️⃣ 测试系统
public class Main {
public static void main(String[] args) throws InterruptedException {
SimpleBroker broker = new SimpleBroker();
broker.createTopic("test-topic");
SimpleProducer producer = new SimpleProducer(broker);
// 创建两个消费者
Thread consumer1 = new Thread(new SimpleConsumer(broker, "test-topic"), "Consumer-1");
Thread consumer2 = new Thread(new SimpleConsumer(broker, "test-topic"), "Consumer-2");
consumer1.start();
consumer2.start();
// 生产消息
for (int i = 1; i <= 10; i++) {
producer.send("test-topic", "message-" + i);
Thread.sleep(500); // 模拟生产速度
}
// 等待几秒
Thread.sleep(5000);
// 停止消费者
consumer1.interrupt();
consumer2.interrupt();
}
}
6️⃣ 功能说明
主题管理
可以创建多个 Topic,每个 Topic 有自己的消息队列。
线程安全
使用 BlockingQueue,支持多生产者、多消费者。
消费者阻塞等待消息
take() 方法会在队列为空时阻塞,类似 Kafka 消费者拉取机制。
简单分发
每个消费者都可以独立消费消息。
如果需要 Kafka 式的消费者组,可以在每个 Topic 中维护消费者偏移量(可以扩展)。
https://vk.com/club238613049
https://vk.com/topic-238613049_61620133
https://vk.com/topic-238613049_61620133
https://vk.com/topic-238613049_61660416
https://vk.com/topic-238613049_61661015
https://vk.com/topic-238613049_61662370
https://vk.com/topic-238613049_61663127
https://vk.com/topic-238613049_61663727
https://vk.com/topic-238613049_61664344
https://vk.com/topic-238613049_61665403
https://vk.com/topic-238613049_61666396
https://vk.com/topic-238613049_61666859
https://vk.com/topic-238613049_61667466
https://vk.com/topic-238613049_61667973
https://vk.com/topic-238613049_61668424
https://vk.com/topic-238613049_61668843
https://vk.com/topic-238613049_61669431
https://vk.com/topic-238613049_61669923
https://vk.com/topic-238613049_61670528
https://vk.com/topic-238613049_61671056
https://vk.com/topic-238613049_61672040
https://vk.com/topic-238613049_61673949
https://vk.com/topic-238613049_61674520
https://vk.com/topic-238613049_61675099
https://vk.com/topic-238613049_61675713
https://vk.com/topic-238613049_61676862
https://vk.com/topic-238613049_61677472
https://vk.com/topic-238613049_61678068
https://vk.com/topic-238613049_61678633
https://vk.com/topic-238613049_61679471
https://vk.com/topic-238613049_61680104
https://vk.com/topic-238613049_61680977
https://vk.com/topic-238613049_61682733
https://vk.com/topic-238613049_61683900
https://vk.com/topic-238613049_61685288
https://vk.com/topic-238613049_61687400
https://vk.com/topic-238613049_61688091
https://vk.com/topic-238613049_61688892
https://vk.com/topic-238613049_61689625
https://vk.com/topic-238613049_61692163
https://vk.com/topic-238613049_61694912
https://vk.com/topic-238613049_61696022
https://vk.com/topic-238613049_61697404
https://vk.com/topic-238613049_61699759
https://vk.com/topic-238613049_61700290
https://vk.com/topic-238613049_61703158
https://vk.com/topic-238613049_61710898
https://vk.com/topic-238613049_61712349
https://vk.com/topic-238613049_61715121
https://vk.com/topic-238613049_61716645
https://vk.com/topic-238613049_61718235
https://vk.com/topic-238613049_61719514
https://vk.com/topic-238613049_61719966
https://vk.com/topic-238613049_61720951
https://vk.com/topic-238613049_61723091
https://vk.com/topic-238613049_61723610
https://vk.com/topic-238613049_61724095
https://vk.com/topic-238613049_61725830
https://vk.com/topic-238613049_61727910
https://vk.com/topic-238613049_61728369
https://vk.com/topic-238613049_61734596
https://vk.com/topic-238613049_61735204
https://vk.com/topic-238613049_61735918
https://vk.com/topic-238613049_61738211
https://vk.com/topic-238613049_61742986
https://vk.com/topic-238613049_61743758
https://vk.com/topic-238613049_61744456
https://vk.com/topic-238613049_61745154
https://vk.com/topic-238613049_61746305
https://vk.com/topic-238613049_61747520
https://vk.com/topic-238613049_61749937
https://vk.com/topic-238613049_61753913
https://vk.com/topic-238613049_61754322
https://vk.com/topic-238613049_61755170
https://vk.com/topic-238613049_61758587
https://vk.com/topic-238613049_61759748
https://vk.com/topic-238613049_61760124
https://vk.com/topic-238613049_61760703
https://vk.com/topic-238613049_61761029
https://vk.com/topic-238613049_61762073
https://vk.com/topic-238613049_61764542
https://vk.com/topic-238613049_61765991
https://vk.com/topic-238613049_61766666
https://vk.com/topic-238613049_61767428
https://vk.com/topic-238613049_61768459
https://vk.com/topic-238613049_61770494
https://vk.com/topic-238613049_61772768
https://vk.com/topic-238613049_61773220
https://vk.com/topic-238613049_61773366
https://vk.com/topic-238613049_61773872
https://vk.com/topic-238613049_61774188
https://vk.com/topic-238613049_61774912
https://vk.com/topic-238613049_61775489
https://vk.com/topic-238613049_61777185
https://vk.com/topic-238613049_61777359
https://vk.com/topic-238613049_61778096
https://vk.com/topic-238613049_61779337
https://vk.com/topic-238613049_61779962
https://vk.com/topic-238613049_61780146
https://vk.com/topic-238613049_61780848
https://vk.com/topic-238613049_61781349
https://vk.com/topic-238613049_61781714
https://vk.com/topic-238613049_61784007
https://vk.com/topic-238613049_61784224
https://vk.com/topic-238613049_61785616
https://vk.com/topic-238613049_61787896
https://vk.com/topic-238613049_61791261
https://vk.com/topic-238613049_61792829
https://vk.com/topic-238613049_61796205
https://vk.com/topic-238613049_61796572
https://vk.com/topic-238613049_61797600
https://vk.com/topic-238613049_61798992
https://vk.com/topic-238613049_61799871
https://vk.com/topic-238613049_61800137
https://vk.com/topic-238613049_61800423
https://vk.com/topic-238613049_61802707
https://vk.com/topic-238613049_61803481
https://vk.com/topic-238613049_61805679
https://vk.com/topic-238613049_61811434
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70050070/viewspace-3117740/
,如需转载,请注明出处,否则将追究法律责任。