|
@@ -0,0 +1,79 @@
|
|
|
+package com.ywt.biz.common.config.kafka;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.kafka.core.KafkaTemplate;
|
|
|
+import org.springframework.kafka.support.SendResult;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.concurrent.FailureCallback;
|
|
|
+import org.springframework.util.concurrent.ListenableFuture;
|
|
|
+import org.springframework.util.concurrent.ListenableFutureCallback;
|
|
|
+import org.springframework.util.concurrent.SuccessCallback;
|
|
|
+
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.TimeoutException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author liyang
|
|
|
+ * @version 1.0
|
|
|
+ * @description: 生产者封装类
|
|
|
+ * @date 2022/9/29 8:50
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+@ConditionalOnProperty(value = "spring.kafka.producer.enable",havingValue = "true")
|
|
|
+public class KafkaAnalyzeProducer {
|
|
|
+ @Autowired
|
|
|
+ private KafkaTemplate<String, Object> kafkaTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步发送数据到kafka 自定义回调
|
|
|
+ *
|
|
|
+ * @param topic topic名称
|
|
|
+ * @param message 发送信息字符串
|
|
|
+ * @param sendCallBack 发送回调
|
|
|
+ */
|
|
|
+ public void send(String topic, String message, SendCallBack sendCallBack) {
|
|
|
+
|
|
|
+ ListenableFuture<SendResult<String, Object>> listenableFuture = kafkaTemplate.send(topic, message);
|
|
|
+ //发送成功后回调
|
|
|
+ SuccessCallback<Object> successCallback = result -> sendCallBack.sendSuccessCallBack(topic, message);
|
|
|
+ //发送失败回调
|
|
|
+ FailureCallback failureCallback = ex -> sendCallBack.sendFailCallBack(topic, message, ex);
|
|
|
+ listenableFuture.addCallback(successCallback, failureCallback);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * producer 同步方式发送数据 10S 超时
|
|
|
+ *
|
|
|
+ * @param topic topic名称
|
|
|
+ * @param message producer发送的数据
|
|
|
+ */
|
|
|
+ public void sendAsynchronize(String topic, String message) throws InterruptedException, ExecutionException, TimeoutException {
|
|
|
+ kafkaTemplate.send(topic, message).get(10, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * producer 异步方式发送数据 通用回调
|
|
|
+ *
|
|
|
+ * @param topic topic名称
|
|
|
+ * @param message producer发送的数据
|
|
|
+ */
|
|
|
+ public void sendSynchronize(String topic, String message) {
|
|
|
+ kafkaTemplate.send(topic, message).addCallback(new ListenableFutureCallback<Object>() {
|
|
|
+ @Override
|
|
|
+ public void onFailure(@NotNull Throwable throwable) {
|
|
|
+ log.error("----事件kafka记录解析完成放入topic:{},发送失败{}", topic, message, throwable);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess(Object o) {
|
|
|
+ log.info("----事件kafka记录解析完成放入topic:{},发送成功:{}", topic, message);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|