1
0
mirror of synced 2024-12-22 09:13:07 +01:00

Merge branch 'release/2.5' into develop

This commit is contained in:
Mark van Renswoude 2021-06-09 10:18:27 +02:00
commit 8e69c79b94
15 changed files with 235 additions and 43 deletions

View File

@ -0,0 +1,52 @@
using System;
namespace Tapeti.Annotations
{
/// <inheritdoc />
/// <summary>
/// Can be attached to a message class to override or extend the generated routing key.
/// One of the intended scenarios is for versioning messages, where you want to add for example a
/// ".v2" postfix but keep the class name itself the same (in a different namespace of course).
/// </summary>
/// <remarks>
/// The implementation of IRoutingKeyStrategy must explicitly add support for this attribute for it to have effect.
/// The default implementation (TypeNameRoutingKeyStrategy) does, of course. Custom implementations can use
/// the Tapeti.Helpers.RoutingKeyHelper class to add support and keep up-to-date automatically.
///
/// When using EnableDeclareDurableQueues to automatically generate the queue bindings, both the sender and receiver must
/// use a Tapeti version >= 2.5 to support this attribute or the binding will differ from the sent routing key.
///
/// The routing keys are no longer used by Tapeti once the message is in the queue, the message handler
/// is instead based on the full message class name (thus including namespace), so if the binding is generated in any
/// other way this remark does not apply and prior versions of Tapeti are compatible.
/// </remarks>
[AttributeUsage(AttributeTargets.Class)]
public class RoutingKeyAttribute : Attribute
{
/// <summary>
/// If specified, the routing key strategy is skipped altogether and this value is used instead.
/// </summary>
/// <remarks>
/// The Prefix and Postfix properties will not have any effect if the Full property is specified.
/// </remarks>
public string Full { get; set; }
/// <summary>
/// If specified, the value generated by the default routing key strategy is prefixed with this value.
/// No dot will be added after this prefix, if you want to include it add it as part of the value.
/// </summary>
/// <remarks>
/// This property will not have any effect if the Full property is specified. Can be used in combination with Postfix.
/// </remarks>
public string Prefix { get; set; }
/// <summary>
/// If specified, the value generated by the default routing key strategy is postfixed with this value.
/// No dot will be added before this postfix, if you want to include it add it as part of the value.
/// </summary>
/// <remarks>
/// This property will not have any effect if the Full property is specified. Can be used in combination with Prefix.
/// </remarks>
public string Postfix { get; set; }
}
}

View File

@ -1,4 +1,6 @@
using System;
using FluentAssertions;
using Tapeti.Annotations;
using Tapeti.Default;
using Xunit;
@ -60,13 +62,54 @@ namespace Tapeti.Tests.Default
AssertRoutingKey("acr.test.mixed.case", typeof(ACRTestMIXEDCaseMESSAGE));
}
[RoutingKey(Prefix = "prefix.")]
private class PrefixAttributeTestMessage { }
[Fact]
public void Prefix()
{
AssertRoutingKey("prefix.prefix.attribute.test", typeof(PrefixAttributeTestMessage));
}
[RoutingKey(Postfix = ".postfix")]
private class PostfixAttributeTestMessage { }
[Fact]
public void Postfix()
{
AssertRoutingKey("postfix.attribute.test.postfix", typeof(PostfixAttributeTestMessage));
}
[RoutingKey(Prefix = "prefix.", Postfix = ".postfix")]
private class PrefixPostfixAttributeTestMessage { }
[Fact]
public void PrefixPostfix()
{
AssertRoutingKey("prefix.prefix.postfix.attribute.test.postfix", typeof(PrefixPostfixAttributeTestMessage));
}
[RoutingKey(Full = "andnowforsomethingcompletelydifferent", Prefix = "ignore.", Postfix = ".me")]
private class FullAttributeTestMessage { }
[Fact]
public void Full()
{
AssertRoutingKey("andnowforsomethingcompletelydifferent", typeof(FullAttributeTestMessage));
}
// ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
private static void AssertRoutingKey(string expected, Type messageType)
{
if (expected == null) throw new ArgumentNullException(nameof(expected));
if (messageType == null) throw new ArgumentNullException(nameof(messageType));
Assert.Equal(expected, new TypeNameRoutingKeyStrategy().GetRoutingKey(messageType));
var routingKey = new TypeNameRoutingKeyStrategy().GetRoutingKey(messageType);
routingKey.Should().Be(expected);
}
}
// ReSharper restore InconsistentNaming

View File

@ -1,4 +1,5 @@
using Tapeti.Helpers;
using FluentAssertions;
using Tapeti.Helpers;
using Xunit;
namespace Tapeti.Tests.Helpers
@ -185,12 +186,12 @@ namespace Tapeti.Tests.Helpers
{
var parsed = ConnectionStringParser.Parse(connectionstring);
Assert.Equal(expected.HostName, parsed.HostName);
Assert.Equal(expected.Port, parsed.Port);
Assert.Equal(expected.VirtualHost, parsed.VirtualHost);
Assert.Equal(expected.Username, parsed.Username);
Assert.Equal(expected.Password, parsed.Password);
Assert.Equal(expected.PrefetchCount, parsed.PrefetchCount);
parsed.HostName.Should().Be(expected.HostName);
parsed.Port.Should().Be(expected.Port);
parsed.VirtualHost.Should().Be(expected.VirtualHost);
parsed.Username.Should().Be(expected.Username);
parsed.Password.Should().Be(expected.Password);
parsed.PrefetchCount.Should().Be(expected.PrefetchCount);
}
}
}

