public ResponseWorkItem
     (
     long id,
     int externalEntityId,
     WorkItemState workItemState,
     SubmissionPriority submissionPriority,
     byte[] messageBody,
     bool assigned,
     bool isRetry,
     string ownerName,
     ContextIdentifier contextIdentifier
     )
     : base
         (
         id,
         externalEntityId,
         workItemState,
         submissionPriority,
         messageBody,
         assigned,
         isRetry,
         ownerName,
         contextIdentifier
         )
 {
 }
Esempio n. 2
0
 public EmsWorkItem
     (
     decimal id,
     int externalEntityId,
     WorkItemState workItemState,
     SubmissionPriority submissionPriority,
     byte[] messageBody,
     bool assigned,
     bool isRetry,
     string ownerName,
     ContextIdentifier contextIdentifier,
     EmsReaderQueue queue,
     Message message
     )
     : base
         (
         id,
         externalEntityId,
         workItemState,
         submissionPriority,
         messageBody,
         assigned,
         isRetry,
         ownerName,
         contextIdentifier
         )
 {
     this.queue = queue;
     this.message = message;
 }
 public PrioritySlotsIndex
     (
     SubmissionPriority submissionPriority,
     int startIndex,
     int endIndex
     )
 {
     _submissionPriority = submissionPriority;
     StartIndex = startIndex;
     EndIndex = endIndex;
 }
