zoukankan      html  css  js  c++  java
  • springboot12(rabbitmq)

    RabbitAutoConfiguration

    @Configuration
    @ConditionalOnClass({ RabbitTemplate.class, Channel.class })
    @EnableConfigurationProperties(RabbitProperties.class)
    @Import(RabbitAnnotationDrivenConfiguration.class)
    public class RabbitAutoConfiguration {
    	//配置连接工厂
        @Bean
        public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties properties,
                                                                ObjectProvider<ConnectionNameStrategy> connectionNameStrategy) throws Exception {
            PropertyMapper map = PropertyMapper.get();
            CachingConnectionFactory factory = new CachingConnectionFactory(
                getRabbitConnectionFactoryBean(properties).getObject());
            map.from(properties::determineAddresses).to(factory::setAddresses);
            map.from(properties::isPublisherConfirms).to(factory::setPublisherConfirms);
            map.from(properties::isPublisherReturns).to(factory::setPublisherReturns);
            RabbitProperties.Cache.Channel channel = properties.getCache().getChannel();
            map.from(channel::getSize).whenNonNull().to(factory::setChannelCacheSize);
            map.from(channel::getCheckoutTimeout).whenNonNull().as(Duration::toMillis)
                .to(factory::setChannelCheckoutTimeout);
            RabbitProperties.Cache.Connection connection = properties.getCache().getConnection();
            map.from(connection::getMode).whenNonNull().to(factory::setCacheMode);
            map.from(connection::getSize).whenNonNull().to(factory::setConnectionCacheSize);
            map.from(connectionNameStrategy::getIfUnique).whenNonNull().to(factory::setConnectionNameStrategy);
            return factory;
        }
    
    RabbitProperties.class // 封装了rabbitmq的所有配置
    
    RabbitTemplate // 给rabbitmq发送和接收消息
    

    AmqpAdmin //rabbitmq系统功能管理组件

    消息转换器,在rabbitmqtemplate中默认使用SimpleMessageConverter

    private MessageConverter messageConverter = new SimpleMessageConverter();
    

    测试的代码

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringbootRabbitmqApplicationTests {
    
        @Autowired
        RabbitTemplate rabbitTemplate;
    
        @Test
        public void contextLoads() {
    
            //Object 默认当成消息体
            //rabbitTemplate.convertAndSend(exchange,routeKey,Object);
            Map<String, Object> map = new HashMap<>();
            map.put("msg", "这是java发的第一个消息");
            map.put("data", Arrays.asList("hello", "what is your name?"));
            rabbitTemplate.convertAndSend("exchange.direct", "delay", map);
    
        }
    
        @Test
        public void receive() {
            Object delay = rabbitTemplate.receiveAndConvert("delay");
            System.out.println(delay.getClass());
            System.out.println(delay);
        }
        @Test
        public void fanout(){
    
            rabbitTemplate.convertAndSend("exchange.fanout","",new Book("你是谁?","199"));
    
        }
    }
    
    

    启动类

    @EnableRabbit
    @SpringBootApplication
    public class SpringbootRabbitmqApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootRabbitmqApplication.class, args);
        }
    
    }
    
    

    配置类

    @Configuration
    public class MyAMQPConfig {
    
        @Bean
        public MessageConverter messageConverter(){
            return new Jackson2JsonMessageConverter();
        }
    
    }
    

    service

    @Service
    public class BookService {
    
        @RabbitListener(queues = "delay")
        public void  receive(Book book){
            System.out.println(book.toString());
        }
        @RabbitListener(queues = "delay.news")
        public void receive2(Message message){
            System.out.println(message.getBody());
            System.out.println(message.getMessageProperties());
        }
    
    }
    
    
  • 相关阅读:
    旋转变换(一)旋转矩阵
    DICOM中几个判断图像方向的tag
    RGB与HSB之间的转换公式
    Delphi图像处理 -- RGB与HSL转换
    指针类型(C# 编程指南)
    关于 Delphi 中流的使用(2) 用 TFileStream(文件流) 读写
    [转载]Delphi Tokyo 10.2.3发布了
    如果设置网络优先级
    .gitignore详解
    Win10 兼容性 Visual studio web应用程序 ASP.NET 4.0 尚未在 Web 服务器上注册
  • 原文地址:https://www.cnblogs.com/lovestart/p/11273321.html
Copyright © 2011-2022 走看看