首页 > 编程语言 >Java实现rabbitMQ向MQ里推送消息

Java实现rabbitMQ向MQ里推送消息

时间:2022-10-26 14:55:55浏览次数:44  
标签:Java getName rabbitMQ parseObject MQ msg exchangeEnum queueEnum

配置文件application.properties里配置rabbitMQ的ip和端口,如果没有配置username和password,就是mq安装时默认的用户名密码:

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/

业务代码:

1     public void send(String msg, ExchangeEnum exchangeEnum, QueueEnum queueEnum, Class msg_class) throws Exception {
2         //绑定交换机和队列
3         this.bindExchangeAndQueue(exchangeEnum, queueEnum);
4 //        Message message = MessageBuilder.withBody(new ObjectMapper().writeValueAsBytes(msg)).build();
5         Object parseObject = JSONObject.parseObject(msg, msg_class);
6         //推送消息
7         rabbitTemplate.convertAndSend(exchangeEnum.getName(), queueEnum.getName(), parseObject);
8     }
    /**
     * 绑定 队列与交换机
     * 
     * @param exchangeEnum
     * @param queueEnum
     */
    private void bindExchangeAndQueue(ExchangeEnum exchangeEnum, QueueEnum queueEnum) {

        amqpAdmin.declareBinding(new Binding(queueEnum.getName(), Binding.DestinationType.QUEUE, exchangeEnum.getName(),
                queueEnum.getName(), null));
    }

 

标签:Java,getName,rabbitMQ,parseObject,MQ,msg,exchangeEnum,queueEnum
From: https://www.cnblogs.com/zeevy/p/16828358.html

相关文章