Esempio n. 4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="priority"></param>
        /// <returns>Retrieved work item or null if not found, didn't pass pre-check, etc.</returns>
        private WorkItem GetWorkItemFromQueue(SubmissionPriority priority)
        {
            //TODO: Pay great attention here, now workItemCandidate is an instance field!!! (SD)
            workItemCandidate = null;
            WorkItem workItem = null;
            TextMessage message = null;
            Message msgTest = null;

            #region Get message from queue

            //var transaction = new CommittableTransaction();

            try
            {
                #region Get next job from the queue

                //var 

                //using (DependentTransaction dependentTransaction =
                //    transaction.DependentClone(DependentCloneOption.BlockCommitUntilComplete))
                //{
                //    using (var scope = new TransactionScope(dependentTransaction))
                //    {
                //TODO: (SD) Provide timeout option
                msgTest = queue.ReadNext();

                message = msgTest as TextMessage;

                //    scope.Complete();
                //}

                //dependentTransaction.Complete();

                if (message == null)
                {
                    // if token is equal to null then commit here, as 
                    // consumer will not get to the item anyway.
                    //if (transaction.TransactionInformation.Status == TransactionStatus.Active)
                    //    transaction.Commit();
                    Log.TraceData(Log.Source, TraceEventType.Verbose, 10001, "Null message received because of the timeout. Queue: " + queue.ServerConfig.Url + ":" + queue.QueueConfig.Name);
                }
                else
                {
                    Log.TraceData(Log.Source, TraceEventType.Verbose, 10002, String.Format("Message [CID:{0}|MID:{1}] received from queue {2}:{3}", message.MessageID, message.CorrelationID, queue.ServerConfig.Url,queue.QueueConfig.Name));
                }

                //}

                #endregion

                #region If job is not null create a work item for it

                if (message != null)
                {
                    //utf-8 is a default encoding for ems
                    workItemCandidate = new EmsWorkItem(0, 0, WorkItemState.AvailableForProcessing,
                        SubmissionPriority.Normal, Encoding.UTF8.GetBytes(message.Text), false, false, this.Name,
                        new ContextIdentifier { InternalId = 0, ExternalReference = message.CorrelationID, ExternalId = message.MessageID, ContextGuid = Trace.CorrelationManager.ActivityId },
                        queue, message)
                        {
                            //Transaction = transaction,
                            RetrievedAt = DateTime.Now
                        };

                    //Trace.CorrelationManager.ActivityId = .ContextUid;

                    Log.TraceData(Log.Source, TraceEventType.Start, 0, String.Format("{0} [{1}:{2}]", message.CorrelationID, queue.ServerConfig.Url, queue.QueueConfig.Name));

                    // Set transaction on the work item
                }

                #endregion

                this.FailureExceptionHandler.ResetState();
            }
            catch (Exception ex)
            {
                // Cleanup will never throw.
                if (ex is EMSException) queue.Close();

                try
                {
                    queue.Rollback();
                    // Rollback the commitable transaction
                    //transaction.Rollback(ex);
                }
                finally
                {
                    //transaction.Dispose();
                }

                Log.TraceData(Log.Source,
                    TraceEventType.Error,
                    ProducerMessage.ErrorDuringObtainingTheWorkItem,
                    new ContextualLogEntry
                    {
                        Message =
                            "Exception happened when trying to get item from the message queue (" +
                            queue.ServerConfig.Url + "). " + Environment.NewLine + ex,
                        ContextIdentifier = ((workItemCandidate != null) ? workItemCandidate.ContextIdentifier : new ContextIdentifier())
                    });

                // To review what is the required handling here for us (SD)
                if (ExecutionState == ProcessExecutionState.Running)
                {
                    FailureExceptionType type = this.FailureExceptionHandler.HandleFailure(ex);
                    if (type == FailureExceptionType.NonRecoverable)
                        this.Stop();
                }
                else
                {
                    if (workItemCandidate != null)
                    {
                        Log.TraceData(Log.Source, System.Diagnostics.TraceEventType.Information,
                            ProducerMessage.RetrievedMessageReturnedToTheRetrievalQueue,
                            new ContextualLogEntry
                            {
                                Message = string.Format(
                                    "'{0}': Retrieved Recurrence(Id = {1}) Successfully Saved to the {2} queue",
                                    Name,
                                    workItemCandidate.Id,
                                    ""
                                    ),
                                ContextIdentifier = workItemCandidate.ContextIdentifier
                            });
                        return null;
                    }
                }
            }

            #endregion Get message from queue

            #region Pre-processing checks

            // Convert queue message to work item
            // In case sql broker no need to do (SD)
            if (workItemCandidate != null)
            {
                #region WorkItem Diagnostics

                workItemCandidate.AttachNote("Received from the " + priority + " Queue ");

                #endregion WorkItem Diagnostics

                // TODO: //**SD1 - Provide the check for 

                // No checks done now, see the DB driven implementation for the checks samples (SD)
                // It means as well that we can simply assign item candidate to be our work item
                workItem = workItemCandidate;

                // TODO: (SD) Message body will be the xml retrieved from the sql broker
                workItem.MessageBody = Encoding.UTF8.GetBytes(message.Text);
                //**message.GetObjectProperty
            }

            #endregion Pre-processing checks

            // Return retrieved work item (or null)
            return workItem;
        }
 public PrioritySlotsIndex this[SubmissionPriority priority]
 {
     get
     {
         foreach (PrioritySlotsIndex qwi in this)
         {
             if (qwi.SubmissionPriority == priority)
             {
                 return qwi;
             }
         }
         return null;
     }
     set
     {
         for (int i = 0; i < List.Count; i++)
         {
             if (((PrioritySlotsIndex) List[i]).SubmissionPriority == priority)
             {
                 List[i] = value;
                 return;
             }
         }
         Add(value);
     }
 }
