53 lines
1.8 KiB
Java
53 lines
1.8 KiB
Java
package com.bwie.auth.consumer;
|
|
|
|
import com.bwie.common.constants.MQQueueNameConstants;
|
|
import com.bwie.common.domain.LoginLog;
|
|
import com.rabbitmq.client.Channel;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.springframework.amqp.core.Message;
|
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* @ClassName:
|
|
* @Description:
|
|
* @Author: zhuwenqiang
|
|
* @Date: 2023/10/25
|
|
*/
|
|
@Component
|
|
@Log4j2
|
|
public class LoginLogQueueConsumer {
|
|
|
|
@Autowired
|
|
private RedisTemplate<String, String> redisTemplate;
|
|
|
|
@RabbitListener(queuesToDeclare = {@Queue(MQQueueNameConstants.LOGIN_LOG_QUEUE_NAME)})
|
|
public void loginLogQueueConsumer(Message message, LoginLog loginLog, Channel channel) {
|
|
log.info("");
|
|
String messageId = message.getMessageProperties().getMessageId();
|
|
try {
|
|
Long add = redisTemplate.opsForSet().add(MQQueueNameConstants.LOGIN_LOG_QUEUE_NAME, messageId);
|
|
if (add == 1) {
|
|
// 正常消费 远程调用
|
|
// 手动 确认
|
|
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
|
log.info("");
|
|
}
|
|
} catch (Exception e) {
|
|
log.error("");
|
|
redisTemplate.opsForSet().remove(MQQueueNameConstants.LOGIN_LOG_QUEUE_NAME, messageId);
|
|
try {
|
|
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
|
|
} catch (IOException ex) {
|
|
log.error("");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|