KafkaFactory.java 976 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package com.java110.utils.kafka;
  2. import com.java110.utils.factory.ApplicationContextFactory;
  3. import org.springframework.kafka.core.KafkaTemplate;
  4. /**
  5. * kafka 工厂类
  6. * Created by wuxw on 2018/4/15.
  7. */
  8. public class KafkaFactory {
  9. /**
  10. * 获取kafka template
  11. * @return
  12. */
  13. private static KafkaTemplate getKafkaTemplate(){
  14. return (KafkaTemplate) ApplicationContextFactory.getBean("kafkaTemplate");
  15. }
  16. /**
  17. * 发送kafka消息
  18. * @param topic
  19. * @param key
  20. * @param message
  21. * @throws Exception
  22. */
  23. public static void sendKafkaMessage(String topic,String key,Object message) throws Exception{
  24. getKafkaTemplate().send(topic,key,message);
  25. }
  26. /**
  27. * 发送kafka消息
  28. * @param topic
  29. * @param message
  30. * @throws Exception
  31. */
  32. public static void sendKafkaMessage(String topic,Object message) throws Exception{
  33. getKafkaTemplate().send(topic,"",message);
  34. }
  35. }