Esempio n. 6
0
        protected virtual bool ReservePrioritySlot
            (
            SubmissionPriority priority
            )
        {
            if (RetrievedItems.Configuration.PrioritySlotCounts[priority].Count == 0)
                return false;
            int timeout = RetrievedItems.Configuration.PrioritySlotCounts[priority].Timeout;

            while (true)
            {
                lock (RetrievedItems.Counters[priority])
                {
                    #region Instrumenting - to be done via perf (SD)

                    int i = RetrievedItems.Counters[priority].ItemsPresentCount;
                    int j = RetrievedItems.Counters[priority].Value;
                    int k = RetrievedItems.Configuration.PrioritySlotCounts[priority].Count;

                    //if (Tools.Instrumentation.Common.InstrumentationManager.Level == InstrumentationLevel.Debug)
                    //{
                    Log.TraceData(Log.Source,
                        TraceEventType.Verbose,
                        20000,
                        "**" + Name + ": Reserve slot attempt (priority:" + priority +
                        ", items retrieved:" + i + ", requested: " + j +
                        ", slots count: " + k);

                    #endregion Instrumenting - to be done via perf mon (SD)

                    if (
                        i // already present
                        + j // requested
                        <
                        k // slots count
                        )
                    {
                        // TODO: Introduce the separate counter for that and
                        // avoid some more locking inside (SD).
                        RetrievedItems.Counters[priority].SyncIncrement();

                        #region Instrumenting

                        Log.TraceData(Log.Source,TraceEventType.Verbose,
                                             20000, "**" + Name + ": Reserve attempt succeeded.");

                        #endregion Instrumenting

                        return true;
                    }
                    if (!Monitor.Wait
                             (
                             RetrievedItems.Counters[priority],
                             timeout
                             ))
                    {
                        #region Instrumenting

                        Log.TraceData(Log.Source,TraceEventType.Verbose,
                                             20000, "**" + Name + ": Reserve attempt failed after waiting for, ms " + timeout);

                        #endregion Instrumenting

                        return false;
                    }
                }
            }
        }
