Configuring Acknowledgements with Spring.NET
Asked Answered
M

1

9

Is there a way to configure Acknowledgements with Spring.NET?

In code it looks like this:

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

Could it be done with the ProductTemplate? Another way?

Mortar answered 16/8, 2010 at 6:53 Comment(0)
P
2

For me, this configuration worked:

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

  <!-- for https://mcmap.net/q/1229934/-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>

In this example I mocked the (apparently) relevant parts of the msmq classes. I use an expression to get a bit-wise comparison on the enum.

If you register the AcknowledgeType with the Spring.Core.TypeResolution.TypeRegistry using TypeRegistry.RegisterType("AcknowledgeTypes", typeof(AcknowledgeTypes));, the expression further simplifies to:

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

I've tested it with the following program, against 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
    }
}

Which outputs:

Q: orders-from-code; AcknowledgeType: FullReachQueue, FullReceive
Q: orders-from-xml; AcknowledgeType: FullReachQueue, FullReceive
Petronel answered 16/8, 2011 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.