feat: 添加Redis做消息队列(二):添加注释与日志

pull/65/head
xiaoxiamo 6 months ago
parent 171f4ba16c
commit 53fea21a44

@ -20,10 +20,8 @@ import java.util.concurrent.TimeUnit;
import java.util.function.Consumer; import java.util.function.Consumer;
/** /**
* Redis
* *
* Guava Eventbus Spring EventBus * Redis
* Redis 便
* *
* @author xiaoxiamao * @author xiaoxiamao
* @date 2024/7/4 * @date 2024/7/4
@ -46,8 +44,11 @@ public class RedisReceiver implements MessageReceiver {
/** /**
* *
*
* @Scheduled() 退
*
*/ */
@Scheduled(fixedDelay = 5000) @Scheduled(fixedDelay = 1000)
public void receiveSendMessage() { public void receiveSendMessage() {
receiveMessage(sendTopic, message -> { receiveMessage(sendTopic, message -> {
List<TaskInfo> taskInfoList = JSON.parseArray(message, TaskInfo.class); List<TaskInfo> taskInfoList = JSON.parseArray(message, TaskInfo.class);
@ -58,7 +59,7 @@ public class RedisReceiver implements MessageReceiver {
/** /**
* *
*/ */
@Scheduled(fixedDelay = 5000) @Scheduled(fixedDelay = 1000)
public void receiveRecallMessage() { public void receiveRecallMessage() {
receiveMessage(recallTopic, message -> { receiveMessage(recallTopic, message -> {
RecallTaskInfo recallTaskInfo = JSON.parseObject(message, RecallTaskInfo.class); RecallTaskInfo recallTaskInfo = JSON.parseObject(message, RecallTaskInfo.class);
@ -69,6 +70,8 @@ public class RedisReceiver implements MessageReceiver {
/** /**
* *
* *
*
*
* @param topic * @param topic
* @param consumer * @param consumer
*/ */
@ -78,12 +81,13 @@ public class RedisReceiver implements MessageReceiver {
// 阻塞操作减少CPUIO消耗 // 阻塞操作减少CPUIO消耗
Optional<String> message = Optional.ofNullable( Optional<String> message = Optional.ofNullable(
stringRedisTemplate.opsForList().rightPop(topic, 0, TimeUnit.SECONDS)); stringRedisTemplate.opsForList().rightPop(topic, 0, TimeUnit.SECONDS));
log.debug("RedisReceiver#receiveMessage Received message from Redis topic {}: {}", topic, message);
if (message.isPresent()) { if (message.isPresent()) {
consumer.accept(message.get()); consumer.accept(message.get());
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Error receiving messages from Redis topic {}: {}", topic, e); log.error("RedisReceiver#receiveMessage Error receiving messages from Redis topic {}: {}", topic, e);
} }
} }
} }

@ -12,6 +12,9 @@ import org.springframework.stereotype.Service;
/** /**
* Redis * Redis
* *
* Guava Eventbus Spring EventBus
* Redis 便
*
* @author xiaoxiamao * @author xiaoxiamao
* @date 2024/7/4 * @date 2024/7/4
*/ */
@ -39,10 +42,11 @@ public class RedisSendMqServiceImpl implements SendMqService {
public void send(String topic, String jsonValue, String tagId) { public void send(String topic, String jsonValue, String tagId) {
// 非业务topic抛错不发送 // 非业务topic抛错不发送
if (!sendTopic.equals(topic) && !recallTopic.equals(topic)) { if (!sendTopic.equals(topic) && !recallTopic.equals(topic)) {
log.error("RedisSendMqServiceImpl#The topic type is not supported! topic:{}, jsonValue:{}, tagId:{}", log.error("RedisSendMqServiceImpl#send The topic type is not supported! topic:{}, jsonValue:{}, tagId:{}",
topic, jsonValue, tagId); topic, jsonValue, tagId);
return; return;
} }
log.debug("RedisSendMqServiceImpl#send topic:{}, jsonValue:{}, tagId:{}", topic, jsonValue, tagId);
stringRedisTemplate.opsForList().leftPush(topic, jsonValue); stringRedisTemplate.opsForList().leftPush(topic, jsonValue);
} }

Loading…
Cancel
Save