/// <summary>
        /// Gets the queue listener configurations.
        /// </summary>
        /// <returns></returns>
        internal static List <QueueListenerConfiguration> GetQueueListenerConfigurations()
        {
            var output = new List <QueueListenerConfiguration>();
            var config = new QueueListenerConfiguration();

            var processor = ConfigurationManager.AppSettings["processor"].ToString();

            if (processor.Equals("ClientNotification"))
            {
                config.ConnectionString       = ConfigurationManager.ConnectionStrings["clientNotificationConnectionString"].ToString();
                config.QueueName              = ConfigurationManager.AppSettings["clientNotificationQueueName"].ToString();
                config.Threads                = 1;
                config.EnlistMessageProcessor = false;
                config.MessageProcessor       = ClientNotificationMessageProcessor.ProcessMessage;
                config.FailedMessageProcessor = ClientNotificationMessageProcessor.SaveFailedMessage;
            }
            else
            {
                config.ConnectionString       = ConfigurationManager.ConnectionStrings["getStudyIdsConnectionString"].ToString();
                config.QueueName              = ConfigurationManager.AppSettings["getStudyIdsQueueName"].ToString();
                config.Threads                = 1;
                config.EnlistMessageProcessor = false;
                config.MessageProcessor       = StudyIdsMessageProcessor.ProcessMessage;
                config.FailedMessageProcessor = StudyIdsMessageProcessor.SaveFailedMessage;
            }

            output.Add(config);
            return(output);
        }
Пример #2
0
        /// <summary>
        /// Checks for messages; if a message is found, then it is processed.
        /// </summary>
        /// <param name="queueListenerConfig">The queue listener config.</param>
        public static void ListenerThreadProc(object queueListenerConfig)
        {
            QueueListenerConfiguration config = (QueueListenerConfiguration)queueListenerConfig;

            while (!stopping)
            {
                TransactionOptions to = new TransactionOptions();
                to.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                to.Timeout        = TimeSpan.MaxValue;

                CommittableTransaction tran = new CommittableTransaction(to);

                // try to get a message; if one is found, process it.
                try
                {
                    using (var con = new SqlConnection(config.ConnectionString))
                    {
                        // get message
                        con.Open();
                        con.EnlistTransaction(tran);
                        var    timeOutInSeconds = 10.0;
                        byte[] message          = ServiceBrokerUtils.GetMessage(config.QueueName, con, TimeSpan.FromSeconds(timeOutInSeconds));
                        if (message == null)
                        {
                            tran.Commit();
                            con.Close();
                            continue;
                        }

                        // process message
                        try
                        {
                            if (config.EnlistMessageProcessor)
                            {
                                using (var ts = new TransactionScope(tran))
                                {
                                    config.MessageProcessor(message);
                                    ts.Complete();
                                }
                            }
                            else
                            {
                                config.MessageProcessor(message);
                            }
                        }
                        catch (SqlException ex)
                        {
                            config.FailedMessageProcessor(message, con, ex);
                        }

                        // the message processing succeeded or the FailedMessageProcessor ran so commit the RECEIVE
                        tran.Commit();
                        con.Close();
                    }
                }
                catch (SqlException ex)
                {
                    Trace.WriteLine("Unexpected Exception in Thread Proc for " + config.QueueName + ".  Thread Proc is exiting: " + ex.Message);
                    tran.Rollback();
                    tran.Dispose();
                    return;
                }
            }
        }