Example #1
0
        public override bool HandlePeekedMessage(IMsmqTransport transport, OpenedQueue queue, Message message)
        {
            using (var tx = transactionStrategy.Begin())
            {
                var processMessageAt = DateTime.FromBinary(BitConverter.ToInt64(message.Extension, 16));
                if (CurrentTime >= processMessageAt)
                {
                    return(false);
                }

                string id;
                if (queueStrategy.TryMoveMessage(queue, message, SubQueue.Timeout, out id) == false)
                {
                    logger.DebugFormat("Failed to move message to timeout queue");
                    return(false);
                }
                tx.Complete();

                logger.DebugFormat("Moving message {0} to timeout queue, will be processed at: {1}",
                                   id, processMessageAt);

                timeoutMessageIds.Write(writer => writer.Add(processMessageAt, id));

                return(true);
            }
        }
Example #2
0
 private void RemoveAllReadyToWorkMessages()
 {
     using (var tx = _transactionStrategy.Begin())
         using (var readyForWorkQueue = MsmqUtil.GetQueuePath(Endpoint).Open(QueueAccessMode.SendAndReceive))
             using (var enumerator = readyForWorkQueue.GetMessageEnumerator2())
             {
                 try
                 {
                     while (enumerator.MoveNext())
                     {
                         while (
                             enumerator.Current != null &&
                             enumerator.Current.Label == typeof(ReadyToWork).FullName)
                         {
                             var current = enumerator.TransactionalRemoveCurrent();
                             HandleLoadBalancerMessage(readyForWorkQueue, current);
                         }
                     }
                 }
                 catch (MessageQueueException e)
                 {
                     if (e.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
                     {
                         throw;
                     }
                 }
                 readyForWork.Clear();
                 tx.Complete();
             }
 }
Example #3
0
 public void CreateDatabase()
 {
     try
     {
         _transaction.Begin();
         _transaction.Commit();
     }
     catch (Exception ex)
     {
         Log.Error("PL-E-CREATEDB, ", ex);
         _transaction.Rollback();
     }
 }
Example #4
0
        public void ReceiveMessageInTransaction(OpenedQueue queue, string messageId, Func <CurrentMessageInformation, bool> messageArrived, Action <CurrentMessageInformation, Exception> messageProcessingCompleted, Action <CurrentMessageInformation> beforeMessageTransactionCommit, Action <CurrentMessageInformation> beforeMessageTransactionRollback)
        {
            using (var tx = _transactionStrategy.Begin())
            {
                var message = queue.TryGetMessageFromQueue(messageId);

                if (message == null)
                {
                    return;// someone else got our message, better luck next time
                }
                ProcessMessage(message, queue, tx, messageArrived, beforeMessageTransactionCommit, beforeMessageTransactionRollback, messageProcessingCompleted);
            }
        }
Example #5
0
        private void ReceiveMessage(object context)
        {
            while (shouldContinue)
            {
                try
                {
                    queueManager.Peek(queueName);
                }
                catch (TimeoutException)
                {
                    logger.DebugFormat("Could not find a message on {0} during the timeout period",
                                       endpoint);
                    continue;
                }
                catch (ObjectDisposedException)
                {
                    logger.DebugFormat("Shutting down the transport for {0} thread {1}", endpoint, context);
                    return;
                }
                catch (EsentErrorException e)
                {
                    // we were shut down, so just return
                    if (e.Error == JET_err.TermInProgress)
                    {
                        logger.DebugFormat("Shutting down the transport for {0} thread {1}", endpoint, context);
                    }
                    else
                    {
                        logger.Error("An error occured while recieving a message, shutting down message processing thread", e);
                    }
                    return;
                }

                if (shouldContinue == false)
                {
                    return;
                }

                using (var tx = transactionStrategy.Begin())
                {
                    Message message;
                    try
                    {
                        message = queueManager.Receive(queueName, TimeSpan.FromSeconds(1));
                    }
                    catch (TimeoutException)
                    {
                        logger.DebugFormat("Could not find a message on {0} during the timeout period",
                                           endpoint);
                        continue;
                    }
                    catch (EsentErrorException e)
                    {
                        // we were shut down, so just return
                        if (e.Error == JET_err.TermInProgress)
                        {
                            return;
                        }
                        logger.Error("An error occured while recieving a message, shutting down message processing thread", e);
                        return;
                    }
                    catch (Exception e)
                    {
                        logger.Error("An error occured while recieving a message, shutting down message processing thread", e);
                        return;
                    }

                    try
                    {
                        var msgType = (MessageType)Enum.Parse(typeof(MessageType), message.Headers["type"]);
                        logger.DebugFormat("Starting to handle message {0} of type {1} on {2}",
                                           message.Id,
                                           msgType,
                                           endpoint);
                        switch (msgType)
                        {
                        case MessageType.AdministrativeMessageMarker:
                            ProcessMessage(message, tx,
                                           AdministrativeMessageArrived,
                                           AdministrativeMessageProcessingCompleted,
                                           null,
                                           null);
                            break;

                        case MessageType.ShutDownMessageMarker:
                            //ignoring this one
                            tx.Complete();
                            break;

                        case MessageType.TimeoutMessageMarker:
                            var timeToSend = XmlConvert.ToDateTime(message.Headers["time-to-send"], XmlDateTimeSerializationMode.Utc);
                            if (timeToSend > DateTime.Now)
                            {
                                timeout.Register(message);
                                queue.MoveTo(SubQueue.Timeout.ToString(), message);
                                tx.Complete();
                            }
                            else
                            {
                                ProcessMessage(message, tx,
                                               MessageArrived,
                                               MessageProcessingCompleted,
                                               BeforeMessageTransactionCommit,
                                               BeforeMessageTransactionRollback);
                            }
                            break;

                        default:
                            ProcessMessage(message, tx,
                                           MessageArrived,
                                           MessageProcessingCompleted,
                                           BeforeMessageTransactionCommit,
                                           BeforeMessageTransactionRollback);
                            break;
                        }
                    }
                    catch (Exception exception)
                    {
                        logger.Debug("Could not process message", exception);
                    }
                }
            }
        }