package com.xzx.log.service; 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.OperationAppLog; import com.xzx.log.mapper.OperationAppLogMapper; 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.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.IOException; @Service @Slf4j public class OperationAppLogService { @Autowired private RedisUtil redisUtil; @Autowired private OperationAppLogMapper operationAppLogMapper; /** * 监听队列 * * @param message * @param channel * @throws IOException */ @RabbitListener(queues = {MqConstants.OPERATION_APP_LOG_QUEUE}) public void addQueue(Message message, Channel channel) throws IOException { long deliveryTag = MqUtil.getTag(message); OperationAppLog bean = MqUtil.getBean(message, OperationAppLog.class); boolean success=true; try { operationAppLogMapper.save(bean); }catch (Exception e) { success=false; } if(success) { channel.basicAck(deliveryTag, false); }else { channel.basicNack(deliveryTag, false, false); } } }