PageRenderTime 1441ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/EasyNetQ/IAdvancedBus.cs

http://github.com/mikehadlow/EasyNetQ
C# | 472 lines | 113 code | 39 blank | 320 comment | 0 complexity | 87fedbe3b3636bc1aeb8e7be40691edd MD5 | raw file
Possible License(s): Apache-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using EasyNetQ.Consumer;
  5. using EasyNetQ.Topology;
  6. using RabbitMQ.Client.Events;
  7. namespace EasyNetQ
  8. {
  9. /// <summary>
  10. /// IAdvancedBus is a lower level API than IBus which gives you fined grained control
  11. /// of routing topology, but keeping the EasyNetQ serialisation, persistent connection,
  12. /// error handling and subscription thread.
  13. /// </summary>
  14. public interface IAdvancedBus : IDisposable
  15. {
  16. /// <summary>
  17. /// Consume a stream of messages
  18. /// </summary>
  19. /// <typeparam name="T">The message type</typeparam>
  20. /// <param name="queueConsumerPairs">Multiple queue - consumer pairs</param>
  21. /// <param name="configure">
  22. /// Fluent configuration e.g. x => x.WithPriority(10)</param>
  23. /// <returns>A disposable to cancel the consumer</returns>
  24. IDisposable Consume(IEnumerable<QueueConsumerPair> queueConsumerPairs, Action<IConsumerConfiguration> configure);
  25. /// <summary>
  26. /// Consume a stream of messages
  27. /// </summary>
  28. /// <typeparam name="T">The message type</typeparam>
  29. /// <param name="queue">The queue to take messages from</param>
  30. /// <param name="onMessage">The message handler</param>
  31. /// <returns>A disposable to cancel the consumer</returns>
  32. IDisposable Consume<T>(IQueue queue, Action<IMessage<T>, MessageReceivedInfo> onMessage) where T : class;
  33. /// <summary>
  34. /// Consume a stream of messages
  35. /// </summary>
  36. /// <typeparam name="T">The message type</typeparam>
  37. /// <param name="queue">The queue to take messages from</param>
  38. /// <param name="onMessage">The message handler</param>
  39. /// <param name="configure">
  40. /// Fluent configuration e.g. x => x.WithPriority(10)</param>
  41. /// <returns>A disposable to cancel the consumer</returns>
  42. IDisposable Consume<T>(IQueue queue, Action<IMessage<T>, MessageReceivedInfo> onMessage, Action<IConsumerConfiguration> configure) where T : class;
  43. /// <summary>
  44. /// Consume a stream of messages asynchronously
  45. /// </summary>
  46. /// <typeparam name="T">The message type</typeparam>
  47. /// <param name="queue">The queue to take messages from</param>
  48. /// <param name="onMessage">The message handler</param>
  49. /// <returns>A disposable to cancel the consumer</returns>
  50. IDisposable Consume<T>(IQueue queue, Func<IMessage<T>, MessageReceivedInfo, Task> onMessage) where T : class;
  51. /// <summary>
  52. /// Consume a stream of messages asynchronously
  53. /// </summary>
  54. /// <typeparam name="T">The message type</typeparam>
  55. /// <param name="queue">The queue to take messages from</param>
  56. /// <param name="onMessage">The message handler</param>
  57. /// <param name="configure">
  58. /// Fluent configuration e.g. x => x.WithPriority(10)</param>
  59. /// <returns>A disposable to cancel the consumer</returns>
  60. IDisposable Consume<T>(IQueue queue, Func<IMessage<T>, MessageReceivedInfo, Task> onMessage, Action<IConsumerConfiguration> configure) where T : class;
  61. /// <summary>
  62. /// Consume a stream of messages. Dispatch them to the given handlers
  63. /// </summary>
  64. /// <param name="queue">The queue to take messages from</param>
  65. /// <param name="addHandlers">A function to add handlers to the consumer</param>
  66. /// <returns>A disposable to cancel the consumer</returns>
  67. IDisposable Consume(IQueue queue, Action<IHandlerRegistration> addHandlers);
  68. /// <summary>
  69. /// Consume a stream of messages. Dispatch them to the given handlers
  70. /// </summary>
  71. /// <param name="queue">The queue to take messages from</param>
  72. /// <param name="addHandlers">A function to add handlers to the consumer</param>
  73. /// <param name="configure">
  74. /// Fluent configuration e.g. x => x.WithPriority(10)</param>
  75. /// <returns>A disposable to cancel the consumer</returns>
  76. IDisposable Consume(IQueue queue, Action<IHandlerRegistration> addHandlers, Action<IConsumerConfiguration> configure);
  77. /// <summary>
  78. /// Consume raw bytes from the queue.
  79. /// </summary>
  80. /// <param name="queue">The queue to subscribe to</param>
  81. /// <param name="onMessage">
  82. /// The message handler. Takes the message body, message properties and some information about the
  83. /// receive context.
  84. /// </param>
  85. /// <returns>A disposable to cancel the consumer</returns>
  86. IDisposable Consume(IQueue queue, Action<byte[], MessageProperties, MessageReceivedInfo> onMessage);
  87. /// <summary>
  88. /// Consume raw bytes from the queue.
  89. /// </summary>
  90. /// <param name="queue">The queue to subscribe to</param>
  91. /// <param name="onMessage">
  92. /// The message handler. Takes the message body, message properties and some information about the
  93. /// receive context.
  94. /// </param>
  95. /// <param name="configure">
  96. /// Fluent configuration e.g. x => x.WithPriority(10)</param>
  97. /// <returns>A disposable to cancel the consumer</returns>
  98. IDisposable Consume(IQueue queue, Action<byte[], MessageProperties, MessageReceivedInfo> onMessage, Action<IConsumerConfiguration> configure);
  99. /// <summary>
  100. /// Consume raw bytes from the queue.
  101. /// </summary>
  102. /// <param name="queue">The queue to subscribe to</param>
  103. /// <param name="onMessage">
  104. /// The message handler. Takes the message body, message properties and some information about the
  105. /// receive context. Returns a Task.
  106. /// </param>
  107. /// <returns>A disposable to cancel the consumer</returns>
  108. IDisposable Consume(IQueue queue, Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage);
  109. /// <summary>
  110. /// Consume raw bytes from the queue.
  111. /// </summary>
  112. /// <param name="queue">The queue to subscribe to</param>
  113. /// <param name="onMessage">
  114. /// The message handler. Takes the message body, message properties and some information about the
  115. /// receive context. Returns a Task.
  116. /// </param>
  117. /// <param name="configure">
  118. /// Fluent configuration e.g. x => x.WithPriority(10)</param>
  119. /// <returns>A disposable to cancel the consumer</returns>
  120. IDisposable Consume(IQueue queue, Func<byte[], MessageProperties, MessageReceivedInfo, Task> onMessage, Action<IConsumerConfiguration> configure);
  121. /// <summary>
  122. /// Publish a message as a byte array
  123. /// </summary>
  124. /// <param name="exchange">The exchange to publish to</param>
  125. /// <param name="routingKey">
  126. /// The routing key for the message. The routing key is used for routing messages depending on the
  127. /// exchange configuration.</param>
  128. /// <param name="mandatory">
  129. /// This flag tells the server how to react if the message cannot be routed to a queue.
  130. /// If this flag is true, the server will return an unroutable message with a Return method.
  131. /// If this flag is false, the server silently drops the message.
  132. /// </param>
  133. /// <param name="messageProperties">The message properties</param>
  134. /// <param name="body">The message body</param>
  135. void Publish(
  136. IExchange exchange,
  137. string routingKey,
  138. bool mandatory,
  139. MessageProperties messageProperties,
  140. byte[] body);
  141. /// <summary>
  142. /// Publish a message as a .NET type
  143. /// </summary>
  144. /// <typeparam name="T"></typeparam>
  145. /// <param name="exchange">The exchange to publish to</param>
  146. /// <param name="routingKey">
  147. /// The routing key for the message. The routing key is used for routing messages depending on the
  148. /// exchange configuration.</param>
  149. /// <param name="mandatory">
  150. /// This flag tells the server how to react if the message cannot be routed to a queue.
  151. /// If this flag is true, the server will return an unroutable message with a Return method.
  152. /// If this flag is false, the server silently drops the message.
  153. /// </param>
  154. /// <param name="message">The message to publish</param>
  155. void Publish<T>(
  156. IExchange exchange,
  157. string routingKey,
  158. bool mandatory,
  159. IMessage<T> message) where T : class;
  160. /// <summary>
  161. /// Publish a message as a .NET type when the type is only known at runtime.
  162. /// Use the generic version of this method <see cref="PublishAsync{T}"/> when you know the type of the message at compile time.
  163. /// Task completes after publish has completed. If publisherConfirms=true is set in the connection string,
  164. /// the task completes after an ACK is received. The task will throw on either NACK or timeout.
  165. /// </summary>
  166. /// <param name="exchange">The exchange to publish to</param>
  167. /// <param name="routingKey">
  168. /// The routing key for the message. The routing key is used for routing messages depending on the
  169. /// exchange configuration.</param>
  170. /// <param name="mandatory">
  171. /// This flag tells the server how to react if the message cannot be routed to a queue.
  172. /// If this flag is true, the server will return an unroutable message with a Return method.
  173. /// If this flag is false, the server silently drops the message.
  174. /// </param>
  175. /// <param name="message">The message to publish</param>
  176. Task PublishAsync(
  177. IExchange exchange,
  178. string routingKey,
  179. bool mandatory,
  180. IMessage message);
  181. /// <summary>
  182. /// Publish a message as a .NET type
  183. /// Task completes after publish has completed. If publisherConfirms=true is set in the connection string,
  184. /// the task completes after an ACK is received. The task will throw on either NACK or timeout.
  185. /// </summary>
  186. /// <typeparam name="T"></typeparam>
  187. /// <param name="exchange">The exchange to publish to</param>
  188. /// <param name="routingKey">
  189. /// The routing key for the message. The routing key is used for routing messages depending on the
  190. /// exchange configuration.</param>
  191. /// <param name="mandatory">
  192. /// This flag tells the server how to react if the message cannot be routed to a queue.
  193. /// If this flag is true, the server will return an unroutable message with a Return method.
  194. /// If this flag is false, the server silently drops the message.
  195. /// </param>
  196. /// <param name="message">The message to publish</param>
  197. Task PublishAsync<T>(
  198. IExchange exchange,
  199. string routingKey,
  200. bool mandatory,
  201. IMessage<T> message) where T : class;
  202. /// <summary>
  203. /// Publish a message as a byte array.
  204. /// Task completes after publish has completed. If publisherConfirms=true is set in the connection string,
  205. /// the task completes after an ACK is received. The task will throw on either NACK or timeout.
  206. /// </summary>
  207. /// <param name="exchange">The exchange to publish to</param>
  208. /// <param name="routingKey">
  209. /// The routing key for the message. The routing key is used for routing messages depending on the
  210. /// exchange configuration.</param>
  211. /// <param name="mandatory">
  212. /// This flag tells the server how to react if the message cannot be routed to a queue.
  213. /// If this flag is true, the server will return an unroutable message with a Return method.
  214. /// If this flag is false, the server silently drops the message.
  215. /// </param>
  216. /// <param name="messageProperties">The message properties</param>
  217. /// <param name="body">The message body</param>
  218. Task PublishAsync(
  219. IExchange exchange,
  220. string routingKey,
  221. bool mandatory,
  222. MessageProperties messageProperties,
  223. byte[] body);
  224. /// <summary>
  225. /// Declare a queue. If the queue already exists this method does nothing
  226. /// </summary>
  227. /// <param name="name">The name of the queue</param>
  228. /// <param name="passive">Throw an exception rather than create the queue if it doesn't exist</param>
  229. /// <param name="durable">Durable queues remain active when a server restarts.</param>
  230. /// <param name="exclusive">Exclusive queues may only be accessed by the current connection, and are deleted when that connection closes.</param>
  231. /// <param name="autoDelete">If set, the queue is deleted when all consumers have finished using it.</param>
  232. /// <param name="perQueueMessageTtl">Determines how long a message published to a queue can live before it is discarded by the server.</param>
  233. /// <param name="expires">Determines how long a queue can remain unused before it is automatically deleted by the server.</param>
  234. /// <param name="maxPriority">Determines the maximum message priority that the queue should support.</param>
  235. /// <param name="deadLetterExchange">Determines an exchange's name can remain unused before it is automatically deleted by the server.</param>
  236. /// <param name="deadLetterRoutingKey">If set, will route message with the routing key specified, if not set, message will be routed with the same routing keys they were originally published with.</param>
  237. /// <param name="maxLength">The maximum number of ready messages that may exist on the queue. Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached</param>
  238. /// <param name="maxLengthBytes">The maximum size of the queue in bytes. Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached</param>
  239. /// <returns>
  240. /// The queue
  241. /// </returns>
  242. IQueue QueueDeclare(
  243. string name,
  244. bool passive = false,
  245. bool durable = true,
  246. bool exclusive = false,
  247. bool autoDelete = false,
  248. int? perQueueMessageTtl = null,
  249. int? expires = null,
  250. int? maxPriority = null,
  251. string deadLetterExchange = null,
  252. string deadLetterRoutingKey = null,
  253. int? maxLength = null,
  254. int? maxLengthBytes = null);
  255. /// <summary>
  256. /// Declare a queue. If the queue already exists this method does nothing
  257. /// </summary>
  258. /// <param name="name">The name of the queue</param>
  259. /// <param name="passive">Throw an exception rather than create the queue if it doesn't exist</param>
  260. /// <param name="durable">Durable queues remain active when a server restarts.</param>
  261. /// <param name="exclusive">Exclusive queues may only be accessed by the current connection, and are deleted when that connection closes.</param>
  262. /// <param name="autoDelete">If set, the queue is deleted when all consumers have finished using it.</param>
  263. /// <param name="perQueueMessageTtl">Determines how long a message published to a queue can live before it is discarded by the server.</param>
  264. /// <param name="expires">Determines how long a queue can remain unused before it is automatically deleted by the server.</param>
  265. /// <param name="maxPriority">Determines the maximum message priority that the queue should support.</param>
  266. /// <param name="deadLetterExchange">Determines an exchange's name can remain unused before it is automatically deleted by the server.</param>
  267. /// <param name="deadLetterRoutingKey">If set, will route message with the routing key specified, if not set, message will be routed with the same routing keys they were originally published with.</param>
  268. /// <param name="maxLength">The maximum number of ready messages that may exist on the queue. Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached</param>
  269. /// <param name="maxLengthBytes">The maximum size of the queue in bytes. Messages will be dropped or dead-lettered from the front of the queue to make room for new messages once the limit is reached</param>
  270. /// <returns>The queue</returns>
  271. Task<IQueue> QueueDeclareAsync(
  272. string name,
  273. bool passive = false,
  274. bool durable = true,
  275. bool exclusive = false,
  276. bool autoDelete = false,
  277. int? perQueueMessageTtl = null,
  278. int? expires = null,
  279. int? maxPriority = null,
  280. string deadLetterExchange = null,
  281. string deadLetterRoutingKey = null,
  282. int? maxLength = null,
  283. int? maxLengthBytes = null);
  284. /// <summary>
  285. /// Delete a queue
  286. /// </summary>
  287. /// <param name="queue">The queue to delete</param>
  288. /// <param name="ifUnused">Only delete if unused</param>
  289. /// <param name="ifEmpty">Only delete if empty</param>
  290. void QueueDelete(IQueue queue, bool ifUnused = false, bool ifEmpty = false);
  291. /// <summary>
  292. /// Purges a queue
  293. /// </summary>
  294. /// <param name="queue">The queue to purge</param>
  295. void QueuePurge(IQueue queue);
  296. /// <summary>
  297. /// Declare an exchange
  298. /// </summary>
  299. /// <param name="name">The exchange name</param>
  300. /// <param name="type">The type of exchange</param>
  301. /// <param name="passive">Throw an exception rather than create the exchange if it doens't exist</param>
  302. /// <param name="durable">Durable exchanges remain active when a server restarts.</param>
  303. /// <param name="autoDelete">If set, the exchange is deleted when all queues have finished using it.</param>
  304. /// <param name="internal">If set, the exchange may not be used directly by publishers, but only when bound to other exchanges.</param>
  305. /// <param name="alternateExchange">Route messages to this exchange if they cannot be routed.</param>
  306. /// <param name="delayed">If set, declars x-delayed-type exchange for routing delayed messages.</param>
  307. /// <returns>The exchange</returns>
  308. IExchange ExchangeDeclare(
  309. string name,
  310. string type,
  311. bool passive = false,
  312. bool durable = true,
  313. bool autoDelete = false,
  314. bool @internal = false,
  315. string alternateExchange = null,
  316. bool delayed = false);
  317. /// <summary>
  318. /// Declare an exchange
  319. /// </summary>
  320. /// <param name="name">The exchange name</param>
  321. /// <param name="type">The type of exchange</param>
  322. /// <param name="passive">Throw an exception rather than create the exchange if it doens't exist</param>
  323. /// <param name="durable">Durable exchanges remain active when a server restarts.</param>
  324. /// <param name="autoDelete">If set, the exchange is deleted when all queues have finished using it.</param>
  325. /// <param name="internal">If set, the exchange may not be used directly by publishers, but only when bound to other exchanges.</param>
  326. /// <param name="alternateExchange">Route messages to this exchange if they cannot be routed.</param>
  327. /// <param name="delayed">If set, declars x-delayed-type exchange for routing delayed messages.</param>
  328. /// <returns>The exchange</returns>
  329. Task<IExchange> ExchangeDeclareAsync(
  330. string name,
  331. string type,
  332. bool passive = false,
  333. bool durable = true,
  334. bool autoDelete = false,
  335. bool @internal = false,
  336. string alternateExchange = null,
  337. bool delayed = false);
  338. /// <summary>
  339. /// Delete an exchange
  340. /// </summary>
  341. /// <param name="exchange">The exchange to delete</param>
  342. /// <param name="ifUnused">If set, the server will only delete the exchange if it has no queue bindings.</param>
  343. void ExchangeDelete(IExchange exchange, bool ifUnused = false);
  344. /// <summary>
  345. /// Bind an exchange to a queue. Does nothing if the binding already exists.
  346. /// </summary>
  347. /// <param name="exchange">The exchange to bind</param>
  348. /// <param name="queue">The queue to bind</param>
  349. /// <param name="routingKey">The routing key</param>
  350. /// <returns>A binding</returns>
  351. IBinding Bind(IExchange exchange, IQueue queue, string routingKey);
  352. /// <summary>
  353. /// Bind an exchange to a queue. Does nothing if the binding already exists.
  354. /// </summary>
  355. /// <param name="exchange">The exchange to bind</param>
  356. /// <param name="queue">The queue to bind</param>
  357. /// <param name="routingKey">The routing key</param>
  358. /// <returns>A binding</returns>
  359. Task<IBinding> BindAsync(IExchange exchange, IQueue queue, string routingKey);
  360. /// <summary>
  361. /// Bind two exchanges. Does nothing if the binding already exists.
  362. /// </summary>
  363. /// <param name="source">The source exchange</param>
  364. /// <param name="destination">The destination exchange</param>
  365. /// <param name="routingKey">The routing key</param>
  366. /// <returns>A binding</returns>
  367. IBinding Bind(IExchange source, IExchange destination, string routingKey);
  368. /// <summary>
  369. /// Bind two exchanges. Does nothing if the binding already exists.
  370. /// </summary>
  371. /// <param name="source">The source exchange</param>
  372. /// <param name="destination">The destination exchange</param>
  373. /// <param name="routingKey">The routing key</param>
  374. /// <returns>A binding</returns>
  375. Task<IBinding> BindAsync(IExchange source, IExchange destination, string routingKey);
  376. /// <summary>
  377. /// Delete a binding
  378. /// </summary>
  379. /// <param name="binding">the binding to delete</param>
  380. void BindingDelete(IBinding binding);
  381. /// <summary>
  382. /// Get a message from the given queue.
  383. /// </summary>
  384. /// <typeparam name="T">The message type to get</typeparam>
  385. /// <param name="queue">The queue from which to retreive the message</param>
  386. /// <returns>An IBasicGetResult.</returns>
  387. IBasicGetResult<T> Get<T>(IQueue queue) where T : class;
  388. /// <summary>
  389. /// Get the raw message from the given queue.
  390. /// </summary>
  391. /// <param name="queue">The queue from which to retreive the message</param>
  392. /// <returns>An IBasicGetResult</returns>
  393. IBasicGetResult Get(IQueue queue);
  394. /// <summary>
  395. /// Counts messages in the given queue
  396. /// </summary>
  397. /// <param name="queue">The queue in which to count messages</param>
  398. /// <returns>The number of counted messages</returns>
  399. uint MessageCount(IQueue queue);
  400. /// <summary>
  401. /// True if the bus is connected, False if it is not.
  402. /// </summary>
  403. bool IsConnected { get; }
  404. /// <summary>
  405. /// Event fires when the bus has connected to a RabbitMQ broker.
  406. /// </summary>
  407. event EventHandler Connected;
  408. /// <summary>
  409. /// Event fires when the bus has disconnected from a RabbitMQ broker.
  410. /// </summary>
  411. event EventHandler Disconnected;
  412. /// <summary>
  413. /// Event fires when the bus gets blocked due to the broker running low on resources.
  414. /// </summary>
  415. event EventHandler<ConnectionBlockedEventArgs> Blocked;
  416. /// <summary>
  417. /// Event fires when the bus is unblocked.
  418. /// </summary>
  419. event EventHandler Unblocked;
  420. /// <summary>
  421. /// Event fires when a mandatory or immediate message is returned as un-routable
  422. /// </summary>
  423. event EventHandler<MessageReturnedEventArgs> MessageReturned;
  424. /// <summary>
  425. /// The IoC container that EasyNetQ uses to resolve its services.
  426. /// </summary>
  427. IContainer Container { get; }
  428. /// <summary>
  429. /// The conventions used by EasyNetQ to name its routing topology elements.
  430. /// </summary>
  431. IConventions Conventions { get; }
  432. }
  433. }