示例#1
0
 public bool Run(TaskInfo info, Message msg)
 {
     _action();
     return true;
 }
示例#2
0
文件: TaskManager.cs 项目: yaq/yaq
        private void LoadAndExec(TaskInfo ti, int cnt)
        {
            IAsyncMessageQueue service = null;
            ICommunicationObject co = null;

            try
            {
                service = _factory();
                co = service as ICommunicationObject;

                service.BeginGetMessages(ti.Queue, cnt, ti.VisibilitySpan, (ar) =>
                {
                    Message[] msgs = null;
                    try
                    {
                        msgs = service.EndGetMessages(ar);
                        if (co != null) co.Close();
                    }
                    catch (Exception ex)
                    {
                        ExProcessor(ex);
                        if (co != null) co.Abort();
                    }
                    finally
                    {
                        int dec = msgs == null ? -cnt : msgs.Length - cnt;
                        if (dec < 0)
                        {
                            Interlocked.Add(ref ti.m_cnt, dec);
                        }
                        Interlocked.Exchange(ref ti.m_isInLoad, 0);
                    }

                    if (msgs == null) return;

                    foreach (var imsg in msgs)
                    {
                        ThreadPool.QueueUserWorkItem((state) =>
                        {
                            var msg = (Message)state;
                            bool delete;
                            try
                            {
                                delete = ti.Task.Run(ti, msg);
                            }
                            catch (Exception ex)
                            {
                                ExProcessor(ex);
                                delete = true;
                            }

                            Interlocked.Decrement(ref ti.m_cnt);

                            if (delete)
                            {
                                IAsyncMessageQueue srv = null;
                                ICommunicationObject sco = null;

                                try
                                {
                                    srv = _factory();
                                    sco = service as ICommunicationObject;

                                    srv.BeginDeleteMessage(ti.Queue, msg.Id, msg.PopReceipt, (ar2) =>
                                    {
                                        try
                                        {
                                            srv.EndDeleteMessage(ar2);
                                            if (sco != null) sco.Close();
                                        }
                                        catch (Exception ex)
                                        {
                                            ExProcessor(ex);
                                            if (sco != null) sco.Abort();
                                        }
                                    },
                                    null);
                                }
                                catch (Exception ex)
                                {
                                    ExProcessor(ex);
                                    if (sco != null) sco.Abort();
                                }
                            }
                        }, imsg); // pass current msg through the state. we cannot use closures here.
                    }
                }
                , null);
            }
            catch (Exception ex)
            {
                ExProcessor(ex);
                Interlocked.Add(ref ti.m_cnt, -cnt);
                Interlocked.Exchange(ref ti.m_isInLoad, 0);
                if (co != null) co.Abort();
            }
        }