ResendProcessor.java 1.6 KB
package com.tianbo.messagebus.service;


import com.tianbo.messagebus.controller.response.ResultJson;
import com.tianbo.messagebus.model.Cache;
import com.tianbo.messagebus.model.MSGS;
import com.tianbo.messagebus.myinterface.KafkaSendApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * 重发服务
 * 从内存数组队列读取报文,并发送直到发送成功为止,
 * 代表这个程序不能随便重启了,重启会丢失需要重复发的数据
 */

@Service
@Slf4j
public class ResendProcessor {

    private static int TIMES = 20;

    @Autowired
    KafkaSendApi kafkaSendApi;

    public boolean sendmsg(MSGS msgs){
        ResultJson response = kafkaSendApi.send(msgs);

        if ("200".equals(response.getCode())){
            log.info("………………重发消息发送成功{}………………",response.toString());
            return true;
        }
        log.info("400-重发消息发送失败->{}",response.toString());
        return false;
    }

    public void resend(){
        if (!Cache.SEND_CACHE.isEmpty()){
            log.info("开始从内存读取报文,发送失败的报文,待重发数量->{}",Cache.SEND_CACHE.size());
            Cache.SEND_CACHE.removeIf(this::send);
        }
    }

    private boolean send(MSGS msgs){
        for (int j = 0; j < TIMES; j++) {
            if(sendmsg(msgs)) {
                return true;
            }
        }
        return false;
    }
}