Example #1
0
 public WebhookPosterTest()
 {
     ForwarderConfiguration = new ForwarderConfiguration();
 }
 public HubEventUnpackerTest()
 {
     Configuration = new ForwarderConfiguration();
 }
Example #3
0
        private static async Task MainAsync()
        {
            var instrumentationKey       = Environment.GetEnvironmentVariable("Payments.InstrumentationKey", EnvironmentVariableTarget.Process);
            var license                  = Environment.GetEnvironmentVariable("NServiceBus.License", EnvironmentVariableTarget.Process);
            var ordersConnectionString   = Environment.GetEnvironmentVariable("Orders.ConnectionString", EnvironmentVariableTarget.Process);
            var paymentsConnectionString = Environment.GetEnvironmentVariable("Payments.ConnectionString", EnvironmentVariableTarget.Process);

            System.Net.ServicePointManager.DefaultConnectionLimit = Int32.MaxValue;

            TelemetryConfiguration.Active.InstrumentationKey = instrumentationKey;
            var telemetryClient = new TelemetryClient();

            var defaultFactory = LogManager.Use <DefaultFactory>();

            defaultFactory.Level(LogLevel.Info);

            var endpointConfig = new EndpointConfiguration("Payments");

            if (!string.IsNullOrEmpty(license))
            {
                endpointConfig.License(license);
            }

            endpointConfig.UsePersistence <InMemoryPersistence>();
            endpointConfig.SendFailedMessagesTo("error");
            var transport = endpointConfig.UseTransport <AzureServiceBusTransport>();

            transport.ConnectionString(paymentsConnectionString);
            var topology  = transport.UseForwardingTopology();
            var factories = transport.MessagingFactories();
            var receivers = transport.MessageReceivers();

            var perReceiverConcurrency = 10;
            var numberOfReceivers      = 2; // Premium messaging has 2 partitions
            var globalConcurrency      = numberOfReceivers * perReceiverConcurrency;

            endpointConfig.LimitMessageProcessingConcurrencyTo(globalConcurrency);
            factories.NumberOfMessagingFactoriesPerNamespace(numberOfReceivers * 2); //Bus receiver, bus sender
            transport.NumberOfClientsPerEntity(numberOfReceivers);
            factories.BatchFlushInterval(TimeSpan.FromMilliseconds(100));

            receivers.PrefetchCount(1000);                                               // The more we prefetch, the better the throughput will be, needs to be balanced though as you can only pull so many messages per batch
            topology.NumberOfEntitiesInBundle(1);                                        // Only use 1 bundle topic, there is no benefit to using more and Particular are going to remove this support moving forward
            transport.Transactions(TransportTransactionMode.ReceiveOnly);                // Use peek lock, vastly quicker than
            transport.BrokeredMessageBodyType(SupportedBrokeredMessageBodyTypes.Stream); // Need to use this for non-NSB integrations - will be what Particular use moving forward too
            transport.TransportType(TransportType.Amqp);                                 // Use this rather than netmessaging, allows more connections to the namespace and is an open standard

            // See here for a better example of this that handles ungraceful shutdowns - https://github.com/Particular/docs.particular.net/blob/master/samples/application-insights/Metrics_1/Endpoint/ApplicationInsightsFeature.cs
            var metricOptions = endpointConfig.EnableMetrics();

            metricOptions.RegisterObservers(context =>
            {
                foreach (var duration in context.Durations)
                {
                    duration.Register(observer: durationLength => telemetryClient.TrackMetric(new MetricTelemetry(duration.Name, durationLength.TotalMilliseconds)));
                }

                foreach (var signal in context.Signals)
                {
                    signal.Register(observer: () => telemetryClient.TrackEvent(new EventTelemetry(signal.Name)));
                }
            });

            var endpoint = await Endpoint.Start(endpointConfig).ConfigureAwait(false);

            var forwarderConfig = new ForwarderConfiguration(
                new ForwarderSourceConfiguration(500, () => CreateMessageReceiver(ordersConnectionString, "Payments")),
                new ForwarderDestinationConfiguration("Payments", () => CreateMessageForwarder(paymentsConnectionString, "Payments")))
                                  .UsingLogger(new Logger(LogManager.GetLogger <Forwarder>()))
                                  .WithConcurrencyOf(3);

            var forwarder = new Forwarder(forwarderConfig);

            await CreateSubscriptionEntitiesIfRequired(ordersConnectionString, "Returns", "Payments");

            forwarder.Start();
        }
