配置
// 設定為批次消費,每個批次數量在Kafka配置引數中設定
factory.setBatchListener(true);
type: batch
------------------------
concurrency: 在消費的時候,一共生成n個消費者來消費訊息,類似於多執行緒,但數量一般小於等於分割槽數
public void listenBatch2(ConsumerRecord<String,String> record, Acknowledgment acknowledgment) {
@KafkaListener(topics = "#{kafkaConfig.topicName}")
// public void listen3(ConsumerRecord<String,String> record, Acknowledgment acknowledgment){
// @KafkaListener(topics = "testkafka", concurrency = "3")
public void listenBatch1(List<ConsumerRecord<String, String>> record, Acknowledgment acknowledgment) {
String msg = record.value();
// @KafkaListener(topics = "topic2", concurrency = "3", groupId = "0")
public void listenRealtimeData2(String msg, Acknowledgment acknowledgment) {
// 轉換為物件
JSONArray jsonArray = JSON.parseArray(msg);
XXX xxxList = jsonArray.toJavaList(XXX.class);
ListenableFuture<SendResult<String, String>> send =
kafkaTemplate.send("test-topic", JSONObject.toJSONString(物件));
// send.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
// //傳送訊息成功回撥
// @Override
// public void onSuccess(SendResult<String, String> result) {
//getRecordMetadata裡面存在傳送的topic和partition等資訊
// String topic = result.getRecordMetadata().topic();
// int partition = result.getRecordMetadata().partition();
// long offset = result.getRecordMetadata().offset();
// log.info("傳送訊息成功回撥topic->:" + topic + "-partition=" + partition + "-offset=" + offset);
// }
// //傳送訊息失敗回撥
// @Override
// public void onFailure(Throwable ex) {
// log.info("傳送訊息失敗回撥=:" + ex.getMessage());
// }
// });
@KafkaListener(containerFactory ="firstKafkaListenerContainerFactory", topics = "topic1",
concurrency = "3", groupId = "a8")
public void listenTermAlarmBatch(List<String> msg, Acknowledgment acknowledgment) {
for(String jsonStr : msg) {
JSONArray jsonArray = JSON.parseArray(jsonStr);
XXXX xxxList= jsonArray.toJavaList(XXXX.class);
for (XXXX xxx: xxxList) {
---
根據key,對應處理
// String msg = record.value();
// JSONObject jsMsg = JSON.parseObject(msg);
// for (Map.Entry<String, Object> entry : jsMsg.entrySet()) {
// ProtocolTypeEnum protocolTypeEnum = ProtocolTypeEnum.valueOf(entry.getKey());
// protocolTypeEnum.preHandle(entry.getValue().toString());
// }