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());
        }
    
    }
    
    
  • 相关阅读:
    绿色简洁供应商采购后台管理系统模板——后台
    通用的电子商务商城后台管理界面模板——后台
    透明的企业网站卡通后台模板——后台
    绿色的宠物店cms后台管理系统模板——后台
    蓝色的cms企业记账管理后台模板源码——后台
    简洁的响应式博客后台管理模板——后台
    基于bootstrap物资管理系统后台模板——后台
    黑色的网站后台管理系统ui界面——后台
    蓝色的企业后台cms管理系统——后台
    黑色的cms商城网站后台管理模板——后台
  • 原文地址:https://www.cnblogs.com/lovestart/p/11273321.html
Copyright © 2011-2022 走看看