Remove DNS lookups of the local hostname in tests (#18059)
[mono-project.git] / mcs / class / Mono.Messaging.RabbitMQ / Mono.Messaging.RabbitMQ / RabbitMQMessagingProvider.cs
blob020520969aa7a7b42b2146c5d6445c5d27c70d87
1 //
2 // Mono.Messaging.RabbitMQ
3 //
4 // Authors:
5 // Michael Barker (mike@middlesoft.co.uk)
6 //
7 // (C) 2008 Michael Barker
8 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.Collections;
33 using System.Net;
34 using System.Net.Sockets;
35 using System.Threading;
37 using Mono.Messaging;
39 using RabbitMQ.Client;
41 namespace Mono.Messaging.RabbitMQ {
43 public class RabbitMQMessagingProvider : IMessagingProvider {
45 private int txCounter = 0;
46 private readonly Guid localId;
47 private readonly MessagingContextPool contextPool;
49 public RabbitMQMessagingProvider ()
51 localId = Guid.NewGuid ();
52 contextPool = new MessagingContextPool (new MessageFactory (this),
53 CreateConnection);
56 public IMessage CreateMessage ()
58 return new MessageBase ();
61 public IMessageQueueTransaction CreateMessageQueueTransaction ()
63 Interlocked.Increment (ref txCounter);
64 string txId = localId.ToString () + "_" + txCounter.ToString ();
66 return new RabbitMQMessageQueueTransaction (txId, contextPool);
69 public IMessagingContext CreateContext (string host)
71 return contextPool.GetContext (host);
74 private IConnection CreateConnection (string host)
76 ConnectionFactory cf = new ConnectionFactory ();
77 cf.Address = host;
78 return cf.CreateConnection ();
81 public void DeleteQueue (QueueReference qRef)
83 RabbitMQMessageQueue.Delete (qRef);
86 private readonly IDictionary queues = new Hashtable ();
87 private readonly ReaderWriterLock qLock = new ReaderWriterLock ();
88 private const int TIMEOUT = 15000;
90 public IMessageQueue[] GetPublicQueues ()
92 IMessageQueue[] qs;
93 qLock.AcquireReaderLock (TIMEOUT);
94 try {
95 ICollection qCollection = queues.Values;
96 qs = new IMessageQueue[qCollection.Count];
97 qCollection.CopyTo (qs, 0);
98 return qs;
99 } finally {
100 qLock.ReleaseReaderLock ();
104 public bool Exists (QueueReference qRef)
106 qLock.AcquireReaderLock (TIMEOUT);
107 try {
108 return queues.Contains (qRef);
109 } finally {
110 qLock.ReleaseReaderLock ();
114 public IMessageQueue CreateMessageQueue (QueueReference qRef,
115 bool transactional)
117 qLock.AcquireWriterLock (TIMEOUT);
118 try {
119 IMessageQueue mq = new RabbitMQMessageQueue (this, qRef, transactional);
120 queues[qRef] = mq;
121 return mq;
122 } finally {
123 qLock.ReleaseWriterLock ();
127 public IMessageQueue GetMessageQueue (QueueReference qRef)
129 qLock.AcquireReaderLock (TIMEOUT);
130 try {
131 if (queues.Contains (qRef))
132 return (IMessageQueue) queues[qRef];
133 else {
134 LockCookie lc = qLock.UpgradeToWriterLock (TIMEOUT);
135 try {
136 IMessageQueue mq = new RabbitMQMessageQueue (this, qRef, false);
137 queues[qRef] = mq;
138 return mq;
139 } finally {
140 qLock.DowngradeFromWriterLock (ref lc);
143 } finally {
144 qLock.ReleaseReaderLock ();