Exemple #1
0
    string SelectDestinationAddress(string endpoint, Func <EndpointInstance, string> resolveTransportAddress)
    {
        var candidates = endpointInstances.FindInstances(endpoint).Select(resolveTransportAddress).ToArray();
        var selected   = distributionPolicy.GetDistributionStrategy(endpoint, DistributionStrategyScope.Send).SelectDestination(candidates);

        return(selected);
    }
    async Task Send(Subscriber subscriber, string publisherEndpoint, string messageType, MessageIntentEnum intent, IRawEndpoint dispatcher)
    {
        var publisherInstances = endpointInstances.FindInstances(publisherEndpoint);
        var publisherAddresses = publisherInstances.Select(i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i))).ToArray();

        foreach (var publisherAddress in publisherAddresses)
        {
            Logger.Debug(
                $"Sending {intent} request for {messageType} to {publisherAddress} on behalf of {subscriber.TransportAddress}.");

            var subscriptionMessage = ControlMessageFactory.Create(intent);

            subscriptionMessage.Headers[Headers.SubscriptionMessageType]          = messageType;
            subscriptionMessage.Headers[Headers.ReplyToAddress]                   = dispatcher.TransportAddress;
            subscriptionMessage.Headers[Headers.SubscriberTransportAddress]       = dispatcher.TransportAddress;
            subscriptionMessage.Headers[Headers.SubscriberEndpoint]               = dispatcher.EndpointName;
            subscriptionMessage.Headers["NServiceBus.Bridge.DestinationEndpoint"] = publisherEndpoint;
            subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
            subscriptionMessage.Headers[Headers.NServiceBusVersion] = "6.3.1"; //The code has been copied from 6.3.1

            var transportOperation = new TransportOperation(subscriptionMessage, new UnicastAddressTag(publisherAddress));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), new TransportTransaction(),
                                      new ContextBag()).ConfigureAwait(false);
        }
    }
Exemple #3
0
    async Task Send(Subscriber subscriber, string publisherEndpoint, string messageType, MessageIntentEnum intent, IRawEndpoint dispatcher, InterBridgeRoutingSettings forwarding)
    {
        var typeFullName = messageType.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).First();

        if (!forwarding.PublisherTable.TryGetValue(typeFullName, out var nextHopEndpoint))
        {
            return;
        }

        var subscriptionMessage = ControlMessageFactory.Create(intent);

        if (publisherEndpoint != null)
        {
            subscriptionMessage.Headers["NServiceBus.Bridge.DestinationEndpoint"] = publisherEndpoint;
        }
        subscriptionMessage.Headers[Headers.SubscriptionMessageType]    = messageType;
        subscriptionMessage.Headers[Headers.ReplyToAddress]             = dispatcher.TransportAddress;
        subscriptionMessage.Headers[Headers.SubscriberTransportAddress] = dispatcher.TransportAddress;
        subscriptionMessage.Headers[Headers.SubscriberEndpoint]         = dispatcher.EndpointName;
        subscriptionMessage.Headers[Headers.TimeSent]           = DateTimeExtensions.ToWireFormattedString(DateTime.UtcNow);
        subscriptionMessage.Headers[Headers.NServiceBusVersion] = "6.3.1"; //The code has been copied from 6.3.1

        var publisherInstances = endpointInstances.FindInstances(nextHopEndpoint);
        var publisherAddresses = publisherInstances.Select(i => dispatcher.ToTransportAddress(LogicalAddress.CreateRemoteAddress(i))).ToArray();

        foreach (var publisherAddress in publisherAddresses)
        {
            Logger.Debug(
                $"Sending {intent} request for {messageType} to {publisherAddress} on behalf of {subscriber.TransportAddress}.");

            var transportOperation = new TransportOperation(subscriptionMessage, new UnicastAddressTag(publisherAddress));
            await dispatcher.Dispatch(new TransportOperations(transportOperation), new TransportTransaction(),
                                      new ContextBag()).ConfigureAwait(false);
        }
    }
        public void Should_default_to_single_instance_when_not_configured()
        {
            var instances      = new EndpointInstances();
            var salesInstances = instances.FindInstances("Sales");

            var singleInstance = salesInstances.Single();

            Assert.IsNull(singleInstance.Discriminator);
            Assert.IsEmpty(singleInstance.Properties);
        }
Exemple #5
0
    public Task Invoke(MulticastContext context, Func <PostroutingContext, Task> next)
    {
        var addresses = endpointInstances.FindInstances(context.DestinationEndpoint)
                        .Select(resolveTransportAddress)
                        .ToArray();

        var operations = addresses.Select(a => new TransportOperation(context.Message, new UnicastAddressTag(a)));

        return(next(new PostroutingContext(context.DestinationEndpoint, operations.ToArray(), context)));
    }
        public void Should_add_instances_grouped_by_endpoint_name()
        {
            var          instances     = new EndpointInstances();
            const string endpointName1 = "EndpointA";
            const string endpointName2 = "EndpointB";

            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(endpointName1),
                new EndpointInstance(endpointName2)
            });

            var salesInstances = instances.FindInstances(endpointName1);

            Assert.AreEqual(1, salesInstances.Count());

            var otherInstances = instances.FindInstances(endpointName2);

            Assert.AreEqual(1, otherInstances.Count());
        }
Exemple #7
0
    public Task Invoke(AnycastContext context, Func <PostroutingContext, Task> next)
    {
        var candidates = endpointInstances.FindInstances(context.DestinationEndpoint)
                         .Select(resolveTransportAddress)
                         .ToArray();

        var selected = distributionPolicy.GetDistributionStrategy(context.DestinationEndpoint, context.DistributionScope).SelectDestination(candidates);

        var operation = new TransportOperation(context.Message, new UnicastAddressTag(selected));

        return(next(new PostroutingContext(context.DestinationEndpoint, operation, context)));
    }
        public void Should_filter_out_duplicate_instances()
        {
            var instances = new EndpointInstances();
            var sales     = "Sales";

            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(sales, "dup"),
                new EndpointInstance(sales, "dup")
            });

            var salesInstances = instances.FindInstances(sales);

            Assert.AreEqual(1, salesInstances.Count());
        }
        public void Should_return_instances_configured_by_static_route()
        {
            var instances = new EndpointInstances();
            var sales     = "Sales";

            instances.AddOrReplaceInstances("A", new List <EndpointInstance>
            {
                new EndpointInstance(sales, "1"),
                new EndpointInstance(sales, "2")
            });

            var salesInstances = instances.FindInstances(sales);

            Assert.AreEqual(2, salesInstances.Count());
        }
Exemple #10
0
 IEnumerable <string> ResolveRoute(UnicastRoute route)
 {
     if (route.Instance != null)
     {
         yield return(resolveTransportAddress(route.Instance));
     }
     else if (route.PhysicalAddress != null)
     {
         yield return(route.PhysicalAddress);
     }
     else
     {
         foreach (var instance in endpointInstances.FindInstances(route.Endpoint))
         {
             yield return(resolveTransportAddress(instance));
         }
     }
 }
Exemple #11
0
 public IEnumerable <string> GetAllTopicExchangeAddresses()
 => endpointInstances.FindInstances(topicExchangeName)
 .Select(transportAddressTranslation);