Wednesday, November 3, 2010

spring.net send messages to multiply locations using MessageQueueGatewaySupport

Use Case

My service utilizing MessageQueueGatewaySupport need to send messages to multiply locations (queues) which are not known at design time.

Solution

Prepare generic application context configuration (see post: spring.net dynamic configuration. Configure IApplicationContext from GenericApplicationContext and file stream. for more details).

On runtime, you can programmatically register queues as shown below:


            // Get template
            var template = Context["messageQueueTemplate"] as MessageQueueTemplate;

            template.MessageQueueFactory.RegisterMessageQueue(requestQueueName,
                 delegate
                 {
                       var mq = new MessageQueue();
                       mq.Path = requestQueuePath;
                       return mq;
                 }
            );



            template.ConvertAndSend(requestQueueName, request, delegate(Message message)
            {
                   // do smth with your message
                   return message;
            });

No comments:

Post a Comment