使用Spring.NET配置确认

时间:2010-08-16 06:53:54

标签: msmq spring.net

有没有办法用Spring.NET配置Acknowledgements?

在代码中它看起来像这样:

var msgQ = new MessageQueue(OrdersQueueName)
           {
             DefaultPropertiesToSend =
             {
               AcknowledgeType = AcknowledgeTypes.FullReachQueue |
                                 AcknowledgeTypes.FullReceive,
               AdministrationQueue = new MessageQueue(AckQueueName)
             }
           };

是否可以使用ProductTemplate完成?另一种方式?

1 个答案:

答案 0 :(得分:2)

对我来说,这个配置有效:

<objects xmlns="http://www.springframework.net">

  <!-- for http://stackoverflow.com/questions/3491152/configuring-acknowledgements-with-spring-net -->

  <object name="orders" type="q3491152.MessageQueue, q3491152">
    <property name="Name" value="orders-from-xml" />
    <property name="DefaultPropertiesToSend">
      <object type="q3491152.PropertiesToSend, q3491152">
        <property name="AdministrationQueue" ref="anotherQ" />
        <property name="AcknowledgeType" 
                  expression="T(q3491152.AcknowledgeTypes).FullReachQueue or T(q3491152.AcknowledgeTypes).FullReceive" />
      </object>
    </property>
  </object>

  <object name="anotherQ" type="q3491152.MessageQueue, q3491152">
    <property name="Name" value="anotherQ" />
  </object>

</objects>

在这个例子中,我模拟了msmq类的(显然)相关部分。 我使用expression来对枚举进行逐位比较。

如果register the AcknowledgeType Spring.Core.TypeResolution.TypeRegistry使用TypeRegistry.RegisterType("AcknowledgeTypes", typeof(AcknowledgeTypes));,则表达式会进一步简化为:

<property name="AcknowledgeType" 
          expression="AcknowledgeTypes.FullReachQueue or AcknowledgeTypes.FullReceive" />

我使用以下程序对Spring.NET 1.3.1进行了测试:

using System;
using Spring.Context;
using Spring.Context.Support;

namespace q3491152
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            IApplicationContext ctx = new XmlApplicationContext("objects.xml");

            var qFromCode = new MessageQueue
                                {
                                    Name="orders-from-code",
                                    DefaultPropertiesToSend =
                                        {
                                            AcknowledgeType = AcknowledgeTypes.FullReachQueue |
                                                              AcknowledgeTypes.FullReceive,
                                            AdministrationQueue = new MessageQueue()
                                        }
                                };

            var qFromXml = (MessageQueue) ctx.GetObject("orders");

            PrintQ(qFromCode);
            PrintQ(qFromXml);

            Console.ReadLine();
        }

        private static void PrintQ(MessageQueue q)
        {
            Console.WriteLine("Q: {0}; AcknowledgeType: {1}", q.Name, q.DefaultPropertiesToSend.AcknowledgeType);
        }
    }

    public class MessageQueue
    {
        public MessageQueue()
        {
            DefaultPropertiesToSend = new PropertiesToSend();
        }

        public string Name { get; set; }
        public PropertiesToSend DefaultPropertiesToSend { get; set; }
    }


    public class PropertiesToSend
    {
        public AcknowledgeTypes AcknowledgeType { get; set; }
        public MessageQueue AdministrationQueue { get; set; }
    }

    [Flags]
    public enum AcknowledgeTypes
    {
        None = 0,
        FullReachQueue,
        FullReceive
    }
}

哪个输出:

Q: orders-from-code; AcknowledgeType: FullReachQueue, FullReceive
Q: orders-from-xml; AcknowledgeType: FullReachQueue, FullReceive