Files
TKDataSave/src/main/java/com/yupi/springbootinit/rabbitMQ/MQSender.java

49 lines
1.8 KiB
Java
Raw Normal View History

2025-06-12 19:44:47 +08:00
package com.yupi.springbootinit.rabbitMQ;
import cn.hutool.core.date.DateTime;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.entity.NewHosts;
2025-06-24 18:07:12 +08:00
import com.yupi.springbootinit.model.entity.ServerBigBrother;
2025-06-12 19:44:47 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
2025-06-24 18:07:12 +08:00
import org.springframework.scheduling.annotation.Async;
2025-06-12 19:44:47 +08:00
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
2025-06-12 19:44:47 +08:00
import java.util.List;
@Slf4j
@Service
public class MQSender {
@Resource
private RabbitTemplate rabbitTemplate;
//方法:发送消息
2025-06-24 18:07:12 +08:00
public void hostsSend(List<NewHosts> list){
2025-06-12 19:44:47 +08:00
try {
// log.info("{} 接收到的消息数量----------->{}", DateTime.now(),list.size());
2025-06-12 19:44:47 +08:00
this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//指定你队列的名字
rabbitTemplate.convertAndSend("HOST_INFO_QUEUE",list);
}catch (Exception e){
throw new BusinessException(ErrorCode.QUEUE_ERROR);
}
2025-06-24 18:07:12 +08:00
}
//方法:发送消息
public void bigBrotherSend(ServerBigBrother bigBrothers){
try {
// log.info("{} 接收到的消息数量----------->{}", DateTime.now(),list.size());
// this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
2025-06-24 18:07:12 +08:00
//指定你队列的名字
rabbitTemplate.convertAndSend("BIG_BROTHER_QUEUE",bigBrothers);
}catch (Exception e){
log.error(e.getMessage() );
2025-06-24 18:07:12 +08:00
throw new BusinessException(ErrorCode.QUEUE_ERROR);
}
2025-06-12 19:44:47 +08:00
}
}