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.
|
|
package com.bfd.parse.utils;
import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.SendResult; import org.springframework.stereotype.Component; import org.springframework.util.concurrent.ListenableFuture; import org.springframework.util.concurrent.ListenableFutureCallback;
/** * @PROJECT_NAME: companybusinesscrawl * @DESCRIPTION:SpringBootKafka 工具类 * @AUTHOR: ying.zhao * @DATE: 2023/4/6 11:09 */ @Slf4j @Component public class SpringBootKafka { @Autowired private KafkaTemplate<String, Object> kafkaTemplate; /** * 自定义topicKafkaTemplate */ /** * public static final String TOPIC = "companyBussTest"; **/ public void send(String topic, String message) { //发送消息
ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topic, message); future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() { @Override public void onFailure(Throwable throwable) { //发送失败的处理
log.info(topic + " - 生产者 发送消息失败:" + throwable.getMessage()); }
@Override public void onSuccess(SendResult<String, Object> stringObjectSendResult) { //成功的处理
log.info("{} - 生产者 发送消息成功:",topic); } }); } }
|