Java public abstract class ByteArrayAbstractKafkaConsumer{ private String clientNumber; // 需要bean类实现的 public abstract KafkaConsumerConfig getConfig(); public abstract String topic(); public abstract void handler(CustomerRecords<String, byte[]> var1); // 内部提供的 public String clientId(String clientNumber)// 标示clientID,根据业务设置 public void handler(){this.handler("01")} // 提供外部接口的使用 public handler(String clientNumber){ // 配置以及实行相关 KafkaConsumerConfig kafkaConfig = this.getKafkaConfig(); if (kafkaConfig == null) { this.logger.error("abstract kafka consumer,consumer fail,kafka config is null"); } else { // 配置读取的相关信息 this.clientNumber = clientNumber; Duration timeout = Duration.ofMillis(kafkaConfig.getPollTimeout() <= 0 ? 500L : (long)kafkaConfig.getPollTimeout()); String topic = this.topic(); Properties properties = new Properties(); properties.put("bootstrap.servers", kafkaConfig.getBootstrapServers()); properties.put("group.id", kafkaConfig.getGroupId()); properties.put("key.deserializer", StringUtils.defaultString(kafkaConfig.getKeyDeserializer(), "org.apache.kafka.common.serialization.StringDeserializer")); properties.put("value.deserializer", "org.apache.kafka.common.serialization.ByteArrayDeserializer"); properties.put("enable.auto.commit", "true"); properties.put("auto.commit.interval.ms", "1000"); properties.put("auto.offset.reset", "latest"); properties.put("session.timeout.ms", "30000"); properties.put("max.poll.interval.ms", "300000"); if (StringUtils.isNotBlank(kafkaConfig.getPartitionAssignmentStrategy())) { properties.put("partition.assignment.strategy", kafkaConfig.getPartitionAssignmentStrategy()); }
String clientId = this.clientId(clientNumber); properties.setProperty("client.id", clientId); List<String> subTopic = Arrays.asList(topic); Thread thread = new Thread(() -> { this.consumer(properties, subTopic, timeout, 0); // 实际的执行 }); thread.setName("kafka-consumer-thread-" + clientNumber + "_" + topic); thread.start(); } } private void consumer(Properties properties, Liat<String> subTopic, Duration time, int retryCount){ MDC.put("code", "0"); try { KafkaConsumer<String, byte[]> consumer = new KafkaConsumer(properties); Throwable var6 = null;
try { consumer.subscribe(subTopic); this.logger.info("abstract kafka consumer,consumer start,clientId:{},kafka config:{}", this.clientId(this.clientNumber), JSON.toJSONString(this.getKafkaConfig())); while(true) { while(true) { try { ConsumerRecords<String, byte[]> records = consumer.poll(timeout); if (records != null && records.count() != 0) { this.handler(records); } } catch (Throwable var16) { AlarmLogger.error("abstract kafka consumer,consumer fail,topic:{},error message:{}", new Object[]{JSON.toJSONString(subTopic), var16.getMessage(), var16}); } } } } catch (Throwable var17) { var6 = var17; throw var17; } finally { if (consumer != null) { if (var6 != null) { try { consumer.close(); } catch (Throwable var15) { var6.addSuppressed(var15); } } else { consumer.close(); } } } } catch (Throwable var19) { if (retryCount >= 10) { AlarmLogger.error("system error,abstract kafka consumer,consumer fail,retry count is too long,shutdown current kafka consumer,properties:{},topic:{},retryCount:{},error message:{}", new Object[]{JSON.toJSONString(properties), JSON.toJSONString(subTopic), retryCount, var19.getMessage(), var19}); } else { AlarmLogger.error("abstract kafka consumer,consumer fail,topic:{},retryCount:{},error message:{}", new Object[]{JSON.toJSONString(subTopic), retryCount, var19.getMessage(), var19}); ++retryCount; this.consumer(properties, subTopic, timeout, retryCount); } } } } |