package com.xzx.log.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.rabbitmq.client.Channel; import com.xzx.log.constants.Constants; import com.xzx.log.constants.MqConstants; import com.xzx.log.constants.QueueEnum; import com.xzx.log.entity.IntefaceAdminLog; import com.xzx.log.mapper.IntefaceAdminLogMapper; import com.xzx.log.util.ExceptionUtils; import com.xzx.log.util.MqUtil; import com.xzx.log.util.RedisUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; import java.util.Date; import java.util.List; @Service @Slf4j public class IntefaceAdminLogService { @Autowired private IntefaceAdminLogMapper intefaceAdminLogMapper; @Autowired private MailService mailService; /** * 监听队列 * @param message * @param channel * @throws IOException */ @RabbitListener(queues = {MqConstants.INTEFACE_ADMIN_LOG_QUEUE}) public void addQueue(Message message, Channel channel) throws IOException { log.debug("监听到接口消息队列"); long deliveryTag =MqUtil.getTag(message); IntefaceAdminLog bean = MqUtil.getBean(message, IntefaceAdminLog.class); boolean success=true; try { intefaceAdminLogMapper.save(bean); mailService.sendEmail(CollUtil.newArrayList(bean)); }catch (Exception e) { success=false; } if(success) { channel.basicAck(deliveryTag, false); }else { log.debug("丢弃接口消息"); channel.basicNack(deliveryTag, false, false); } } }