Ejemplo n.º 1
0
        //public Task<CallbackMessageEventArgs<ViaElement>> RequestAsync(string serviceName, string exchangeType, string queryName, object queryData, string responceHandlerName, object responseHandlerData, MessageEntryParam parameters)
        //{
        //    var responseHandlerDataStr = responseHandlerData?.ToJson(true);
        //    return RequestAsync(serviceName, exchangeType, queryName, queryData, responceHandlerName, responseHandlerDataStr, null, parameters);
        //}

        public Task <DataArgs <ViaElement> > RequestAsync <T>(string serviceName, string exchangeType, string queryName, DataArgs <T> queryData, string responceHandlerName, string responseHandlerData, MessageEntryParam parameters)
        {
            return(RequestAsync(serviceName, exchangeType, queryName, queryData, responceHandlerName, responseHandlerData, null, parameters));
        }
Ejemplo n.º 2
0
        private Task <DataArgs <ViaElement> > RequestAsync <T>(string serviceName, string exchangeType, string queryName, DataArgs <T> queryData, string responceHandlerName, string responseHandlerData, Action <string> callback, MessageEntryParam parameters)
        {
            var responseResolve = parameters?.NeedResponseResolve ?? false;

            //TODO: подумать ExchangeTypes.EXCHANGETYPE_DIRECT для responceHandlers?
            var responseKind = callback == null ? ExchangeTypes.EXCHANGETYPE_FANOUT : ExchangeTypes.EXCHANGETYPE_DIRECT;

            var replayTo      = responseResolve ? Dispatcher.SelfServiceName : Dispatcher.GetConnectionByExchangeType(responseKind);
            var queryDataJson = queryData.ToJson();

            //записываем  текущее состояние запроса
            var currentViaElement = new ViaElement()
            {
                replyTo             = replayTo,
                mqWorkKind          = responseKind,
                messageId           = Guid.NewGuid().ToString(),
                queryHandlerName    = queryName,
                appId               = Dispatcher.AppId,
                priority            = parameters?.Priority ?? 0,
                doResolve           = responseResolve,
                responseHandlerName = responceHandlerName,
                responseHandlerData = responseHandlerData
            };

            this._via.queue.Push(currentViaElement);

            var properties = Dispatcher.Connection.CreateChannelProperties();

            properties.CorrelationId = ReceivedMessage?.Properties?.CorrelationId ?? parameters?.TransactionId ?? Guid.NewGuid().ToString();
            properties.ContentType   = currentViaElement.queryHandlerName;
            properties.MessageId     = currentViaElement.messageId;
            properties.ReplyTo       = currentViaElement.replyTo;
            properties.Priority      = currentViaElement.priority == 0 ? (byte)1 : currentViaElement.priority;
            properties.Headers       = new Dictionary <string, object>();
            properties.Headers.TryAdd(MessageBasicPropertiesHeaders.VIA, this._via.ToJson());
            properties.Headers.TryAdd(MessageBasicPropertiesHeaders.DIRECTION, MessageBasicPropertiesHeaders.DIRECTION_VALUE_REQUEST);
            properties.Headers.TryAdd(MessageBasicPropertiesHeaders.WORKKIND, responseKind);
            properties.Headers.TryAdd(MessageBasicPropertiesHeaders.WORKKIND_TYPE, MessageBasicPropertiesHeaders.WORKKIND_TYPE_VALUE);
            properties.Headers.TryAdd(MessageBasicPropertiesHeaders.VIA_TYPE, MessageBasicPropertiesHeaders.VIA_TYPE_VALUE);

            exchangeType = ExchangeTypes.Get(exchangeType);

            var link = LinkTypes.GetLinkByExchangeType(exchangeType);

            var routingKey = string.Empty;

            if (exchangeType == ExchangeTypes.EXCHANGETYPE_FANOUT)
            {
                routingKey = serviceName;
            }
            else if (responseKind == ExchangeTypes.EXCHANGETYPE_DIRECT)
            {
                routingKey = parameters?.AppId;
                if (string.IsNullOrEmpty(routingKey))
                {
                    throw new CoreException("Relation [Direct to Direct] - AppId must be initialized");
                }
            }

            var exchangeName = exchangeType == ExchangeTypes.EXCHANGETYPE_FANOUT ? "" : serviceName;

            return(Push(currentViaElement, parameters?.NeedRequestResolve ?? true, link, serviceName, exchangeName, routingKey, exchangeType, queryDataJson, properties)
                   .ContinueWith((result) =>
            {
                if (callback != null)
                {
                    if (result.Exception != null)
                    {
                        callback(new DataArgs <object>(result.Exception).ToJson());
                    }
                    else
                    {
                        Dispatcher.DeclareResponseCallback(currentViaElement.messageId, callback, parameters?.Timeout);
                    }
                }
                return result.Result;
            }));
        }
Ejemplo n.º 3
0
 public Task <DataArgs <ViaElement> > RequestAsync <T>(string serviceName, string exchangeType, string queryName, DataArgs <T> queryData, Action <string> callback, MessageEntryParam parameters)
 {
     return(RequestAsync(serviceName, exchangeType, queryName, queryData, null, null, callback, parameters));
 }