Esempio n. 1
0
        /// <summary>
        /// Рассылка сообщения
        /// </summary>
        /// <param name="pparam">Параметры сообщения</param>
        /// <param name="content">Сообщение</param>
        /// <param name="customProperties">Дополнительные параметры сообщения. Пример: Channel.CreateBasicProperties();</param>
        public void Publish(ProducerParam pparam, byte[] content, IBasicProperties customProperties)
        {
            try
            {
                var default_durable = Configuration.MQ.durable.HasValue ? Configuration.MQ.durable.Value : false;
                if (pparam?.ExchangeParam != null)
                {
                    //Exchange declare
                    channel.ExchangeDeclare(pparam.ExchangeParam.Name,
                                            pparam.ExchangeParam.Type,
                                            pparam.ExchangeParam.Durable ?? default_durable,
                                            pparam.ExchangeParam.AutoDelete,
                                            pparam.ExchangeParam.Arguments);
                }

                channel.BasicPublish(exchange: pparam?.ExchangeParam?.Name ?? "",
                                     routingKey: pparam?.RoutingKey ?? "",
                                     basicProperties: customProperties,
                                     body: content);

                //Trace.TraceInformation($"PUBLISH exchange:{ pparam?.ExchangeParam?.Name},routingKey:{pparam?.RoutingKey}, data:{Encoding.UTF8.GetString(content)}");
            }
            catch (Exception ex)
            {
                throw new CoreException(ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Опрос оператора
        /// </summary>
        /// <param name="exchange"></param>
        /// <param name="isSelf"></param>
        /// <returns></returns>
        private Task <bool> Send(string exchange, bool isSelf)
        {
            return(Task.Run(() =>
            {
                if (this.Bind)
                {
                    List <object> request = new List <object>();
                    List <TaskCompletionSource <LinkEntry[]> > cancelledTacks = new List <TaskCompletionSource <LinkEntry[]> >();
                    foreach (var p_item in pending)
                    {
                        var pendingElement = p_item.Value;
                        //если объект соответствует типу и еще не запущен
                        if (pendingElement != null && pendingElement.IsSelf == isSelf && !pendingElement.IsSend)
                        {
                            request.Add(pendingElement.Request);
                            cancelledTacks.Add(pendingElement.Context);
                        }
                    }
                    if (request.Any())
                    {
                        try
                        {
                            var data_str = request.ToJson(true);
                            if (data_str != null)
                            {
                                var properties = Connection.CreateChannelProperties();
                                properties.CorrelationId = Guid.NewGuid().ToString();
                                properties.ReplyTo = cfg_starter.responseexchangename;
                                properties.AppId = AppId;
                                properties.MessageId = Guid.NewGuid().ToString();

                                ProducerParam options = new ProducerParam()
                                {
                                    ExchangeParam = new ChannelExchangeParam()
                                    {
                                        Name = exchange,
                                        Type = ExchangeTypes.EXCHANGETYPE_FANOUT
                                    }
                                };
                                //отправляем скопом всех ожидающих процессов в одном запросе
                                Connection.Publish(options, Encoding.UTF8.GetBytes(data_str), properties);

                                //Trace.TraceInformation("Resolver publish->" + data_str);
                            }
                            return true;
                        }
                        catch (Exception exception)
                        {
                            foreach (var taskcs in cancelledTacks)
                            {
                                taskcs.SetException(exception);
                            }
                            throw new CoreException(exception);
                        }
                    }
                }
                return false;
            }));
        }
Esempio n. 3
0
        private void Say(string msg)
        {
            var prod = new ProducerParam();

            //be ignored
            prod.RoutingKey = AppId.CurrentUID;
            var rec_exchangeName = RecName + ".fanout";

            prod.ExchangeParam = new ChannelExchangeParam()
            {
                Name = rec_exchangeName,
                Type = ExchangeTypes.EXCHANGETYPE_FANOUT,
            };
            Trace.TraceInformation($"Say {Name}: {msg}");
            CoreConnection.Publish(prod, Encoding.UTF8.GetBytes(msg), null);
        }
Esempio n. 4
0
 private void Say(string msg)
 {
     try
     {
         var prod = new ProducerParam();
         //be ignored
         prod.RoutingKey = RecID;
         var rec_exchangeName = RecName + ".direct";
         prod.ExchangeParam = new ChannelExchangeParam()
         {
             Name = rec_exchangeName,
             Type = ExchangeTypes.EXCHANGETYPE_DIRECT,
         };
         Trace.TraceInformation($"Say {Name}: {msg}");
         CoreConnection.Publish(prod, Encoding.UTF8.GetBytes(msg), null);
     }
     catch (Exception ex)
     {
         Trace.TraceError(ex.ToString());
     }
 }
Esempio n. 5
0
        private Task <DataArgs <ViaElement> > Push(ViaElement via, bool requestResolve, string link, string service, string exchangeName, string routingKey, string exchangeType, string queryData, IBasicProperties properties)
        {
            DataArgs <ViaElement> messageEventArgs = new DataArgs <ViaElement>();

            if (requestResolve)
            {
                //Для начала надо узнать в какие очереди посылать запросы
                return(this.Dispatcher.Resolve(service, link)
                       .ContinueWith(resolver_link =>
                {
                    //try
                    //{
                    if (resolver_link.Exception != null)
                    {
                        messageEventArgs.SetException(resolver_link.Exception);
                    }
                    var link_str = resolver_link.Result;
                    var exchange = ExchangeTypes.GetExchangeName(link_str, null, exchangeType);
                    bool isFanout = ExchangeType.Fanout.Equals(exchangeType, StringComparison.CurrentCultureIgnoreCase);

                    var rk = isFanout ? exchange : routingKey;

                    var options = new ProducerParam()
                    {
                        RoutingKey = rk
                    };

                    if (!isFanout)
                    {
                        options.ExchangeParam = new ChannelExchangeParam()
                        {
                            Name = exchange,
                            Type = exchangeType ?? ExchangeTypes.EXCHANGETYPE_FANOUT
                        };
                    }

                    var data = Encoding.UTF8.GetBytes(queryData);
                    //посылаем сообщение
                    Dispatcher.Connection.Publish(options, data, properties);
                    messageEventArgs.result = true;
                    messageEventArgs.data = via;
                    //}
                    //catch (Exception ex)
                    //{
                    //    messageEventArgs.SetException(ex);
                    //}
                    return messageEventArgs;
                }));
            }
            else
            {
                return(Task.Run(() =>
                {
                    //try
                    //{
                    //для запросов, где очереди известны
                    var options = new ProducerParam()
                    {
                        RoutingKey = routingKey
                    };
                    if (!string.IsNullOrEmpty(exchangeName))
                    {
                        options.ExchangeParam = new ChannelExchangeParam()
                        {
                            Name = exchangeName,
                            Type = exchangeType ?? ExchangeTypes.EXCHANGETYPE_FANOUT
                        };
                    }

                    var data = Encoding.UTF8.GetBytes(queryData);
                    //посылаем сообщение
                    Dispatcher.Connection.Publish(options, data, properties);
                    //}
                    //catch (Exception ex)
                    //{
                    //    messageEventArgs.result = false;
                    //    messageEventArgs.SetException(ex);
                    //}
                    return messageEventArgs;
                }));
            }
        }