1.延迟接收消息,防止出现线程吃满

This commit is contained in:
2025-07-09 15:57:23 +08:00
parent 7aceb760a2
commit 51de8a63e1
8 changed files with 47 additions and 16 deletions

View File

@@ -0,0 +1,27 @@
package com.yupi.springbootinit.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Slf4j
public class DelayedRabbitMQStarter implements ApplicationRunner {
@Resource
private RabbitListenerEndpointRegistry registry; // Spring 提供的监听器注册表
@Override
public void run(ApplicationArguments args) throws Exception {
// 延迟 5 秒后启动监听器
log.info("延迟5秒启动 rabbitMQ 监听");
Thread.sleep(5000);
registry.getListenerContainer("hosts").start();
registry.getListenerContainer("bigbrother").start();
}
}

View File

@@ -2,10 +2,8 @@ package com.yupi.springbootinit.controller;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.model.entity.NewHosts;
import com.yupi.springbootinit.model.entity.ServerBigBrother;
import com.yupi.springbootinit.rabbitMQ.MQSender;
import com.yupi.springbootinit.service.HostInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

View File

@@ -12,6 +12,7 @@ import com.yupi.springbootinit.service.ServerBigBrotherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@@ -44,7 +45,7 @@ public class MQReceiver {
// }
// }
@RabbitListener(queues = "HOST_INFO_QUEUE")
@RabbitListener(queues = "HOST_INFO_QUEUE",id = "hosts", autoStartup = "false")
@Async("taskExecutor")
public void receive(List<NewHosts> hosts, Channel channel, Message message) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();
@@ -61,7 +62,7 @@ public class MQReceiver {
}
}
@RabbitListener(queues = "BIG_BROTHER_QUEUE")
@RabbitListener(queues = "BIG_BROTHER_QUEUE",id = "bigbrother", autoStartup = "false")
@Async("taskExecutor")
public void bigBrotherReceive(ServerBigBrother bigBrotherList, Channel channel, Message message) throws IOException {
long deliveryTag = message.getMessageProperties().getDeliveryTag();