MessageBatchReceiver.java
4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cn.com.glsx.entmicro.kafka;
import com.alibaba.fastjson.JSON;
import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author
*/
@Slf4j
@Setter
@Getter
public abstract class MessageBatchReceiver {
protected ConsumerConfig consumerConfig;
protected String topic;
protected String nThreads;
protected int consumerCount = 100;
/**
* 最大消费间隔时间
*/
protected int maxIntervalSeconds;
protected kafka.javaapi.consumer.ConsumerConnector connector;
protected Map<String, Integer> topicCountMap = new HashMap<>();
public abstract void processMessage(List<byte[]> messageList) throws Exception;
protected void init() {
connector = Consumer.createJavaConsumerConnector(consumerConfig);
int threadCnt = 1;
if (null != nThreads && Integer.parseInt(nThreads) > 0) {
threadCnt = Integer.parseInt(nThreads);
}
String[] topics = topic.split(",");
for (int i = 0; i < topics.length; i++) {
topicCountMap.put(topics[i], threadCnt);
}
final Map<String, List<KafkaStream<byte[], byte[]>>> streams = connector.createMessageStreams(topicCountMap);
List<KafkaStream<byte[], byte[]>> list = new ArrayList<>();
for (String ss : topics) {
list.addAll(streams.get(ss));
}
final ExecutorService executor = Executors.newFixedThreadPool(threadCnt * topics.length);
for (final KafkaStream<byte[], byte[]> kafkaStream : list) {
executor.submit(() -> {
ConsumerIterator<byte[], byte[]> it = kafkaStream.iterator();
List<byte[]> streamList = new ArrayList<>();
Long starttime = System.currentTimeMillis();
while (it.hasNext()) {
byte[] message = it.next().message();
try {
// 接收到流并读取,转化为消息内容
streamList.add(message);
// 批量消费kafka中的数据
Long endtime = System.currentTimeMillis();
Long time = endtime - starttime;
log.info("时长{}", time);
if (streamList.size() % consumerCount == 0 || time > 1000 * 60) {
starttime = endtime;
log.info("批量消费{}", streamList.size());
processMessage(streamList);
log.info("set before " + streamList.toString());
streamList.clear();
log.info("set after" + streamList.toString());
}
} catch (Exception e) {
e.printStackTrace();
log.error(JSON.toJSONString(e));
log.error(e.getMessage());
}
}
});
}
Runtime.getRuntime().addShutdownHook(new Thread(executor::shutdown));
}
public void destroy() {
if (null != connector)
connector.shutdown();
}
public ConsumerConfig getConsumerConfig() {
return consumerConfig;
}
public void setConsumerConfig(ConsumerConfig consumerConfig) {
this.consumerConfig = consumerConfig;
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic.trim().replaceAll(" ", "");
}
public String getnThreads() {
return nThreads;
}
public void setnThreads(String nThreads) {
this.nThreads = nThreads;
}
public int getConsumerCount() {
return consumerCount;
}
public void setConsumerCount(int consumerCount) {
this.consumerCount = consumerCount;
}
public int getMaxIntervalSeconds() {
return maxIntervalSeconds;
}
public void setMaxIntervalSeconds(int maxIntervalSeconds) {
this.maxIntervalSeconds = maxIntervalSeconds;
}
}