/// <summary>
        /// Create a Queue that will be used by the Consumer.
        /// </summary>
        /// <param name="queue">Queue to create.</param>
        /// <returns>Instance of the created Queue.</returns>
        private queueType CreateQueue(queueType queue)
        {
            string url = $"{EnvironmentUtils.ParseServiceUrl(Environment, ServiceType.UTILITY, InfrastructureServiceNames.queues)}";
            string body = SerialiseQueue(queue);
            string xml = HttpUtils.PostRequest(url, RegistrationService.AuthorisationToken, body);
            if (log.IsDebugEnabled) log.Debug($"Response from POST {url} request ...");
            if (log.IsDebugEnabled) log.Debug(xml);

            return DeserialiseQueue(xml);
        }
Esempio n. 2
0
        /// <summary>
        /// Create a Queue that will be used by the Consumer.
        /// </summary>
        /// <param name="queue">Queue to create.</param>
        /// <returns>Instance of the created Queue.</returns>
        private queueType CreateQueue(queueType queue)
        {
            string url          = $"{EnvironmentUtils.ParseServiceUrl(Environment, ServiceType.UTILITY, InfrastructureServiceNames.queues)}";
            string requestBody  = SerialiseQueue(queue);
            string responseBody = HttpUtils.PostRequest(
                url,
                RegistrationService.AuthorisationToken,
                requestBody,
                contentTypeOverride: ContentType.ToDescription(),
                acceptOverride: Accept.ToDescription());

            if (log.IsDebugEnabled)
            {
                log.Debug($"Response from POST {url} request ...");
            }
            if (log.IsDebugEnabled)
            {
                log.Debug(responseBody);
            }

            return(DeserialiseQueue(responseBody));
        }
        /// <summary>
        /// <see cref="IEventConsumer.Start(string, string)">Start</see>
        /// </summary>
        public void Start(string zoneId = null, string contextId = null)
        {
            if (log.IsDebugEnabled) log.Debug($"Started Consumer to wait for SIF Events of type {TypeName}.");

            try
            {
                // Register the Event Consumer with the SIF Broker.
                RegistrationService.Register(ref environment);

                // Retrieve the Subscription identifier (if exist).
                string subscriptionId = SessionsManager.ConsumerSessionService.RetrieveSubscriptionId(
                    Environment.ApplicationInfo.ApplicationKey,
                    Environment.SolutionId,
                    Environment.UserToken,
                    Environment.InstanceId);

                // If the Subscription identifier does NOT exist, create a Subscription and associated Queue.
                if (string.IsNullOrWhiteSpace(subscriptionId))
                {

                    // For the SIF Broker, the name property is a mandatory.
                    queueType queue = new queueType
                    {
                        name = $"{TypeName}-event-consumer"
                    };

                    Queue = CreateQueue(queue);

                    subscriptionType subscription = new subscriptionType()
                    {
                        contextId = contextId,
                        queueId = Queue.id,
                        serviceName = $"{TypeName}s",
                        serviceType = ServiceType.OBJECT.ToDescription(),
                        zoneId = zoneId
                    };

                    Subscription = CreateSubscription(subscription);

                    // Store Queue and Subscription identifiers.
                    SessionsManager.ConsumerSessionService.UpdateQueueId(
                        Queue.id,
                        Environment.ApplicationInfo.ApplicationKey,
                        Environment.SolutionId,
                        Environment.UserToken,
                        Environment.InstanceId);

                    SessionsManager.ConsumerSessionService.UpdateSubscriptionId(
                        Subscription.id,
                        Environment.ApplicationInfo.ApplicationKey,
                        Environment.SolutionId,
                        Environment.UserToken,
                        Environment.InstanceId);
                }
                // If the Subscription identifier does exist, retrieve the Queue.
                else
                {

                    try
                    {
                        string queueId = SessionsManager.ConsumerSessionService.RetrieveQueueId(
                            Environment.ApplicationInfo.ApplicationKey,
                            Environment.SolutionId,
                            Environment.UserToken,
                            Environment.InstanceId);

                        Queue = RetrieveQueue(queueId);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = $"Could not retrieve Queue details due to the following error:\n{e.GetBaseException().Message}.";
                        if (log.IsErrorEnabled) log.Error($"{errorMessage}\n{e.StackTrace}");
                        throw e;
                    }

                }

                // Manage SIF Events using background tasks.
                cancellationTokenSource = new CancellationTokenSource();
                CancellationToken cancellationToken = cancellationTokenSource.Token;
                task = Task.Factory.StartNew(
                    () => ProcessEvents(cancellationToken),
                    cancellationToken,
                    TaskCreationOptions.LongRunning,
                    TaskScheduler.Default);
            }
            catch (RegistrationException e)
            {
                string errorMessage = $"Error registering the Event Consumer:\n{e.GetBaseException().Message}.\n{e.StackTrace}";
                if (log.IsErrorEnabled) log.Error(e, errorMessage);
                throw e;
            }
            catch (Exception e)
            {
                string errorMessage = $"Error starting the Event Consumer:\n{e.GetBaseException().Message}.\n{e.StackTrace}";
                if (log.IsErrorEnabled) log.Error(e, errorMessage);
                throw e;
            }

        }
 /// <summary>
 /// Serialise a queueType object.
 /// </summary>
 /// <param name="queue">queueType object.</param>
 /// <returns>XML string representation of the queueType object.</returns>
 private string SerialiseQueue(queueType queue)
 {
     return SerialiserFactory.GetXmlSerialiser<queueType>().Serialise(queue);
 }
Esempio n. 5
0
 /// <summary>
 /// Serialise a queueType object.
 /// </summary>
 /// <param name="queue">queueType object.</param>
 /// <returns>String representation of the queueType object.</returns>
 private string SerialiseQueue(queueType queue)
 {
     return(SerialiserFactory.GetSerialiser <queueType>(ContentType).Serialise(queue));
 }