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.

Tokens
9.6K
Snippets
54
Records
63
Agent score
75%

What's inside ArtemisNetClient

  1. Understand the Apache Artemis Address messaging model

    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.
  2. 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.
  3. Manage message durability and delivery guarantees in ActiveMQ Artemis

    ActiveMQ Artemis supports two durability modes that determine how messages are persisted and acknowledged:

    1. 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.
    2. 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 Send method, which is non-blocking and does not wait for broker confirmation.
  4. Use TestKit for in-memory message broker testing

    The TestKit class is the core of the ArtemisNetClient.Testing package. 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);
  5. Receive messages using IConsumer

    Create an IConsumer by specifying an address and routing type. Messages are delivered automatically and buffered. Use ReceiveAsync to retrieve a message from the buffer. If the buffer is empty, the method waits asynchronously. A CancellationToken should 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);
  6. 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 IRecoveryPolicy interface, which determines the number of retry attempts and the delay between them. To retry indefinitely, set RetryCount to int.MaxValue.

    If the initial connection fails, automatic recovery also triggers. To prevent ConnectionFactory.CreateAsync from waiting indefinitely when using a permanent recovery policy, pass a CancellationToken to the method.

    public interface IRecoveryPolicy
    {
        int RetryCount { get; }
        TimeSpan GetDelay(int attempt);
    }
  7. Install ArtemisNetClient DI and Hosting packages

    To integrate ArtemisNetClient with Microsoft.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.Hosting
  8. Manage 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 Priority is set to null, 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 Message object takes precedence over the priority configured on the Producer.
  9. Send multiple messages atomically using Transactions

    Use the Transaction class to ensure a group of messages are sent in an 'all or nothing' manner. Messages sent within a transaction are not visible to consumers until CommitAsync() 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();
    }