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; import com.yupi.springbootinit.model.entity.ServerBigBrother; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.LocalDateTime; import java.util.List; @Slf4j @Service public class MQSender { @Resource private RabbitTemplate rabbitTemplate; //方法:发送消息 public void hostsSend(List list){ try { // log.info("{} 接收到的消息数量----------->{}", DateTime.now(),list.size()); this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); //指定你队列的名字 rabbitTemplate.convertAndSend("HOST_INFO_QUEUE",list); }catch (Exception e){ throw new BusinessException(ErrorCode.QUEUE_ERROR); } } //方法:发送消息 public void bigBrotherSend(ServerBigBrother bigBrothers){ try { // log.info("{} 接收到的消息数量----------->{}", DateTime.now(),list.size()); // this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); //指定你队列的名字 rabbitTemplate.convertAndSend("BIG_BROTHER_QUEUE",bigBrothers); }catch (Exception e){ log.error(e.getMessage() ); throw new BusinessException(ErrorCode.QUEUE_ERROR); } } }