Exemplo n.º 1
0
        /// <summary>
        /// 交换链接信息
        /// </summary>
        /// <param name="routingKey">路由关键字</param>
        /// <param name="queueName">队列名称</param>
        /// <param name="message">消息内容</param>
        public static void SendRabbitMQ(string routingKey, string queueName, string message)
        {
            RabbitProducerConfig _rabbitConfig = new RabbitProducerConfig()
            {
                ServerAddress = _serverAddress,
                VirtualHost   = _virtualHost,
                UserName      = _userName,
                Password      = _password,
                Exchange      = "erp.service",
                ExchangeType  = "direct",
                RoutingKey    = routingKey
            };

            if (_rabbitProducer == null || !_rabbitProducer.IsOpen)
            {
                _rabbitProducer = new RabbitProducer(_rabbitConfig);
            }
            try
            {
                _rabbitProducer.ProduceMessage(message, queueName);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
            }
            finally
            {
                _rabbitProducer.Close();
            }
        }
Exemplo n.º 2
0
 private IConnection conn; //建立联接
 public RabbitProducer(RabbitProducerConfig Rbinfo)
 {
     RBSendinfo = Rbinfo;
     cf         = new ConnectionFactory()
     {
         UserName           = RBSendinfo.UserName,
         Password           = RBSendinfo.Password,
         VirtualHost        = RBSendinfo.VirtualHost,
         RequestedHeartbeat = 0,
         Uri = RBSendinfo.ServerAddress
     };
     conn = cf.CreateConnection();
 }
Exemplo n.º 3
0
 /// <summary>
 /// 初始化Rabbit连接,此方法只在测试时使用
 /// </summary>
 /// <param name="rbinfo"></param>
 public RabbitProducer(string username, string password, string virtualhost, string serveraddress, string exchange, string exchangetype, string routingkey)
 {
     cf = new ConnectionFactory()
     {
         UserName           = username,
         Password           = password,
         VirtualHost        = virtualhost,
         RequestedHeartbeat = 0,
         Uri = serveraddress
     };
     conn       = cf.CreateConnection();
     RBSendinfo = new RabbitProducerConfig()
     {
         UserName      = username,
         Password      = password,
         VirtualHost   = virtualhost,
         ServerAddress = serveraddress,
         Exchange      = exchange,
         ExchangeType  = exchangetype,
         RoutingKey    = routingkey
     };
 }