ArtemisNetClient .NET Client for Apache Artemis
website·Indexed 30 days ago
https://havret.github.io/dotnet-activemq-artemis-client/A lightweight, .NET Standard compliant library built on top of AmqpNetLite for interacting with Apache Artemis. The client supports asynchronous messaging, transactions, message filtering, request-reply patterns, and provides integration with Microsoft.Extensions.DependencyInjection via ArtemisNetClient.Extensions.DependencyInjection.
What's inside ArtemisNetClient
- In Apache Artemis, producers do not send messages directly to queues. Instead, they send messages to an 'Address', which acts as a message endpoint. The Address receives the message and determines how to distribute it to attached queues based on the configured routing type. The producer remains unaware of whether the message is delivered to one queue, multiple queues, or discarded.
Performance and architecture of .NET Client for Apache Artemis
The .NET Client for Apache Artemis is designed to be fully asynchronous from the ground up. It functions as a lightweight wrapper around AmqpNetLite, ensuring minimal overhead and high performance.Compatibility and requirements for .NET Client for Apache Artemis
The .NET Client for Apache Artemis is compliant with .NET Standard 2.0, making it compatible for use in any .NET Core application.Manage message durability and delivery guarantees in ActiveMQ Artemis
ActiveMQ Artemis supports two durability modes that determine how messages are persisted and acknowledged:
- Durable: The broker persists the message to disk before sending a confirmation frame (ack) back to the client. This provides at-least-once delivery semantics but incurs higher overhead. This is the default behavior for
SendAsync. - Nondurable: The broker does not persist the message. This provides at-most-once delivery semantics and offers higher performance with lower overhead. This is the default behavior for the
Sendmethod, which is non-blocking and does not wait for broker confirmation.
- Durable: The broker persists the message to disk before sending a confirmation frame (ack) back to the client. This provides at-least-once delivery semantics but incurs higher overhead. This is the default behavior for
Use TestKit for in-memory message broker testing
TheTestKitclass is the core of theArtemisNetClient.Testingpackage. It spins up an in-memory message broker that supports the AMQP protocol, allowing a system under test to connect, send, and receive messages without requiring a full external broker installation.var endpoint = Endpoint.Create( host: "localhost", port: 5672, "artemis", "artemis" ); using var testKit = new TestKit(endpoint);Install ArtemisNetClient.Testing via NuGet
TheArtemisNetClient.Testingpackage provides tools to test messaging-dependent parts of an application in a controlled environment using an in-memory message broker.dotnet add package ArtemisNetClient.TestingReceive messages using IConsumer
Create anIConsumerby specifying an address and routing type. Messages are delivered automatically and buffered. UseReceiveAsyncto retrieve a message from the buffer. If the buffer is empty, the method waits asynchronously. ACancellationTokenshould be used to handle application shutdown or timeouts.var consumer = await connection.CreateConsumerAsync("a1", RoutingType.Anycast); // Simple receive var message = await consumer.ReceiveAsync(); // Receive with cancellation token for graceful shutdown var cts = new CancellationTokenSource(); var messageWithToken = await consumer.ReceiveAsync(cts.Token);Configure automatic connection recovery in .NET ActiveMQ Artemis client
The client library supports automatic recovery of connections, producers, and consumers, which is enabled by default. Recovery behavior is governed by the
IRecoveryPolicyinterface, which determines the number of retry attempts and the delay between them. To retry indefinitely, setRetryCounttoint.MaxValue.If the initial connection fails, automatic recovery also triggers. To prevent
ConnectionFactory.CreateAsyncfrom waiting indefinitely when using a permanent recovery policy, pass aCancellationTokento the method.public interface IRecoveryPolicy { int RetryCount { get; } TimeSpan GetDelay(int attempt); }Install ArtemisNetClient DI and Hosting packages
To integrate ArtemisNetClient withMicrosoft.Extensions.DependencyInjection, install the following NuGet packages based on your application type. For ASP.NET Core or generic hosted applications, both packages are required.# For DI-only registration dotnet add package ArtemisNetClient.Extensions.DependencyInjection # For ASP.NET Core or generic hosted applications dotnet add package ArtemisNetClient.Extensions.HostingInstall .NET Client for Apache Artemis
The .NET Client for Apache Artemis is a lightweight library built on AmqpNetLite. It is distributed via NuGet and can be installed using the dotnet CLI.dotnet add package ArtemisNetClientManage message priority in ActiveMQ Artemis .NET client
Message priority defines the importance of a message, where higher priority messages are delivered before lower priority ones. Messages with the same priority are delivered in the order they were sent.
Key priority rules:
- Priority levels range from 0 (lowest) to 9 (highest).
- If
Priorityis set tonull, the message defaults to normal priority (4). - Priority can be set at the Producer level or the Message level.
- A priority explicitly set on a
Messageobject takes precedence over the priority configured on theProducer.
Send multiple messages atomically using Transactions
Use theTransactionclass to ensure a group of messages are sent in an 'all or nothing' manner. Messages sent within a transaction are not visible to consumers untilCommitAsync()is called. If any operation fails before the commit, no messages from that transaction will be delivered.var connectionFactory = new ConnectionFactory(); var endpoint = Endpoint.Create("localhost", 5672, "guest", "guest"); var connection = await connectionFactory.CreateAsync(endpoint); await using (var transaction = new Transaction()) { await producer.SendAsync( address: "credit-queue", message: new Message("credit:500,account:a"), transaction: transaction ); await producer.SendAsync( address: "debit-queue", message: new Message("debit:500,account:b"), transaction: transaction); await transaction.CommitAsync(); }