其他分享
首页 > 其他分享> > spingboot整合RabbitMQ

spingboot整合RabbitMQ

作者:互联网

SpirngBoot(2.16)自动配置RabbitMQ

对于springboot 1.x版本

springboot2.16版本

@Component
public class SendEmailConsumer {

    private static final Logger logger = LoggerFactory.getLogger(SendEmailConsumer.class);

    private final SendInnerUtil sendInnerUtil;

    @Autowired
    public SendEmailConsumer(SendInnerUtil sendInnerUtil) {
        this.sendInnerUtil = sendInnerUtil;
    }

    /*** 消费者处理接收消息方法
     * ****重要说明*****
     *  如果生产者是以convertSendAndReceive方法发送,则一定要手动给予返回,处理完后加入下面这一行:
     *  ack-true处理:channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
     *               参数说明-------消息id,fasle代表不批量处理(批量是指将消息id小于当前id的都处理掉)
     *  ack-false处理:channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
     *               参数说明-------消息id,fasle代表不批量处理(批量是指将消息id小于当前id的都处理掉),第二个false表示不重新入队(重新入队用true)
     *  拒绝消息:channel.basicReject(message.getMessageProperties().getDeliveryTag(), false); 消息不会重新入队
     *              参数说明-------消息id,fasle表示不重新入队(重新入队用true)
     *
     *  异常情况下,如果不手动做ack-false处理,则该消息会一直认为没有被消费掉,会一直占用rabbitmq内存空间,时间一久,必然造成内存溢出,切记!!!
     */
    @RabbitListener(queues = RabbitMQConstant.SEND_EMAIL_QUEUE)
    public void sendEmailHandler(RabbitParams params, Message message, Channel channel) {
        try {
            // 统一配置的自动确认,所以不需要手动应答
            logger.info("发送邮件消费者开始执行-------->" + params);
            // 发送邮件
            String template = "InternalServerErrorTemplate";
            AbstractContext context = new Context();
            context.setVariable("contactName", params.getContactName());
            context.setVariable("type", params.getType());
            context.setVariable("dbName", params.getDbName());
            context.setVariable("ip", params.getIp());
            context.setVariable("port", params.getPort());
            context.setVariable("itemName", params.getItemName());
            context.setVariable("threshold", params.getThreshold());
            context.setVariable("value", params.getValue());
            context.setVariable("occurredTime", getDate());
            sendInnerUtil.sendTemplateEmail(params.getEmail(), MonitorConstant.SEND_EMAIL_SUBJECT, template, context);
        } catch (Exception e) {
            logger.error("发送邮件失败=======>" + e.toString(), e);
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException io) {
                io.printStackTrace();
            }
        }
    }

    public String getDate() {
        Calendar calendar = Calendar.getInstance();
        return " " + calendar.get(Calendar.YEAR) + "年" +
                " " + (calendar.get(Calendar.MONTH) + 1) + "月" +
                " " + calendar.get(Calendar.DAY_OF_MONTH) + "日" +
                " " + calendar.get(Calendar.HOUR_OF_DAY) + "时" +
                " " + calendar.get(Calendar.MINUTE) + "分" +
                " " + calendar.get(Calendar.SECOND) + "秒";
    }
}

标签:false,整合,spingboot,RabbitMQ,params,context,Calendar,setVariable,calendar
来源: https://blog.csdn.net/weixin_43704975/article/details/98478702