RabbitMqProducer类说明
路径
com.roma.apic.livedata.client.v1.RabbitMqProducer
说明
生产RabbitMQ消息。若发送消息没有异常,则消息发送成功;若发送消息抛出异常,则消息发送失败。
使用示例
- 用direct交换器生产消息,把消息路由到bindingKey与routingKey完全匹配的Queue中。
    
importClass(com.roma.apic.livedata.client.v1.RabbitMqProducer); importClass(com.roma.apic.livedata.config.v1.RabbitMqConfig); importClass(com.roma.apic.livedata.config.v1.QueueConfig); importClass(com.roma.apic.livedata.config.v1.ExchangeConfig); importClass(com.roma.apic.livedata.config.v1.ConnectionConfig); function execute(data) { var connectionConfig = new ConnectionConfig("10.10.10.10", 5672, "admin", "123456"); var queueConfig = new QueueConfig("directQueue", false, false, false, null); var exchangeConfig = new ExchangeConfig("directExchange", "direct", true, false, false, null); var config = new RabbitMqConfig(connectionConfig, queueConfig, exchangeConfig); var producer = new RabbitMqProducer(config); producer.produceWithDirectExchange("direct.exchange", "PERSISTENT_TEXT_PLAIN", "direct exchange message"); return "produce successful."; } - 用topic交换器生产消息,把消息路由到bindingKey与routingKey模糊匹配的Queue中。
    
importClass(com.roma.apic.livedata.client.v1.RabbitMqProducer); importClass(com.roma.apic.livedata.config.v1.RabbitMqConfig); importClass(com.roma.apic.livedata.config.v1.QueueConfig); importClass(com.roma.apic.livedata.config.v1.ExchangeConfig); importClass(com.roma.apic.livedata.config.v1.ConnectionConfig); function execute(data) { var connectionConfig = new ConnectionConfig("10.10.10.10", 5672, "admin", "123456"); var queueConfig = new QueueConfig(“topicQueue”, false, false, false, null); var exchangeConfig = new ExchangeConfig("topicExchange", "topic", true, false, false, null); var config = new RabbitMqConfig(connectionConfig, queueConfig, exchangeConfig); var producer = new RabbitMqProducer(config); producer.produceWithTopicExchange("topic.#", “topic.A”, null, “message”); return "produce successful."; } - 用fanout交换器生产消息,把所有发送到该Exchange的消息路由到所有与它绑定的Queue中。
    
importClass(com.roma.apic.livedata.client.v1.RabbitMqProducer); importClass(com.roma.apic.livedata.config.v1.RabbitMqConfig); importClass(com.roma.apic.livedata.config.v1.QueueConfig); importClass(com.roma.apic.livedata.config.v1.ExchangeConfig); importClass(com.roma.apic.livedata.config.v1.ConnectionConfig); function execute(data) { var connectionConfig = new ConnectionConfig("10.10.10.10", 5672, "admin", "123456"); var queueConfig = new QueueConfig(“fanoutQueue”, false, false, false, null); var exchangeConfig = new ExchangeConfig(“fanoutExchange”, "fanout", true, false, null); var config = new RabbitMqConfig(connectionConfig, queueConfig, exchangeConfig); var producer = new RabbitMqProducer(config); producer.produceWithFanoutExchange( null, “message”); return "produce successful." } 
构造器详情
public RabbitMqProducer(RabbitMqConfig rabbitMqConfig)
构造一个RabbitMQ消息生产者。
参数:rabbitMqConfig表示RabbitMQ的配置信息。
方法列表
| 
        返回类型  | 
      
        方法和说明  | 
     
|---|---|
| 
        void  | 
      
        produceWithDirectExchange(String routingKey, String props, String message) 用direct交换器生产消息,把消息路由到bindingKey与routingKey完全匹配的Queue中。  | 
     
| 
        void  | 
      
        produceWithTopicExchange(String bindingKey, String routingKey, String props, String message) 用topic交换器生产消息,把消息路由到bindingKey与routingKey模糊匹配的Queue中。  | 
     
| 
        void  | 
      
        produceWithFanoutExchange(String props, String message) 用fanout交换器生产消息,把所有发送到该Exchange的消息路由到所有与它绑定的Queue中。  | 
     
方法详情
- public void produceWithDirectExchange(String routingKey, String props, String message)
    
用direct交换器生产消息,把消息路由到bindingKey与routingKey完全匹配的Queue中。
输入参数
- routingKey:消息路由键
 - props:消息持久化设置,非必填
 - message:消息内容
 
 - public void produceWithTopicExchange(String bindingKey, String routingKey, String props, String message)
    
用topic交换器生产消息,把消息路由到bindingKey与routingKey模糊匹配的Queue中。
输入参数
- bindingKey:队列绑定键
 - routingKey:消息路由键
 - props:消息持久化设置,非必填
 - message:消息内容
 
 - produceWithFanoutExchange(String props, String message)
    
用fanout交换器生产消息,把所有发送到该Exchange的消息路由到所有与它绑定的Queue中。
输入参数
- props:消息持久化设置,非必填
 - message:消息内容