ISession.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 delegate that is notified whenever a Transational evemt occurs for
  23. /// the specified session such as TX started, committed or rolled back.
  24. /// </summary>
  25. public delegate void SessionTxEventDelegate(ISession session);
  26. /// <summary>
  27. /// Represents a single unit of work on an IConnection.
  28. /// So the ISession can be used to perform transactional receive and sends
  29. /// </summary>
  30. public interface ISession : IDisposable
  31. {
  32. /// <summary>
  33. /// Creates a producer of messages
  34. /// </summary>
  35. IMessageProducer CreateProducer();
  36. /// <summary>
  37. /// Creates a producer of messages
  38. /// </summary>
  39. Task<IMessageProducer> CreateProducerAsync();
  40. /// <summary>
  41. /// Creates a producer of messages on a given destination
  42. /// </summary>
  43. IMessageProducer CreateProducer(IDestination destination);
  44. /// <summary>
  45. /// Creates a producer of messages on a given destination
  46. /// </summary>
  47. Task<IMessageProducer> CreateProducerAsync(IDestination destination);
  48. /// <summary>
  49. /// Creates a consumer of messages on a given destination
  50. /// </summary>
  51. IMessageConsumer CreateConsumer(IDestination destination);
  52. /// <summary>
  53. /// Creates a consumer of messages on a given destination
  54. /// </summary>
  55. Task<IMessageConsumer> CreateConsumerAsync(IDestination destination);
  56. /// <summary>
  57. /// Creates a consumer of messages on a given destination with a selector
  58. /// </summary>
  59. IMessageConsumer CreateConsumer(IDestination destination, string selector);
  60. /// <summary>
  61. /// Creates a consumer of messages on a given destination with a selector
  62. /// </summary>
  63. Task<IMessageConsumer> CreateConsumerAsync(IDestination destination, string selector);
  64. /// <summary>
  65. /// Creates a consumer of messages on a given destination with a selector
  66. /// </summary>
  67. IMessageConsumer CreateConsumer(IDestination destination, string selector, bool noLocal);
  68. /// <summary>
  69. /// Creates a consumer of messages on a given destination with a selector
  70. /// </summary>
  71. Task<IMessageConsumer> CreateConsumerAsync(IDestination destination, string selector, bool noLocal);
  72. IMessageConsumer CreateDurableConsumer(ITopic destination, string name);
  73. Task<IMessageConsumer> CreateDurableConsumerAsync(ITopic destination, string name);
  74. IMessageConsumer CreateDurableConsumer(ITopic destination, string name, string selector);
  75. Task<IMessageConsumer> CreateDurableConsumerAsync(ITopic destination, string name, string selector);
  76. /// <summary>
  77. /// Creates a named durable consumer of messages on a given destination with a selector
  78. /// </summary>
  79. IMessageConsumer CreateDurableConsumer(ITopic destination, string name, string selector, bool noLocal);
  80. /// <summary>
  81. /// Creates a named durable consumer of messages on a given destination with a selector
  82. /// </summary>
  83. Task<IMessageConsumer> CreateDurableConsumerAsync(ITopic destination, string name, string selector, bool noLocal);
  84. IMessageConsumer CreateSharedConsumer(ITopic destination, string name);
  85. Task<IMessageConsumer> CreateSharedConsumerAsync(ITopic destination, string name);
  86. IMessageConsumer CreateSharedConsumer(ITopic destination, string name, string selector);
  87. Task<IMessageConsumer> CreateSharedConsumerAsync(ITopic destination, string name, string selector);
  88. IMessageConsumer CreateSharedDurableConsumer(ITopic destination, string name);
  89. Task<IMessageConsumer> CreateSharedDurableConsumerAsync(ITopic destination, string name);
  90. IMessageConsumer CreateSharedDurableConsumer(ITopic destination, string name, string selector);
  91. Task<IMessageConsumer> CreateSharedDurableConsumerAsync(ITopic destination, string name, string selector);
  92. /// <summary>
  93. /// Deletes a durable consumer created with CreateDurableConsumer().
  94. /// </summary>
  95. /// <param name="name">Name of the durable consumer</param>
  96. [Obsolete("should use unsubscribe instead")]
  97. void DeleteDurableConsumer(string name);
  98. void Unsubscribe(string name);
  99. Task UnsubscribeAsync(string name);
  100. /// <summary>
  101. /// Creates a QueueBrowser object to peek at the messages on the specified queue.
  102. /// </summary>
  103. /// <param name="queue">
  104. /// A <see cref="IQueue"/>
  105. /// </param>
  106. /// <returns>
  107. /// A <see cref="IQueueBrowser"/>
  108. /// </returns>
  109. /// <exception cref="System.NotSupportedException">
  110. /// If the Prodiver does not support creation of Queue Browsers.
  111. /// </exception>
  112. IQueueBrowser CreateBrowser(IQueue queue);
  113. /// <summary>
  114. /// Creates a QueueBrowser object to peek at the messages on the specified queue.
  115. /// </summary>
  116. /// <param name="queue">
  117. /// A <see cref="IQueue"/>
  118. /// </param>
  119. /// <returns>
  120. /// A <see cref="IQueueBrowser"/>
  121. /// </returns>
  122. /// <exception cref="System.NotSupportedException">
  123. /// If the Prodiver does not support creation of Queue Browsers.
  124. /// </exception>
  125. Task<IQueueBrowser> CreateBrowserAsync(IQueue queue);
  126. /// <summary>
  127. /// Creates a QueueBrowser object to peek at the messages on the specified queue
  128. /// using a message selector.
  129. /// </summary>
  130. /// <param name="queue">
  131. /// A <see cref="IQueue"/>
  132. /// </param>
  133. /// <param name="selector">
  134. /// A <see cref="System.String"/>
  135. /// </param>
  136. /// <returns>
  137. /// A <see cref="IQueueBrowser"/>
  138. /// </returns>
  139. /// <exception cref="System.NotSupportedException">
  140. /// If the Prodiver does not support creation of Queue Browsers.
  141. /// </exception>
  142. IQueueBrowser CreateBrowser(IQueue queue, string selector);
  143. /// <summary>
  144. /// Creates a QueueBrowser object to peek at the messages on the specified queue
  145. /// using a message selector.
  146. /// </summary>
  147. /// <param name="queue">
  148. /// A <see cref="IQueue"/>
  149. /// </param>
  150. /// <param name="selector">
  151. /// A <see cref="System.String"/>
  152. /// </param>
  153. /// <returns>
  154. /// A <see cref="IQueueBrowser"/>
  155. /// </returns>
  156. /// <exception cref="System.NotSupportedException">
  157. /// If the Prodiver does not support creation of Queue Browsers.
  158. /// </exception>
  159. Task<IQueueBrowser> CreateBrowserAsync(IQueue queue, string selector);
  160. /// <summary>
  161. /// Returns the queue for the given name
  162. /// </summary>
  163. IQueue GetQueue(string name);
  164. /// <summary>
  165. /// Returns the queue for the given name
  166. /// </summary>
  167. Task<IQueue> GetQueueAsync(string name);
  168. /// <summary>
  169. /// Returns the topic for the given name
  170. /// </summary>
  171. ITopic GetTopic(string name);
  172. /// <summary>
  173. /// Returns the topic for the given name
  174. /// </summary>
  175. Task<ITopic> GetTopicAsync(string name);
  176. /// <summary>
  177. /// Creates a temporary queue
  178. /// </summary>
  179. ITemporaryQueue CreateTemporaryQueue();
  180. /// <summary>
  181. /// Creates a temporary queue
  182. /// </summary>
  183. Task<ITemporaryQueue> CreateTemporaryQueueAsync();
  184. /// <summary>
  185. /// Creates a temporary topic
  186. /// </summary>
  187. ITemporaryTopic CreateTemporaryTopic();
  188. /// <summary>
  189. /// Creates a temporary topic
  190. /// </summary>
  191. Task<ITemporaryTopic> CreateTemporaryTopicAsync();
  192. /// <summary>
  193. /// Delete a destination (Queue, Topic, Temp Queue, Temp Topic).
  194. /// </summary>
  195. void DeleteDestination(IDestination destination);
  196. /// <summary>
  197. /// Delete a destination (Queue, Topic, Temp Queue, Temp Topic).
  198. /// </summary>
  199. Task DeleteDestinationAsync(IDestination destination);
  200. // Factory methods to create messages
  201. /// <summary>
  202. /// Creates a new message with an empty body
  203. /// </summary>
  204. IMessage CreateMessage();
  205. /// <summary>
  206. /// Creates a new message with an empty body
  207. /// </summary>
  208. Task<IMessage> CreateMessageAsync();
  209. /// <summary>
  210. /// Creates a new text message with an empty body
  211. /// </summary>
  212. ITextMessage CreateTextMessage();
  213. /// <summary>
  214. /// Creates a new text message with an empty body
  215. /// </summary>
  216. Task<ITextMessage> CreateTextMessageAsync();
  217. /// <summary>
  218. /// Creates a new text message with the given body
  219. /// </summary>
  220. ITextMessage CreateTextMessage(string text);
  221. /// <summary>
  222. /// Creates a new text message with the given body
  223. /// </summary>
  224. Task<ITextMessage> CreateTextMessageAsync(string text);
  225. /// <summary>
  226. /// Creates a new Map message which contains primitive key and value pairs
  227. /// </summary>
  228. IMapMessage CreateMapMessage();
  229. /// <summary>
  230. /// Creates a new Map message which contains primitive key and value pairs
  231. /// </summary>
  232. Task<IMapMessage> CreateMapMessageAsync();
  233. /// <summary>
  234. /// Creates a new Object message containing the given .NET object as the body
  235. /// </summary>
  236. IObjectMessage CreateObjectMessage(object body);
  237. /// <summary>
  238. /// Creates a new Object message containing the given .NET object as the body
  239. /// </summary>
  240. Task<IObjectMessage> CreateObjectMessageAsync(object body);
  241. /// <summary>
  242. /// Creates a new binary message
  243. /// </summary>
  244. IBytesMessage CreateBytesMessage();
  245. /// <summary>
  246. /// Creates a new binary message
  247. /// </summary>
  248. Task<IBytesMessage> CreateBytesMessageAsync();
  249. /// <summary>
  250. /// Creates a new binary message with the given body
  251. /// </summary>
  252. IBytesMessage CreateBytesMessage(byte[] body);
  253. /// <summary>
  254. /// Creates a new binary message with the given body
  255. /// </summary>
  256. Task<IBytesMessage> CreateBytesMessageAsync(byte[] body);
  257. /// <summary>
  258. /// Creates a new stream message
  259. /// </summary>
  260. IStreamMessage CreateStreamMessage();
  261. /// <summary>
  262. /// Creates a new stream message
  263. /// </summary>
  264. Task<IStreamMessage> CreateStreamMessageAsync();
  265. /// <summary>
  266. /// Closes the session. There is no need to close the producers and consumers
  267. /// of a closed session.
  268. /// </summary>
  269. void Close();
  270. Task CloseAsync();
  271. /// <summary>
  272. /// A Delegate that is called each time a Message is dispatched to allow the client to do
  273. /// any necessary transformations on the received message before it is delivered.
  274. /// The Session instance sets the delegate on each Consumer it creates.
  275. /// </summary>
  276. ConsumerTransformerDelegate ConsumerTransformer { get; set; }
  277. /// <summary>
  278. /// A delegate that is called each time a Message is sent from this Producer which allows
  279. /// the application to perform any needed transformations on the Message before it is sent.
  280. /// The Session instance sets the delegate on each Producer it creates.
  281. /// </summary>
  282. ProducerTransformerDelegate ProducerTransformer { get; set; }
  283. /// <summary>
  284. /// Stops all Message delivery in this session and restarts it again
  285. /// with the oldest unacknowledged message. Messages that were delivered
  286. /// but not acknowledge should have their redelivered property set.
  287. /// This is an optional method that may not by implemented by all NMS
  288. /// providers, if not implemented an Exception will be thrown.
  289. /// Message redelivery is not requried to be performed in the original
  290. /// order. It is not valid to call this method on a Transacted Session.
  291. /// </summary>
  292. void Recover();
  293. /// <summary>
  294. /// Stops all Message delivery in this session and restarts it again
  295. /// with the oldest unacknowledged message. Messages that were delivered
  296. /// but not acknowledge should have their redelivered property set.
  297. /// This is an optional method that may not by implemented by all NMS
  298. /// providers, if not implemented an Exception will be thrown.
  299. /// Message redelivery is not requried to be performed in the original
  300. /// order. It is not valid to call this method on a Transacted Session.
  301. /// </summary>
  302. Task RecoverAsync();
  303. void Acknowledge();
  304. Task AcknowledgeAsync();
  305. #region Transaction methods
  306. /// <summary>
  307. /// If this is a transactional session then commit all message
  308. /// send and acknowledgements for producers and consumers in this session
  309. /// </summary>
  310. void Commit();
  311. /// <summary>
  312. /// If this is a transactional session then commit all message
  313. /// send and acknowledgements for producers and consumers in this session
  314. /// </summary>
  315. Task CommitAsync();
  316. /// <summary>
  317. /// If this is a transactional session then rollback all message
  318. /// send and acknowledgements for producers and consumers in this session
  319. /// </summary>
  320. void Rollback();
  321. /// <summary>
  322. /// If this is a transactional session then rollback all message
  323. /// send and acknowledgements for producers and consumers in this session
  324. /// </summary>
  325. Task RollbackAsync();
  326. #endregion
  327. #region Session Events
  328. event SessionTxEventDelegate TransactionStartedListener;
  329. event SessionTxEventDelegate TransactionCommittedListener;
  330. event SessionTxEventDelegate TransactionRolledBackListener;
  331. #endregion
  332. #region Attributes
  333. TimeSpan RequestTimeout { get; set; }
  334. bool Transacted { get; }
  335. AcknowledgementMode AcknowledgementMode { get; }
  336. #endregion
  337. }
  338. }