• rabbitMQ发送消息端关键代码


    package com.tszr;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.tszr.entity.Weather;
    import org.springframework.amqp.core.Message;
    import org.springframework.amqp.core.MessageBuilder;
    import org.springframework.amqp.core.MessageDeliveryMode;
    import org.springframework.amqp.rabbit.connection.CorrelationData;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Application implements CommandLineRunner {
        @Autowired
        private ObjectMapper objectMapper;
    
        @Autowired
        RabbitTemplate rabbitTemplate;
    
        public static void main(String[] args){
            SpringApplication.run(Application.class,args);
        }
    
        /**
         * 定义发布者
         */
        @Override
        public void run(String... args) throws Exception {
            //定义消息对象
            Weather weather = new Weather();
            weather.setId("010");
            weather.setCity("北京");
            weather.setWeatherDetail("今天晴到多云,南风5-6级,温度19-26°C");
    
            //指定Json转换器,Jackson2JsonMessageConverter默认将消息转换成byte[]类型的消息
            rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
            //objectMapper将weather对象转换为JSON字节数组
            Message msg= MessageBuilder.withBody(objectMapper.writeValueAsBytes(weather))
                    .setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT)
                    .build();
            // 消息唯一ID
            CorrelationData correlationData = new CorrelationData(weather.getId());
            //使用已封装好的convertAndSend(String exchange , String routingKey , Object message, CorrelationData correlationData)
            //将特定的路由key发送消息到指定的交换机
            rabbitTemplate.convertAndSend(
                    "weather-exchange", //分发消息的交换机名称
                    "weather.message", //用来匹配消息的路由Key
                    msg, //消息体
                    correlationData);
        }
    }
  • 相关阅读:
    独立使用 ecj
    公司没有 DBA,Mysql 运维自己来
    mysql安装
    常用模块
    基本数据类型
    Incorrect column name 问题解决
    mysql中date和datetime的区别
    python yield用法详解(未完成)
    mysql报错--initialize specified but the data directory has files in it. Aborting.
    python 列表解析式
  • 原文地址:https://www.cnblogs.com/tszr/p/15990679.html
Copyright © 2020-2023  润新知