rabbitmq交换机是广播模式fanout获取信息

rabbitmq交换机是广播模式fanout获取信息_第1张图片使用rabbitMQ,通过消费vhost为,exchange为: 模式为广播模式

 1.配置获取信息类RabbitMqUtils

 rabbitmq交换机是广播模式fanout获取信息_第2张图片rabbitmq交换机是广播模式fanout获取信息_第3张图片

 

 

@Slf4j
@Configuration
@RefreshScope
public class RabbitMqUtils {


    @Bean(name = "dateConnectionFactory")
    @Primary
    public ConnectionFactory dateConnectionFactory(
            @Value("${spring.rabbitmq.date.host}") String host,
            @Value("${spring.rabbitmq.date.port}") int port,
            @Value("${spring.rabbitmq.date.username}") String username,
            @Value("${spring.rabbitmq.date.password}") String password,
            @Value("${spring.rabbitmq.date.virtual-host}") String virtualHost
    ) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        return connectionFactory;
    }

    @Bean(name = "dateRabbitTemplate")
    public RabbitTemplate dateRabbitTemplate(
            @Qualifier("dateConnectionFactory") ConnectionFactory connectionFactory
    ) {
        RabbitTemplate dateRabbitTemplate = new RabbitTemplate(connectionFactory);

        return dateRabbitTemplate;
    }

    @Bean(name = "dateFactory")
    public SimpleRabbitListenerContainerFactory dateFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("dateConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
            factory.setConnectionFactory(connectionFactory);
            factory.setMessageConverter(new Jackson2JsonMessageConverter());
            factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(factory, connectionFactory);

        return factory;
    }
    @Bean(name = "dateRabbitAdmin")
    public RabbitAdmin dateRabbitAdmin(
            @Qualifier("dateConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

}

2.获取rabbitmq的信息

rabbitmq交换机是广播模式fanout获取信息_第4张图片

 

@Slf4j
@Component
public class QueryMsgFromMq {
    @Autowired
    private SysDateService sysDateService;
    @RabbitListener(queues = "${rabbitmq.date.test.Queue}", containerFactory = "dateFactory")
    @RabbitListener(  containerFactory = "dateFactory",bindings ={@QueueBinding(
            value =@Queue(value = "${rabbitmq.date.test.Queue}", declare = "true"),
            exchange =@Exchange(value ="${rabbitmq.date.test.Exchange}", type= ExchangeTypes.FANOUT, durable = "true")
    )
    })
    public void onDateMessage(String msg) {
        log.info("接收到日历系统MQ消息:{}", msg);
        if (StringUtils.isNotBlank(msg)) {
                sysDateService.getHttpPost();
        }
    }
}

3.配置

rabbitmq交换机是广播模式fanout获取信息_第5张图片

 

你可能感兴趣的:(微服务,java-rabbitmq,rabbitmq,java)