Example #4
0
        private static List <ForwarderConfiguration> GetConfiguration(IBackgroundLogger errorLogger)
        {
            List <ForwarderConfiguration> configs = new List <ForwarderConfiguration>();

            try {
                // Read the XML configuration file.
                XDocument doc;
                using (FileStream fs = new FileStream(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "configuration.xml"), FileMode.Open, FileAccess.Read)) {
                    doc = XDocument.Load(fs);
                }

                XElement root = doc.Root;
                if (root.Name.LocalName == "TcpForwarderConfiguration")
                {
                    foreach (XNode node in root.Nodes())
                    {
                        try {
                            if (node is XElement && ((XElement)node).Name.LocalName == "Forwarder")
                            {
                                // Found an entry.
                                XElement el = (XElement)node;

                                XAttribute remoteHost = el.Attribute("remoteHost");
                                XAttribute remotePort = el.Attribute("remotePort");
                                XAttribute localPort  = el.Attribute("localPort");

                                if (remoteHost == null || remotePort == null || localPort == null)
                                {
                                    throw new ArgumentException("One of the required attributes (maxConcurrentConnections, remoteHost, remotePort, localPort) is missing.");
                                }

                                int        maxConcurrentConnections;
                                XAttribute maxConcurrentConnectionsAttr = el.Attribute("maxConcurrentConnections");
                                if (maxConcurrentConnectionsAttr != null)
                                {
                                    maxConcurrentConnections = int.Parse(maxConcurrentConnectionsAttr.Value, CultureInfo.InvariantCulture);
                                }
                                else
                                {
                                    maxConcurrentConnections = 10000;
                                }

                                string     remoteSslHostname = null;
                                XAttribute at = el.Attribute("remoteSslHostname");
                                if (at != null)
                                {
                                    remoteSslHostname = at.Value;
                                }

                                X509Certificate2 cert = null;
                                at = el.Attribute("localSslCertFingerprint");
                                if (at != null)
                                {
                                    cert = TcpConnectionForwarderUtils.GetCurrentUserOrLocalMachineCertificateFromFingerprint(at.Value);
                                    if (cert == null)
                                    {
                                        throw new Exception("The certificate with fingerprint \"" + at.Value + "\" was not found.");
                                    }

                                    // check if we have enough privileges to get the private key of the certificate (when using the local machine's store,
                                    // the program might need administrative rights to access the private key)
                                    try {
                                        System.Security.Cryptography.AsymmetricAlgorithm am = cert.PrivateKey;
                                        GC.KeepAlive(am); // Ensure the compiler does not optimize-away the above call
                                    } catch (Exception ex) {
                                        if (ExceptionUtils.ShouldExceptionBeRethrown(ex))
                                        {
                                            throw;
                                        }

                                        throw new Exception("Error when retrieving the private key of the certificate. Make sure "
                                                            + "you run this program with correct privileges.\r\n\r\n"
                                                            + ex.GetType().ToString() + ": " + ex.Message, ex);
                                    }
                                }

                                ForwarderConfiguration conf = new ForwarderConfiguration()
                                {
                                    MaxConcurrentConnections = maxConcurrentConnections,
                                    RemoteHost        = remoteHost.Value,
                                    RemotePort        = ushort.Parse(remotePort.Value, CultureInfo.InvariantCulture),
                                    LocalPort         = ushort.Parse(localPort.Value, CultureInfo.InvariantCulture),
                                    RemoteSslHostname = remoteSslHostname,
                                    LocalSslCert      = cert
                                };

                                configs.Add(conf);
                            }
                        } catch (Exception ex) {
                            if (ExceptionUtils.ShouldExceptionBeRethrown(ex))
                            {
                                throw;
                            }

                            errorLogger.LogError("An error occured when parsing the following XML configuration entry:\r\n"
                                                 + node.ToString() + "\r\n\r\nError Details:\r\n" + FormatExceptionForLog(ex));
                        }
                    }
                }
            } catch (Exception ex) {
                if (ExceptionUtils.ShouldExceptionBeRethrown(ex))
                {
                    throw;
                }

                errorLogger.LogError("An error when reading the XML configuration.\r\n\r\nError Details:\r\n" + FormatExceptionForLog(ex));
            }

            return(configs);
        }