Esempio n. 7
0
        protected virtual void CancelPrioritySlotReservation(SubmissionPriority priority)
        {
            lock (RetrievedItems.Counters[priority])
            {
                // TODO: Introduce the separate counter for that and
                // avoid some more locking inside (SD).

                #region Instrumenting

                //if (Tools.Instrumentation.Common.InstrumentationManager.Level == InstrumentationLevel.Debug)
                //{
                Log.TraceData(Log.Source,TraceEventType.Verbose,
                                     20000, "**" + Name + ": Cancelling slot reservation");
                //}

                #endregion Instrumenting

                RetrievedItems.Counters[priority].SyncDecrement();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="priority"></param>
        /// <returns>Retrieved work item or null if not found, didn't pass pre-check, etc.</returns>
        private WorkItem GetWorkItemFromQueue(SubmissionPriority priority)
        {
            //TODO: Pay great attention here, now workItemCandidate is an instance field!!! (SD)
            workItemCandidate = null;
            WorkItem workItem = null;
            Tools.Commands.Implementation.IF1.req item = null;

            Trace.CorrelationManager.ActivityId = Guid.NewGuid();


            #region Get message from queue

            var transaction = new CommittableTransaction();

            try
            {
                #region Get next job from the queue

                //var 

                using (DependentTransaction dependentTransaction =
                    transaction.DependentClone(DependentCloneOption.BlockCommitUntilComplete))
                {
                    using (var scope = new TransactionScope(dependentTransaction))
                    {
                        //TODO: (SD) Provide timeout option
                        item = new Tools.Commands.Implementation.IF1.req
                        {
                            reqId = (++IdSequence).ToString(),
                            processingStatus = "P",
                            errorDesc = "ok",
                            returnValue = "ok",
                            updateMechanism = "JMS"
                        }; 
                        
                        scope.Complete();
                    }

                    dependentTransaction.Complete();

                    if (item == null)
                    {
                        // if token is equal to null then commit here, as 
                        // consumer will not get to the item anyway.
                        if (transaction.TransactionInformation.Status == TransactionStatus.Active)
                            transaction.Commit();
                    }
                }

                #endregion

                #region If job is not null create a work item for it

                if (item != null)
                {
                    workItemCandidate = new RequestWorkItem(0, 0, WorkItemState.AvailableForProcessing,
                        SubmissionPriority.Normal, Encoding.UTF8.GetBytes(SerializationUtility.Serialize2String(item)), false, false, this.Name,
                        new ContextIdentifier { InternalId = 0, ExternalReference = IdSequence.ToString(), ExternalId = IdSequence.ToString() })
                        {
                            Transaction = transaction,
                            RetrievedAt = DateTime.Now
                        };

                    //**Trace.CorrelationManager.ActivityId = .ContextUid;
                    Log.Source.TraceEvent(TraceEventType.Start, 0, "Received the item " + item);

                    // Set transaction on the work item
                }

                #endregion

                this.FailureExceptionHandler.ResetState();
            }
            catch (Exception ex)
            {

                try
                {

                    // Rollback the commitable transaction
                    transaction.Rollback(ex);
                }
                finally
                {
                    transaction.Dispose();
                }

                Log.TraceData(Log.Source,
                    TraceEventType.Error,
                    ProducerMessage.ErrorDuringObtainingTheWorkItem,
                    new ContextualLogEntry
                    {
                        Message =
                            "Exception happened when trying to get item " + Environment.NewLine + ex,
                        ContextIdentifier = ((workItemCandidate != null) ? workItemCandidate.ContextIdentifier : new ContextIdentifier())
                    });

                // To review what is the required handling here for us (SD)
                if (ExecutionState == ProcessExecutionState.Running)
                {
                    FailureExceptionType type = this.FailureExceptionHandler.HandleFailure(ex);
                    if (type == FailureExceptionType.NonRecoverable)
                        this.Stop();
                }
                else
                {
                    if (workItemCandidate != null)
                    {
                        Log.TraceData(Log.Source, System.Diagnostics.TraceEventType.Information,
                            ProducerMessage.RetrievedMessageReturnedToTheRetrievalQueue,
                            new ContextualLogEntry
                            {
                                Message = string.Format(
                                    "'{0}': Retrieved Recurrence(Id = {1}) Successfully Saved to the {2} queue",
                                    Name,
                                    workItemCandidate.Id,
                                    ""
                                    ),
                                ContextIdentifier = workItemCandidate.ContextIdentifier
                            });
                        return null;
                    }
                }
            }

            #endregion Get message from queue

            #region Pre-processing checks

            // Convert queue message to work item
            // In case sql broker no need to do (SD)
            if (workItemCandidate != null)
            {
                #region WorkItem Diagnostics

                workItemCandidate.AttachNote("Received from the " + priority + " Queue ");

                #endregion WorkItem Diagnostics

                // TODO: //**SD1 - Provide the check for 

                // No checks done now, see the DB driven implementation for the checks samples (SD)
                // It means as well that we can simply assign item candidate to be our work item
                workItem = workItemCandidate;

                // TODO: (SD) Message body will be the xml retrieved from the sql broker
                //**message.GetObjectProperty
            }

            #endregion Pre-processing checks

            // Return retrieved work item (or null)
            return workItem;
        }
Esempio n. 9
0
 public static WorkItemSlot Create(SubmissionPriority priority)
 {
     return new WorkItemSlot(priority);
 }
Esempio n. 10
0
 protected WorkItemSlot(SubmissionPriority priority)
 {
     _submissionPriority = priority;
 }
Esempio n. 11
0
        protected WorkItem
            (
            decimal id,
            int externalEntityId,
            WorkItemState workItemState,
            SubmissionPriority submissionPriority,
            byte[] messageBody,
            bool assigned,
            bool isRetry,
            string ownerName,
            ContextIdentifier contextIdentifier
            )
        {
            Id = id;
            ExternalEntityId = externalEntityId;
            _workItemState = workItemState;
            _submissionPriority = submissionPriority;
            _messageBody = messageBody;
            Assigned = assigned;
            IsRetry = isRetry;
            OwnerName = ownerName;
            _contextIdentifier = contextIdentifier;
//			_processCode = processCode;
//			_assignmentToken = assignmentToken;
        }
 public PriorityWorkItemsRequestedCounter this[SubmissionPriority priority]
 {
     get
     {
         foreach (PriorityWorkItemsRequestedCounter qwi in this)
         {
             if (qwi.SubmissionPriority == priority)
             {
                 return qwi;
             }
         }
         return null;
     }
     set
     {
         for (int i = 0; i < List.Count; i++)
         {
             if (((PriorityWorkItemsRequestedCounter) List[i]).SubmissionPriority == priority)
             {
                 List[i] = value;
                 return;
             }
         }
         Add(value);
     }
 }
 public PriorityWorkItemsRequestedCounter(SubmissionPriority submissionPriority)
     : this()
 {
     _submissionPriority = submissionPriority;
 }