INMSContext.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. using System;
  18. using System.Threading.Tasks;
  19. namespace Apache.NMS
  20. {
  21. /// <summary>
  22. /// A INMSContext is the main interface in the simplified NMS API
  23. /// introduced for NMS 2.0. This combines in a single object the functionality of
  24. /// two separate objects from the NMS 1.x API: an IConnection and an ISession.
  25. ///
  26. /// This is comparable to JMS 2.0 API that extended JMS 1.x API
  27. /// </summary>
  28. public interface INMSContext : IDisposable, IStartable, IStoppable
  29. {
  30. INMSContext CreateContext(AcknowledgementMode acknowledgementMode);
  31. /// <summary>
  32. /// Creates a producer of messages
  33. /// </summary>
  34. INMSProducer CreateProducer();
  35. /// <summary>
  36. /// Creates a producer of messages
  37. /// </summary>
  38. Task<INMSProducer> CreateProducerAsync();
  39. /// <summary>
  40. /// Creates a consumer of messages on a given destination
  41. /// </summary>
  42. INMSConsumer CreateConsumer(IDestination destination);
  43. /// <summary>
  44. /// Creates a consumer of messages on a given destination with a selector
  45. /// </summary>
  46. INMSConsumer CreateConsumer(IDestination destination, string selector);
  47. /// <summary>
  48. /// Creates a consumer of messages on a given destination with a selector
  49. /// </summary>
  50. INMSConsumer CreateConsumer(IDestination destination, string selector, bool noLocal);
  51. INMSConsumer CreateDurableConsumer(ITopic destination, string subscriptionName);
  52. INMSConsumer CreateDurableConsumer(ITopic destination, string subscriptionName, string selector);
  53. /// <summary>
  54. /// Creates a named durable consumer of messages on a given destination with a selector
  55. /// </summary>
  56. INMSConsumer CreateDurableConsumer(ITopic destination, string subscriptionName, string selector, bool noLocal);
  57. INMSConsumer CreateSharedConsumer(ITopic destination, string subscriptionName);
  58. INMSConsumer CreateSharedConsumer(ITopic destination, string subscriptionName, string selector);
  59. INMSConsumer CreateSharedDurableConsumer(ITopic destination, string subscriptionName);
  60. INMSConsumer CreateSharedDurableConsumer(ITopic destination, string subscriptionName, string selector);
  61. /// <summary>
  62. /// Creates a consumer of messages on a given destination
  63. /// </summary>
  64. Task<INMSConsumer> CreateConsumerAsync(IDestination destination);
  65. /// <summary>
  66. /// Creates a consumer of messages on a given destination with a selector
  67. /// </summary>
  68. Task<INMSConsumer> CreateConsumerAsync(IDestination destination, string selector);
  69. /// <summary>
  70. /// Creates a consumer of messages on a given destination with a selector
  71. /// </summary>
  72. Task<INMSConsumer> CreateConsumerAsync(IDestination destination, string selector, bool noLocal);
  73. Task<INMSConsumer> CreateDurableConsumerAsync(ITopic destination, string subscriptionName);
  74. Task<INMSConsumer> CreateDurableConsumerAsync(ITopic destination, string subscriptionName, string selector);
  75. /// <summary>
  76. /// Creates a named durable consumer of messages on a given destination with a selector
  77. /// </summary>
  78. Task<INMSConsumer> CreateDurableConsumerAsync(ITopic destination, string subscriptionName, string selector, bool noLocal);
  79. Task<INMSConsumer> CreateSharedConsumerAsync(ITopic destination, string subscriptionName);
  80. Task<INMSConsumer> CreateSharedConsumerAsync(ITopic destination, string subscriptionName, string selector);
  81. Task<INMSConsumer> CreateSharedDurableConsumerAsync(ITopic destination, string subscriptionName);
  82. Task<INMSConsumer> CreateSharedDurableConsumerAsync(ITopic destination, string subscriptionName, string selector);
  83. void Unsubscribe(string name);
  84. Task UnsubscribeAsync(string name);
  85. /// <summary>
  86. /// Creates a QueueBrowser object to peek at the messages on the specified queue.
  87. /// </summary>
  88. /// <param name="queue">
  89. /// A <see cref="IQueue"/>
  90. /// </param>
  91. /// <returns>
  92. /// A <see cref="IQueueBrowser"/>
  93. /// </returns>
  94. /// <exception cref="System.NotSupportedException">
  95. /// If the Prodiver does not support creation of Queue Browsers.
  96. /// </exception>
  97. IQueueBrowser CreateBrowser(IQueue queue);
  98. /// <summary>
  99. /// Creates a QueueBrowser object to peek at the messages on the specified queue.
  100. /// </summary>
  101. /// <param name="queue">
  102. /// A <see cref="IQueue"/>
  103. /// </param>
  104. /// <returns>
  105. /// A <see cref="IQueueBrowser"/>
  106. /// </returns>
  107. /// <exception cref="System.NotSupportedException">
  108. /// If the Prodiver does not support creation of Queue Browsers.
  109. /// </exception>
  110. Task<IQueueBrowser> CreateBrowserAsync(IQueue queue);
  111. /// <summary>
  112. /// Creates a QueueBrowser object to peek at the messages on the specified queue
  113. /// using a message selector.
  114. /// </summary>
  115. /// <param name="queue">
  116. /// A <see cref="IQueue"/>
  117. /// </param>
  118. /// <param name="selector">
  119. /// A <see cref="System.String"/>
  120. /// </param>
  121. /// <returns>
  122. /// A <see cref="IQueueBrowser"/>
  123. /// </returns>
  124. /// <exception cref="System.NotSupportedException">
  125. /// If the Prodiver does not support creation of Queue Browsers.
  126. /// </exception>
  127. IQueueBrowser CreateBrowser(IQueue queue, string selector);
  128. /// <summary>
  129. /// Creates a QueueBrowser object to peek at the messages on the specified queue
  130. /// using a message selector.
  131. /// </summary>
  132. /// <param name="queue">
  133. /// A <see cref="IQueue"/>
  134. /// </param>
  135. /// <param name="selector">
  136. /// A <see cref="System.String"/>
  137. /// </param>
  138. /// <returns>
  139. /// A <see cref="IQueueBrowser"/>
  140. /// </returns>
  141. /// <exception cref="System.NotSupportedException">
  142. /// If the Prodiver does not support creation of Queue Browsers.
  143. /// </exception>
  144. Task<IQueueBrowser> CreateBrowserAsync(IQueue queue, string selector);
  145. /// <summary>
  146. /// Returns the queue for the given name
  147. /// </summary>
  148. IQueue GetQueue(string name);
  149. /// <summary>
  150. /// Returns the queue for the given name
  151. /// </summary>
  152. Task<IQueue> GetQueueAsync(string name);
  153. /// <summary>
  154. /// Returns the topic for the given name
  155. /// </summary>
  156. ITopic GetTopic(string name);
  157. /// <summary>
  158. /// Returns the topic for the given name
  159. /// </summary>
  160. Task<ITopic> GetTopicAsync(string name);
  161. /// <summary>
  162. /// Creates a temporary queue
  163. /// </summary>
  164. ITemporaryQueue CreateTemporaryQueue();
  165. /// <summary>
  166. /// Creates a temporary queue
  167. /// </summary>
  168. Task<ITemporaryQueue> CreateTemporaryQueueAsync();
  169. /// <summary>
  170. /// Creates a temporary topic
  171. /// </summary>
  172. ITemporaryTopic CreateTemporaryTopic();
  173. /// <summary>
  174. /// Creates a temporary topic
  175. /// </summary>
  176. Task<ITemporaryTopic> CreateTemporaryTopicAsync();
  177. // Factory methods to create messages
  178. /// <summary>
  179. /// Creates a new message with an empty body
  180. /// </summary>
  181. IMessage CreateMessage();
  182. /// <summary>
  183. /// Creates a new message with an empty body
  184. /// </summary>
  185. Task<IMessage> CreateMessageAsync();
  186. /// <summary>
  187. /// Creates a new text message with an empty body
  188. /// </summary>
  189. ITextMessage CreateTextMessage();
  190. /// <summary>
  191. /// Creates a new text message with an empty body
  192. /// </summary>
  193. Task<ITextMessage> CreateTextMessageAsync();
  194. /// <summary>
  195. /// Creates a new text message with the given body
  196. /// </summary>
  197. ITextMessage CreateTextMessage(string text);
  198. /// <summary>
  199. /// Creates a new text message with the given body
  200. /// </summary>
  201. Task<ITextMessage> CreateTextMessageAsync(string text);
  202. /// <summary>
  203. /// Creates a new Map message which contains primitive key and value pairs
  204. /// </summary>
  205. IMapMessage CreateMapMessage();
  206. /// <summary>
  207. /// Creates a new Map message which contains primitive key and value pairs
  208. /// </summary>
  209. Task<IMapMessage> CreateMapMessageAsync();
  210. /// <summary>
  211. /// Creates a new Object message containing the given .NET object as the body
  212. /// </summary>
  213. IObjectMessage CreateObjectMessage(object body);
  214. /// <summary>
  215. /// Creates a new Object message containing the given .NET object as the body
  216. /// </summary>
  217. Task<IObjectMessage> CreateObjectMessageAsync(object body);
  218. /// <summary>
  219. /// Creates a new binary message
  220. /// </summary>
  221. IBytesMessage CreateBytesMessage();
  222. /// <summary>
  223. /// Creates a new binary message
  224. /// </summary>
  225. Task<IBytesMessage> CreateBytesMessageAsync();
  226. /// <summary>
  227. /// Creates a new binary message with the given body
  228. /// </summary>
  229. IBytesMessage CreateBytesMessage(byte[] body);
  230. /// <summary>
  231. /// Creates a new binary message with the given body
  232. /// </summary>
  233. Task<IBytesMessage> CreateBytesMessageAsync(byte[] body);
  234. /// <summary>
  235. /// Creates a new stream message
  236. /// </summary>
  237. IStreamMessage CreateStreamMessage();
  238. /// <summary>
  239. /// Creates a new stream message
  240. /// </summary>
  241. Task<IStreamMessage> CreateStreamMessageAsync();
  242. /// <summary>
  243. /// Closes the session. There is no need to close the producers and consumers
  244. /// of a closed session.
  245. /// </summary>
  246. void Close();
  247. /// <summary>
  248. /// Closes the session. There is no need to close the producers and consumers
  249. /// of a closed session.
  250. /// </summary>
  251. Task CloseAsync();
  252. /// <summary>
  253. /// A Delegate that is called each time a Message is dispatched to allow the client to do
  254. /// any necessary transformations on the received message before it is delivered.
  255. /// The Session instance sets the delegate on each Consumer it creates.
  256. /// </summary>
  257. ConsumerTransformerDelegate ConsumerTransformer { get; set; }
  258. /// <summary>
  259. /// A delegate that is called each time a Message is sent from this Producer which allows
  260. /// the application to perform any needed transformations on the Message before it is sent.
  261. /// The Session instance sets the delegate on each Producer it creates.
  262. /// </summary>
  263. ProducerTransformerDelegate ProducerTransformer { get; set; }
  264. /// <summary>
  265. /// Stops all Message delivery in this session and restarts it again
  266. /// with the oldest unacknowledged message. Messages that were delivered
  267. /// but not acknowledge should have their redelivered property set.
  268. /// This is an optional method that may not by implemented by all NMS
  269. /// providers, if not implemented an Exception will be thrown.
  270. /// Message redelivery is not requried to be performed in the original
  271. /// order. It is not valid to call this method on a Transacted Session.
  272. /// </summary>
  273. void Recover();
  274. /// <summary>
  275. /// Stops all Message delivery in this session and restarts it again
  276. /// with the oldest unacknowledged message. Messages that were delivered
  277. /// but not acknowledge should have their redelivered property set.
  278. /// This is an optional method that may not by implemented by all NMS
  279. /// providers, if not implemented an Exception will be thrown.
  280. /// Message redelivery is not requried to be performed in the original
  281. /// order. It is not valid to call this method on a Transacted Session.
  282. /// </summary>
  283. Task RecoverAsync();
  284. void Acknowledge();
  285. Task AcknowledgeAsync();
  286. #region Transaction methods
  287. /// <summary>
  288. /// If this is a transactional session then commit all message
  289. /// send and acknowledgements for producers and consumers in this session
  290. /// </summary>
  291. void Commit();
  292. /// <summary>
  293. /// If this is a transactional session then commit all message
  294. /// send and acknowledgements for producers and consumers in this session
  295. /// </summary>
  296. Task CommitAsync();
  297. /// <summary>
  298. /// If this is a transactional session then rollback all message
  299. /// send and acknowledgements for producers and consumers in this session
  300. /// </summary>
  301. void Rollback();
  302. /// <summary>
  303. /// If this is a transactional session then rollback all message
  304. /// send and acknowledgements for producers and consumers in this session
  305. /// </summary>
  306. Task RollbackAsync();
  307. #endregion
  308. #region Session Events
  309. event SessionTxEventDelegate TransactionStartedListener;
  310. event SessionTxEventDelegate TransactionCommittedListener;
  311. event SessionTxEventDelegate TransactionRolledBackListener;
  312. /// <summary>
  313. /// An asynchronous listener which can be notified if an error occurs
  314. /// </summary>
  315. event ExceptionListener ExceptionListener;
  316. /// <summary>
  317. /// An asynchronous listener that is notified when a Fault tolerant connection
  318. /// has been interrupted.
  319. /// </summary>
  320. event ConnectionInterruptedListener ConnectionInterruptedListener;
  321. /// <summary>
  322. /// An asynchronous listener that is notified when a Fault tolerant connection
  323. /// has been resumed.
  324. /// </summary>
  325. event ConnectionResumedListener ConnectionResumedListener;
  326. #endregion
  327. #region Attributes
  328. TimeSpan RequestTimeout { get; set; }
  329. bool Transacted { get; }
  330. AcknowledgementMode AcknowledgementMode { get; }
  331. string ClientId { get; set; }
  332. bool AutoStart { get; set; }
  333. #endregion
  334. /// <summary>
  335. /// For a long running Connection that creates many temp destinations
  336. /// this method will close and destroy all previously created temp
  337. /// destinations to reduce resource consumption. This can be useful
  338. /// when the Connection is pooled or otherwise used for long periods
  339. /// of time. Only locally created temp destinations should be removed
  340. /// by this call.
  341. /// NOTE: This is an optional operation and for NMS providers that
  342. /// do not support this functionality the method should just return
  343. /// without throwing any exceptions.
  344. /// </summary>
  345. void PurgeTempDestinations();
  346. }
  347. }