View File

@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

View File

@ -2,11 +2,10 @@
namespace Tapeti.Config
{
/// <inheritdoc />
/// <summary>
/// Provides information about the message currently being handled.
/// </summary>
public interface IMessageContext : IDisposable
public interface IMessageContext : IAsyncDisposable, IDisposable
{
/// <summary>
/// Provides access to the Tapeti config.
@ -49,7 +48,7 @@ namespace Tapeti.Config
/// middleware stages (mostly for IControllerMiddlewareBase descendants).
/// </summary>
/// <param name="key">A unique key. It is recommended to prefix it with the package name which hosts the middleware to prevent conflicts</param>
/// <param name="value">Will be disposed if the value implements IDisposable</param>
/// <param name="value">Will be disposed if the value implements IDisposable or IAsyncDisposable</param>
void Store(string key, object value);
/// <summary>

View File

@ -25,8 +25,17 @@ namespace Tapeti.Connection
}
public async ValueTask DisposeAsync()
{
if (consuming)
await Stop();
}
public void Dispose()
{
if (consuming)
Stop().GetAwaiter().GetResult();
}

View File

@ -1,4 +1,5 @@
using Tapeti.Config;
using System.Threading.Tasks;
using Tapeti.Config;
namespace Tapeti.Default
{
@ -41,9 +42,19 @@ namespace Tapeti.Default
/// <inheritdoc />
public void Dispose()
{
// Do not call decoratedContext.Dispose - by design
}
/// <inheritdoc />
public ValueTask DisposeAsync()
{
// Do not call decoratedContext.DisposeAsync - by design
return default;
}
/// <inheritdoc />
public void Store(string key, object value)
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Tapeti.Config;
namespace Tapeti.Default
@ -39,6 +40,18 @@ namespace Tapeti.Default
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
foreach (var item in items.Values)
{
if (item is IAsyncDisposable asyncDisposable)
await asyncDisposable.DisposeAsync();
}
}
/// <inheritdoc />
public void Store(string key, object value)
{

View File

@ -3,6 +3,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Tapeti.Helpers;
namespace Tapeti.Default
{
@ -45,15 +46,18 @@ namespace Tapeti.Default
/// <param name="messageType"></param>
protected virtual string BuildRoutingKey(Type messageType)
{
// Split PascalCase into dot-separated parts. If the class name ends in "Message" leave that out.
var words = SplitPascalCase(messageType.Name);
if (words == null)
return "";
return RoutingKeyHelper.Decorate(messageType, () =>
{
// Split PascalCase into dot-separated parts. If the class name ends in "Message" leave that out.
var words = SplitPascalCase(messageType.Name);
if (words == null)
return "";
if (words.Count > 1 && words.Last().Equals("Message", StringComparison.InvariantCultureIgnoreCase))
words.RemoveAt(words.Count - 1);
if (words.Count > 1 && words.Last().Equals("Message", StringComparison.InvariantCultureIgnoreCase))
words.RemoveAt(words.Count - 1);
return string.Join(".", words.Select(s => s.ToLower()));
return string.Join(".", words.Select(s => s.ToLower()));
});
}

View File

@ -0,0 +1,31 @@
using System;
using System.Reflection;
using Tapeti.Annotations;
namespace Tapeti.Helpers
{
/// <summary>
/// Helper class for compositing a routing key with support for the RoutingKey attribute.
/// Should be used by all implementations of IRoutingKeyStrategy unless there is a good reason not to.
/// </summary>
public static class RoutingKeyHelper
{
/// <summary>
/// Applies the RoutingKey attribute for the specified messageClass.
/// </summary>
/// <param name="messageType"></param>
/// <param name="applyStrategy">Called when the strategy needs to be applied to the message class to generate a routing key.
/// Will not be called if the Full property is specified on the RoutingKey attribute.</param>
public static string Decorate(Type messageType, Func<string> applyStrategy)
{
var routingKeyAttribute = messageType.GetCustomAttribute<RoutingKeyAttribute>();
if (routingKeyAttribute == null)
return applyStrategy();
if (!string.IsNullOrEmpty(routingKeyAttribute.Full))
return routingKeyAttribute.Full;
return (routingKeyAttribute.Prefix ?? "") + applyStrategy() + (routingKeyAttribute.Postfix ?? "");
}
}
}

View File

@ -47,11 +47,10 @@ namespace Tapeti
public delegate void DisconnectedEventHandler(object sender, DisconnectedEventArgs e);
/// <inheritdoc />
/// <summary>
/// Represents a connection to a RabbitMQ server
/// </summary>
public interface IConnection : IDisposable
public interface IConnection : IAsyncDisposable, IDisposable
{
/// <summary>
/// Creates a subscriber to consume messages from the bound queues.

View File

@ -5,11 +5,10 @@ using System.Threading.Tasks;
namespace Tapeti
{
/// <inheritdoc />
/// <summary>
/// Manages subscriptions to queues as configured by the bindings.
/// </summary>
public interface ISubscriber : IDisposable
public interface ISubscriber : IAsyncDisposable, IDisposable
{
/// <summary>
/// Starts consuming from the subscribed queues if not already started.

View File

@ -18,6 +18,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RabbitMQ.Client" Version="6.2.1" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />

View File

@ -30,6 +30,8 @@ namespace Tapeti
private readonly Lazy<ITapetiClient> client;
private TapetiSubscriber subscriber;
private bool disposed;
/// <summary>
/// Creates a new instance of a TapetiConnection and registers a default IPublisher
/// in the IoC container as provided in the config.
@ -97,12 +99,26 @@ namespace Tapeti
/// <inheritdoc />
public void Dispose()
{
Close().Wait();
subscriber?.Dispose();
if (!disposed)
DisposeAsync().GetAwaiter().GetResult();
}
/// <inheritdoc />
public async ValueTask DisposeAsync()
{
if (disposed)
return;
if (subscriber != null)
await subscriber.DisposeAsync();
await Close();
disposed = true;
}
private class ConnectionEventListener: IConnectionEventListener
{
private readonly TapetiConnection owner;

View File

@ -11,44 +11,57 @@ before_build:
after_build:
# Tapeti
- cmd: dotnet pack --output output Tapeti\Tapeti.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti\Tapeti.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Annotations
- cmd: dotnet pack --output output Tapeti.Annotations\Tapeti.Annotations.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Annotations\Tapeti.Annotations.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Annotations.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Annotations.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.DataAnnotations
- cmd: dotnet pack --output output Tapeti.DataAnnotations\Tapeti.DataAnnotations.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.DataAnnotations\Tapeti.DataAnnotations.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.DataAnnotations.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.DataAnnotations.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.DataAnnotations.Extensions
- cmd: dotnet pack --output output Tapeti.DataAnnotations.Extensions\Tapeti.DataAnnotations.Extensions.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.DataAnnotations.Extensions\Tapeti.DataAnnotations.Extensions.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.DataAnnotations.Extensions.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.DataAnnotations.Extensions.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Flow
- cmd: dotnet pack --output output Tapeti.Flow\Tapeti.Flow.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Flow\Tapeti.Flow.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Flow.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Flow.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Flow.SQL
- cmd: dotnet pack --output output Tapeti.Flow.SQL\Tapeti.Flow.SQL.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Flow.SQL\Tapeti.Flow.SQL.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Flow.SQL.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Flow.SQL.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Transient
- cmd: dotnet pack --output output Tapeti.Transient\Tapeti.Transient.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Transient\Tapeti.Transient.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Transient.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Transient.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Serilog
- cmd: dotnet pack --output output Tapeti.Serilog\Tapeti.Serilog.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Serilog\Tapeti.Serilog.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Serilog.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Serilog.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.SimpleInjector
- cmd: dotnet pack --output output Tapeti.SimpleInjector\Tapeti.SimpleInjector.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.SimpleInjector\Tapeti.SimpleInjector.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.SimpleInjector.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.SimpleInjector.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Autofac
- cmd: dotnet pack --output output Tapeti.Autofac\Tapeti.Autofac.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Autofac\Tapeti.Autofac.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Autofac.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Autofac.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.CastleWindsor
- cmd: dotnet pack --output output Tapeti.CastleWindsor\Tapeti.CastleWindsor.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.CastleWindsor\Tapeti.CastleWindsor.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.CastleWindsor.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.CastleWindsor.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.Ninject
- cmd: dotnet pack --output output Tapeti.Ninject\Tapeti.Ninject.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.Ninject\Tapeti.Ninject.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.Ninject.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.Ninject.%GitVersion_NuGetVersion%.snupkg"
# Tapeti.UnityContainer
- cmd: dotnet pack --output output Tapeti.UnityContainer\Tapeti.UnityContainer.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: dotnet pack -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg --output output Tapeti.UnityContainer\Tapeti.UnityContainer.csproj /p:Configuration=Release /p:Version=%GitVersion_NuGetVersion%
- cmd: appveyor PushArtifact "output\Tapeti.UnityContainer.%GitVersion_NuGetVersion%.nupkg"
- cmd: appveyor PushArtifact "output\Tapeti.UnityContainer.%GitVersion_NuGetVersion%.snupkg"
build:
project: Tapeti.sln
@ -65,4 +78,4 @@ deploy:
secure: 3WCSZAzan66vEmHZ1q3XzfOfucuAQiA+SiCDChO/gswbxfIXUpiM1eMNASDa3qWH
skip_symbols: false
artifact: /.*\.nupkg/
artifact: /.*(\.|\.s)nupkg/