Ejemplo n.º 1
0
        internal static RabbitEndpoint GetEndpointConfig(string endpointName)
        {
            RabbitEndpoint endpoint = null;

            try
            {
                var sql = string.Format("select * from rmq.tb_RabbitEndpoint with(nolock) where AliasName = '{0}'", endpointName);

                using (SqlConnection connection = new SqlConnection("Context Connection = true"))
                {
                    connection.Open();

                    SqlCommand command = new SqlCommand(sql, connection);
                    var        dr      = command.ExecuteReader();

                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            endpoint = new RabbitEndpoint(dr);
                            break;
                        }
                    }
                }

                //SqlContext.Pipe.Send(endpoint.ToString());
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }

            return(endpoint);
        }
Ejemplo n.º 2
0
        internal static void SendMsg(RabbitEndpoint endpoint, string msg)
        {
            var factory = new ConnectionFactory()
            {
                HostName = endpoint.ServerName, Port = endpoint.Port, UserName = endpoint.LoginName, Password = endpoint.LoginPassword
            };

            using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    string exchange     = string.IsNullOrEmpty(endpoint.Exchange) ? "" : endpoint.Exchange;
                    string exchangeType = string.IsNullOrEmpty(endpoint.ExchangeType) ? "" : endpoint.ExchangeType;
                    string routeKey     = string.IsNullOrEmpty(endpoint.RoutingKey) ? "" : endpoint.RoutingKey;
                    string queue        = string.IsNullOrEmpty(endpoint.Queue) ? "" : endpoint.Queue;

                    if (queue != "")
                    {
                        channel.QueueDeclare(queue: endpoint.Queue,
                                             durable: true,
                                             exclusive: false,
                                             autoDelete: false,
                                             arguments: null);
                    }

                    if (exchange != "")
                    {
                        channel.ExchangeDeclare(exchange: exchange, type: exchangeType == "" ? "direct" : exchangeType);
                        channel.QueueBind(queue, exchange, routeKey);
                    }

                    var body = Encoding.GetEncoding("gb2312").GetBytes(msg);
                    channel.BasicPublish(exchange: exchange,
                                         routingKey: routeKey,
                                         basicProperties: null,
                                         body: body);
                }
        }