Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from stone-payments/feature/add-expiration-to-…
Browse files Browse the repository at this point in the history
…message

add expiration when publishing message
  • Loading branch information
lommez authored Sep 11, 2018
2 parents 265fdc6 + 5196ec3 commit 14a0da1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions RabbitMQ.Abstraction/Messaging/Interfaces/IQueueClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace RabbitMQ.Abstraction.Messaging.Interfaces
{
public interface IQueueClient : IDisposable
{
Task PublishAsync<T>(string exchangeName, string routingKey, T content, byte? priority = null);
Task PublishAsync<T>(IModel model, string exchangeName, string routingKey, T content, byte? priority = null);
Task PublishAsync<T>(string exchangeName, string routingKey, T content, byte? priority = null, TimeSpan? delay = null);
Task PublishAsync<T>(IModel model, string exchangeName, string routingKey, T content, byte? priority = null, TimeSpan? delay = null);
Task BatchPublishAsync<T>(string exchangeName, string routingKey, IEnumerable<T> contentList, byte? priority = null);
Task BatchPublishAsync<T>(IModel model, string exchangeName, string routingKey, IEnumerable<T> contentList, byte? priority = null);
Task BatchPublishTransactionalAsync<T>(string exchangeName, string routingKey, IEnumerable<T> contentList, byte? priority = null);
Expand Down
14 changes: 12 additions & 2 deletions RabbitMQ.Abstraction/Messaging/RabbitMQClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static HttpClient GetHttpClient(string username, string password, string
};
}

public Task PublishAsync<T>(string exchangeName, string routingKey, T content, byte? priority = null)
public Task PublishAsync<T>(string exchangeName, string routingKey, T content, byte? priority = null, TimeSpan? delay = null)
{
return Task.Factory.StartNew(() =>
{
Expand All @@ -141,13 +141,18 @@ public Task PublishAsync<T>(string exchangeName, string routingKey, T content, b
props.Priority = priority.Value;
}

if (delay.HasValue)
{
props.Expiration = delay.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
}

var payload = Encoding.UTF8.GetBytes(serializedContent);
_modelPublisher.BasicPublish(exchangeName, routingKey, props, payload);
}
});
}

public Task PublishAsync<T>(IModel model, string exchangeName, string routingKey, T content, byte? priority = null)
public Task PublishAsync<T>(IModel model, string exchangeName, string routingKey, T content, byte? priority = null, TimeSpan? delay = null)
{
return Task.Factory.StartNew(() =>
{
Expand All @@ -161,6 +166,11 @@ public Task PublishAsync<T>(IModel model, string exchangeName, string routingKey
props.Priority = priority.Value;
}

if (delay.HasValue)
{
props.Expiration = delay.Value.TotalMilliseconds.ToString(CultureInfo.InvariantCulture);
}

var payload = Encoding.UTF8.GetBytes(serializedContent);
model.BasicPublish(exchangeName, routingKey, props, payload);
});
Expand Down
5 changes: 3 additions & 2 deletions RabbitMQ.Abstraction/Messaging/RabbitMQConsumerContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RabbitMQ.Abstraction.Messaging.Interfaces;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -16,9 +17,9 @@ public RabbitMQConsumerContext(IQueueClient queueClient, IModel model)
_queueClient = queueClient;
}

public Task PublishAsync<T>(string exchangeName, string routingKey, T content, byte? priority = null)
public Task PublishAsync<T>(string exchangeName, string routingKey, T content, byte? priority = null, TimeSpan? delay = null)
{
return _queueClient.PublishAsync(_model, exchangeName, routingKey, content, priority);
return _queueClient.PublishAsync(_model, exchangeName, routingKey, content, priority, delay);
}

public Task BatchPublishAsync<T>(string exchangeName, string routingKey, IEnumerable<T> contentList,
Expand Down
2 changes: 1 addition & 1 deletion RabbitMQ.Abstraction/RabbitMQ.Abstraction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageId>Stone.RabbitMQ.Abstraction</PackageId>
<PackageVersion>2.0.1-beta3</PackageVersion>
<PackageVersion>2.0.1-beta5</PackageVersion>
<Title>RabbitMQ Abstraction</Title>
<Authors>Stone Pagamentos</Authors>
<Description>RabbitMQ Abstraction for messaging workflow and infrastructure management</Description>
Expand Down

0 comments on commit 14a0da1

Please sign in to comment.