public void PublishBackplaneMessage(IBackplane backplane)
 {
     backplane.Send(new BackplaneMessage()
     {
         Message = "backplane message"
     });
 }
 public CompletionAction Handle(Message <string, string> message)
 {
     backplane.Send(new ActivityRoles {
         Activity = message.Need, Roles = message.Solution
     });
     return(CompletionAction.DoNothing);
 }
        private OrganisationHierarchyCacheItem GetOrganisationTreeItemForOrganisation(Guid organisationId)
        {
            _logger.Trace(OperationGetOrgTree, new LogItem(nameof(organisationId), organisationId.ToString()));

            var queryChildRelationshipsMessage = new Message <object, Relationship>
            {
                Method = QueryChildRelationshipsMethodName,
                Need   = new
                {
                    StartNodeId           = organisationId,
                    Active                = new RelationshipDateRange(_dateTimeProvider.UtcNow, null),
                    LinkRelationshipTypes = new[] { BusinessToBusinessRelationshipType },
                    NodeTypes             = new[] { AssetTypeOrganisation }
                }
            };

            var queryChildRelationshipsResponse = _requestStore.PublishAndWaitForResponse(queryChildRelationshipsMessage, ResponseStyle.WholeSolution, SolutionMatchFunctionForAllMetadataNotNull);

            CheckResponse(queryChildRelationshipsResponse, organisationId);

            var relationshipsForOrganisation = JsonConvert.DeserializeObject <List <Relationship> >(queryChildRelationshipsResponse.Solution);

            OrganisationHierarchyCacheItem organisationTreeItem;

            if (relationshipsForOrganisation.Any())
            {
                organisationTreeItem = ConvertRelationshipsToOrganisationTreeItem(relationshipsForOrganisation, organisationId);
            }
            else
            {
                //no relationships exist, so get the requested organisation and cast it to an organisation tree item so it can be cached.
                var getOrganisationMessage = new Message <Guid, OrganisationHierarchyCacheItem>
                {
                    Method = GetOrganisationMethodName,
                    Need   = organisationId
                };

                var getOrganisationResponse = _requestStore.PublishAndWaitForResponse(getOrganisationMessage, ResponseStyle.FirstOrDefault);

                CheckResponse(getOrganisationResponse, organisationId);

                organisationTreeItem = JsonConvert.DeserializeObject <OrganisationHierarchyCacheItem>(getOrganisationResponse.Solution);
                organisationTreeItem.OrganisationId     = organisationId;
                organisationTreeItem.ChildOrganisations = null;
            }

            if (organisationTreeItem != null)
            {
                //publish result to the backplane to update other caches
                _backplane.Send(organisationTreeItem);
            }
            else
            {
                _logger.Warn(OperationGetOrgTree,
                             new LogItem("Event", "Org tree was null, skipping publish"));
            }


            return(organisationTreeItem);
        }
Beispiel #4
0
        public CompletionAction Handle(Message <TNeed, TSolution> message)
        {
            // If Solution == null and there are no errors, we have nothing to store.
            if (message.Solution == null && !message.Errors.Any())
            {
                return(CompletionAction.DoNothing);
            }

            if (requestStore.Methods.Contains(message.Method))
            {
                // We get here if:
                //   - (there is a solution on the message, or
                //   - there are errors on the message)
                // AND the message.Method has been asked for

                MessageWrapper mw = new MessageWrapper {
                    Uuid         = message.Uuid,
                    SolutionJson = JsonConvert.SerializeObject(message.Solution),
                    ErrorsJson   = JsonConvert.SerializeObject(message.Errors)
                };

                _logger.Info($"{nameof(RequestResponseMessageHandler<TNeed, TSolution>)}.{nameof(Handle)}",
                             new LogItem("Event", "Response received"),
                             new LogItem("MessageId", message.Uuid.ToString),
                             new LogItem("MessageContainsSolution", ((message.Solution?.Length ?? 0) > 0).ToString()),
                             new LogItem("MessageContainerErrors", ((message.Errors?.Count ?? 0) > 0).ToString()));

                // When there is a solution to a message we're interested in,
                // send it on the private exchange
                if (message.Solution != null)
                {
                    mw.FirstOrDefaultJson = JsonConvert.SerializeObject(message.Solution.FirstOrDefault());
                }

                backplane.Send(mw);
            }
            return(CompletionAction.DoNothing);
        }
 public void Register(MessageSubscriber.MethodInspector inspector)
 {
     backplane.Send(inspector);
 }
        public JsonBusResponse PublishAndWaitForResponse <TNeed, TSolution>(Message <TNeed, TSolution> message, ResponseStyle responseStyle = ResponseStyle.WholeSolution,
                                                                            Func <string, bool> solutionMatchFunction = null)
        {
            Guid requestId = CreateUniqueId.Invoke();

            if (message.Uuid == Guid.Empty)
            {
                message.Uuid = requestId;
            }

            message.LastModifiedBy   = ServiceInfo.Name;
            message.LastModifiedTime = dateTimeProvider.UtcNow;

            if (!Methods.Contains(message.Method))
            {
                Methods.Add(message.Method);
                backplane.Send(new RequestMethodRegistration {
                    Method = message.Method
                });
            }

            logger.Trace($"{nameof(RequestStore)}.{nameof(PublishAndWaitForTypedResponse)}",
                         new LogItem("Event", "Preparing to publish message"),
                         new LogItem("MessageId", message.Uuid.ToString),
                         new LogItem("MessageMethod", message.Method));

            var response = new JsonBusResponse();

            using (DataWaitHandle dataWaitHandle = new DataWaitHandle(false, EventResetMode.AutoReset, solutionMatchFunction))
            {
                requestMatcher.RegisterWaitHandle(message.Uuid, dataWaitHandle, responseStyle);

                queueWrapper.PublishMessage(message, message.Uuid);

                if (dataWaitHandle.WaitOne(busTimeoutMilliseconds))
                {
                    List <ResponseError> errors = null;
                    if (dataWaitHandle.Errors != null)
                    {
                        errors = JsonConvert.DeserializeObject <List <ResponseError> >(dataWaitHandle.Errors);
                    }

                    logger.Trace($"{nameof(RequestStore)}.{nameof(PublishAndWaitForTypedResponse)}",
                                 new LogItem("Event", "Published message and received response"),
                                 new LogItem("MessageId", message.Uuid.ToString),
                                 new LogItem("MessageMethod", message.Method));

                    if (errors != null && errors.Any())
                    {
                        response.Errors = dataWaitHandle.Errors;
                    }

                    response.Solution = dataWaitHandle.Solution;
                }

                if (string.IsNullOrEmpty(response.Errors) && string.IsNullOrEmpty(response.Solution))
                {
                    logger.Trace($"{nameof(RequestStore)}.{nameof(PublishAndWaitForTypedResponse)}",
                                 new LogItem("Event", "Published message and received no response"),
                                 new LogItem("MessageId", message.Uuid.ToString),
                                 new LogItem("MessageMethod", message.Method));
                }

                return(response);
            }
        }