文档解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

6 months ago
  1. package com.bfd.parse.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.kafka.core.KafkaTemplate;
  6. import org.springframework.kafka.support.SendResult;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.util.concurrent.ListenableFuture;
  9. import org.springframework.util.concurrent.ListenableFutureCallback;
  10. /**
  11. * @PROJECT_NAME: companybusinesscrawl
  12. * @DESCRIPTION:SpringBootKafka 工具类
  13. * @AUTHOR: ying.zhao
  14. * @DATE: 2023/4/6 11:09
  15. */
  16. @Slf4j
  17. @Component
  18. public class SpringBootKafka {
  19. @Autowired
  20. private KafkaTemplate<String, Object> kafkaTemplate;
  21. /**
  22. * 自定义topicKafkaTemplate
  23. */
  24. /**
  25. * public static final String TOPIC = "companyBussTest";
  26. **/
  27. public void send(String topic, String message) {
  28. //发送消息
  29. ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topic, message);
  30. future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
  31. @Override
  32. public void onFailure(Throwable throwable) {
  33. //发送失败的处理
  34. log.info(topic + " - 生产者 发送消息失败:" + throwable.getMessage());
  35. }
  36. @Override
  37. public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
  38. //成功的处理
  39. log.info("{} - 生产者 发送消息成功:",topic);
  40. }
  41. });
  42. }
  43. }