import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bcat.wechat.official.common.response.ProcessContext;
import com.bcat.wechat.official.common.response.ResponseVo;
import com.bcat.wechat.official.common.response.RespStatusEnum;
import com.bcat.wechat.official.service.api.bo.SendProcessModel;
import com.bcat.wechat.official.service.api.impl.action.framework.BusinessProcess;
import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 发送mq
 *
 * @author shuwenjun
 * @version 2022/7/15
 * @since 1.0
 */
@Slf4j
@Service
public class SendMqAction implements BusinessProcess<SendProcessModel> {

    @Value('wechat.official.topic.name')
    private String sendTopicName;

    @Value('wechat.official.tagId.value')
    private String tagId;

    @Autowired
    private SendMqService sendMqService;

    @Override
    public void process(ProcessContext<SendProcessModel> context) {
        SendProcessModel sendProcessModel = context.getProcessModel();
        try {
            String message = JSON.toJSONString(sendProcessModel.getTaskInfoList(), SerializerFeature.WriteClassName);
            sendMqService.send(sendTopicName, message, tagId);
        } catch (Exception e){
            context.setIsBreak(true).setResponseVo(ResponseVo.fail(RespStatusEnum.MESSAGE_PUSH_FAIL));
            log.error('mq push fail, templateId:{}, taskInfo:{}', sendProcessModel.getMessageTemplateId(),
                    CollUtil.getFirst(sendProcessModel.getTaskInfoList()).toString());
        }

    }
}
import com.bcat.wechat.official.support.constant.MessageQueuePipeline;
import com.bcat.wechat.official.support.mq.SendMqService;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.internals.RecordHeader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

/**
 * <pre>
 * [
 * 调用关系:
 * 实现接口及父类:
 * 子类:
 * 内部类列表:
 * ]
 * </pre>
 *
 * @author shuwenjun
 * @version 2022/10/21
 * @since 1.0
 */
@Slf4j
@Service('sendMqService')
@ConditionalOnProperty(value = 'wechat.official.mq.pipeline', havingValue = MessageQueuePipeline.KAFKA)
public class KafkaMqServiceImpl implements SendMqService {

    @Value('${wechat.official.tagId.key}')
    private String tagIdKey;

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Override
    public void send(String topic, String value) {
        kafkaTemplate.send(topic, value);
    }

    @Override
    public void send(String topic, String value, String tagId) {
        List<Header> headers = Collections.singletonList(new RecordHeader(tagIdKey, tagId.getBytes(StandardCharsets.UTF_8)));
        kafkaTemplate.send(new ProducerRecord(topic, null, null, null, value, headers));
    }
}
Spring Boot 使用Kafka发送消息实战 - 简书

原文地址: https://www.cveoy.top/t/topic/jvd2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录