Skip to content

Document outcome #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions RabbitMQ.AMQP.Client/IPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,54 @@ public PublisherException(string message) : base(message)
}
}

/// <summary>
/// Represents the status of a publish operation.
/// Accepted: The message was accepted for publication.
/// Rejected: The message was rejected by the broker.
/// Released: The message was released by the broker.
/// </summary>
public enum OutcomeState
{
Accepted,
Rejected,
Released,
}

/// <summary>
/// PublishOutcome represents the outcome of a publish operation.
/// It contains the state of the outcome and an error if the outcome is not successful.
/// </summary>

public class PublishOutcome
{
private readonly OutcomeState _state;
private readonly Error? _error;

public PublishOutcome(OutcomeState state, Error? error)
{
_state = state;
_error = error;
State = state;
Error = error;
}

public OutcomeState State => _state;
public Error? Error => _error;
public OutcomeState State { get; }

public Error? Error { get; }
}

public class PublishResult
{
private IMessage _message;
private PublishOutcome _outcome;

public PublishResult(IMessage message, PublishOutcome outcome)
{
_message = message;
_outcome = outcome;
Message = message;
Outcome = outcome;
}

public IMessage Message => _message;
public PublishOutcome Outcome => _outcome;
public IMessage Message { get; }

public PublishOutcome Outcome { get; }
}

/// <summary>
/// Interface for publishing messages to an AMQP broker.
/// Implementations of this interface are expected to be thread-safe.
/// </summary>
public interface IPublisher : ILifeCycle
{
Task<PublishResult> PublishAsync(IMessage message, CancellationToken cancellationToken = default);
Expand Down
24 changes: 12 additions & 12 deletions RabbitMQ.AMQP.Client/Impl/AmqpPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ await base.OpenAsync()
}
}

// TODO: Consider implementing this method with the send method
// a way to send a batch of messages

// protected override async Task<int> ExecuteAsync(SenderLink link)
// {
// int batch = this.random.Next(1, this.role.Args.Batch);
// Message[] messages = CreateMessages(this.id, this.total, batch);
// await Task.WhenAll(messages.Select(m => link.SendAsync(m)));
// this.total += batch;
// return batch;
// }


/// <summary>
/// Publishes a message to the broker in an asynchronous manner.
/// The PublishResult is synchronous. In order to increase the performance
/// you can use more tasks to publish messages in parallel
/// </summary>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="NotSupportedException"></exception>
/// <exception cref="PublisherException"></exception>
public async Task<PublishResult> PublishAsync(IMessage message, CancellationToken cancellationToken = default)
{
ThrowIfClosed();
Expand Down
8 changes: 6 additions & 2 deletions docs/Examples/GettingStarted/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@
case OutcomeState.Accepted:
Trace.WriteLine(TraceLevel.Information, $"[Publisher] Message: {message.Body()} confirmed");
break;
case OutcomeState.Released:
Trace.WriteLine(TraceLevel.Information, $"[Publisher] Message: {message.Body()} Released");
break;


case OutcomeState.Rejected:
Trace.WriteLine(TraceLevel.Error,
$"outcome result, state: {pr.Outcome.State}, message_id: " +
$"{message.MessageId()}, error: {pr.Outcome.Error}");
$"[Publisher] Message: {message.Body()} Rejected with error: {pr.Outcome.Error}");
break;
default:
throw new ArgumentOutOfRangeException();
Expand Down
Loading