Exemplo n.º 1
0
        public MessageEntry(ICoreDispatcher dispatcher, ReceivedMessageEventArgs receivedMessage)
        {
            ReceivedMessage = receivedMessage;

            Dispatcher = dispatcher;

            if (receivedMessage != null)
            {
                _via = receivedMessage.GetVia();

                IsRequest = receivedMessage.GetHeaderValue(MessageBasicPropertiesHeaders.DIRECTION) != MessageBasicPropertiesHeaders.DIRECTION_VALUE_RESPONSE;
                if (_via == null)
                {
                    _via = new ViaContainer()
                    {
                        queue = new Stack <ViaElement>()
                    };

                    if (receivedMessage.Properties != null)
                    {
                        _via.queue.Push(new ViaElement()
                        {
                            appId            = receivedMessage.Properties.AppId,
                            messageId        = receivedMessage.Properties.MessageId,
                            mqWorkKind       = ExchangeTypes.Get(receivedMessage.GetHeaderValue(MessageBasicPropertiesHeaders.WORKKIND)) ?? ExchangeTypes.EXCHANGETYPE_FANOUT,
                            priority         = receivedMessage.Properties.Priority,
                            queryHandlerName = receivedMessage.Properties.ContentType,
                            replyTo          = receivedMessage.Properties.ReplyTo
                        });
                    }
                }
            }
            else
            {
                _via = new ViaContainer()
                {
                    queue = new Stack <ViaElement>()
                };
            }
        }
Exemplo n.º 2
0
 public static string GetLinkByExchangeType(string kind)
 {
     return(ExchangeTypes.Get(kind) == ExchangeTypes.EXCHANGETYPE_DIRECT ? LINK_EXCHANGE : LINK_QUEUE);
 }
Exemplo n.º 3
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;
            }));
        }