public async Task ExecuteAsync(ISendEndpointProvider endpointProvider, ProducerOptions options, CancellationToken stoppingToken)
        {
            var endpoint = await endpointProvider.GetSendEndpoint(new Uri(@"queue:record-timestamp"));

            while (!stoppingToken.IsCancellationRequested && _executionCount < options.MaxSendCount)
            {
                Task[] tasks = new Task[options.MaxSendConcurrency];

                for (int i = 0; i < tasks.Length; i++)
                {
                    _logger.LogInformation("Sending message {Index}/{Total}, total = {ExecutionCount} ...", i, options.MaxSendConcurrency, _executionCount);

                    tasks[i] = endpoint.Send <RecordTimestamp>(new
                    {
                        TaskId     = i,
                        ThreadId   = Thread.CurrentThread.ManagedThreadId,
                        ProducerId = Id,
                        SentTime   = DateTime.UtcNow
                    });

                    _executionCount++;
                }

                Task.WaitAll(tasks);

                if (options.Interval > 0)
                {
                    await Task.Delay(options.Interval, stoppingToken);
                }
            }
        }
Ejemplo n.º 2
0
 public ProducerStreamFactory(ConnectionPool connectionPool, ProducerOptions options, IFaultStrategy faultStrategy)
 {
     _connectionPool = connectionPool;
     _options        = options;
     _faultStrategy  = faultStrategy;
     _sequenceId     = new SequenceId(options.InitialSequenceId);
 }
Ejemplo n.º 3
0
        protected ProducerOptions GetProducerOptions()
        {
            var options = new ProducerOptions();

            options.PostProcess();
            return(options);
        }
Ejemplo n.º 4
0
 public Producer(
     Uri serviceUrl,
     ProducerOptions <TMessage> options,
     ProcessManager processManager,
     IHandleException exceptionHandler,
     IConnectionPool connectionPool,
     ICompressorFactory?compressorFactory)
 {
     _operationName = $"{options.Topic} send";
     _tags          = new KeyValuePair <string, object?>[]
     {
         new KeyValuePair <string, object?>("messaging.destination", options.Topic),
         new KeyValuePair <string, object?>("messaging.destination_kind", "topic"),
         new KeyValuePair <string, object?>("messaging.system", "pulsar"),
         new KeyValuePair <string, object?>("messaging.url", serviceUrl),
     };
     _sequenceId        = new SequenceId(options.InitialSequenceId);
     _state             = new StateManager <ProducerState>(ProducerState.Disconnected, ProducerState.Closed, ProducerState.Faulted);
     ServiceUrl         = serviceUrl;
     Topic              = options.Topic;
     _isDisposed        = 0;
     _options           = options;
     _exceptionHandler  = exceptionHandler;
     _connectionPool    = connectionPool;
     _compressorFactory = compressorFactory;
     _processManager    = processManager;
     _messageRouter     = options.MessageRouter;
     _cts       = new CancellationTokenSource();
     _executor  = new Executor(Guid.Empty, this, _exceptionHandler);
     _producers = new ConcurrentDictionary <int, SubProducer <TMessage> >();
     _          = Setup();
 }
Ejemplo n.º 5
0
        protected ProducerOptions GetProducerOptions(string bindingName)
        {
            var options = new ProducerOptions();

            options.PostProcess(bindingName);
            return(options);
        }
        public void CompressionType_ShouldThrowsException_WhenValueIsNotDefineInEnum(CompressionType value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act + Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => options.CompressionType = value);
        }
        public void MaxMessagesInBufferingQueue_ShouldThrowsException_WhenValueIsNotPositive(int value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act + Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => options.MaxMessagesInBufferingQueue = value);
        }
        public void RetryCount_ShouldThrowsException_WhenValueIsNegative(int value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act + Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => options.RetryCount = value);
        }
        public void RetryAfter_ShouldThrowsException_WhenValueIsNotPositive(TimeSpan value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act + Assert
            Assert.Throws <ArgumentOutOfRangeException>(() => options.RetryAfter = value);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="connection">连接对象</param>
 /// <param name="options">配置对象</param>
 /// <param name="consumerOptions">消费者配置对象</param>
 /// <param name="producerOptions">生产者配置对象</param>
 /// <param name="loggerFactory">日志工厂</param>
 public ConnectionObject(IConnection connection, RabbitMQOptions options, ConsumerOptions consumerOptions, ProducerOptions producerOptions, ILoggerFactory loggerFactory)
 {
     channels           = new List <ChannelObject>();
     this.connection    = connection;
     RabbitMQOptions    = options;
     ConsumerOptions    = consumerOptions;
     ProducerOptions    = producerOptions;
     this.loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
     logger             = loggerFactory.CreateLogger <ConnectionObject>();
 }
Ejemplo n.º 11
0
        public void TestSetupProducerThrowsOnNonExistentExchange()
        {
            var producerOptions = new ProducerOptions();

            producerOptions.ExchangeName = null;
            Assert.Throws <ArgumentException>(() => _testAdapter.SetupProducer(producerOptions));

            producerOptions.ExchangeName = "TEST.DoesNotExistExchange";
            Assert.Throws <ApplicationException>(() => _testAdapter.SetupProducer(producerOptions));
        }
 public static ProducerConfiguration ToConfiguration(this ProducerOptions producerOptions)
 {
     return(new ProducerConfiguration
     {
         MessagePriority = producerOptions.MessagePriority,
         MessageDurabilityMode = producerOptions.MessageDurabilityMode,
         MessageIdPolicy = producerOptions.MessageIdPolicy,
         SetMessageCreationTime = producerOptions.SetMessageCreationTime,
     });
 }
Ejemplo n.º 13
0
        protected BindingOptions CreateProducerBindingOptions(ProducerOptions producerOptions)
        {
            var bindingOptions = new BindingOptions()
            {
                ContentType = BindingOptions.DEFAULT_CONTENT_TYPE.ToString()
            };

            bindingOptions.Producer = producerOptions;
            return(bindingOptions);
        }
        public void MaxMessagesInBufferingQueue_ShouldNotThrowsException_WhenValueIsPosistive(int value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act
            options.MaxMessagesInBufferingQueue = value;

            // Assert
            Assert.Equal(value, options.MaxMessagesInBufferingQueue);
        }
        public void MessageBatchSize_ShouldNotThrowsException_WhenValueIsPositive(int value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act
            options.MessageBatchSize = value;

            // Assert
            Assert.Equal(value, options.MessageBatchSize);
        }
        public void MessageTimeoutSetter_shouldNotThrowsException_WhenValueIsPositive(TimeSpan value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act
            options.MessageTimeout = value;

            // Assert
            Assert.Equal(value, options.MessageTimeout);
        }
        public void CompressionType_ShouldNotThrowsException_WhenValueIsDefineInEnum(CompressionType value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act
            options.CompressionType = value;

            // Assert
            Assert.Equal(value, options.CompressionType);
        }
Ejemplo n.º 18
0
        public MicroserviceTester(RabbitOptions rabbitOptions, params ConsumerOptions[] peopleYouWantToSendMessagesTo)
        {
            CleanUpAfterTest = true;

            _adapter = new RabbitMqAdapter(rabbitOptions.CreateConnectionFactory(), "TestHost");

            Factory = new ConnectionFactory
            {
                HostName    = rabbitOptions.RabbitMqHostName,
                Port        = rabbitOptions.RabbitMqHostPort,
                VirtualHost = rabbitOptions.RabbitMqVirtualHost,
                UserName    = rabbitOptions.RabbitMqUserName,
                Password    = rabbitOptions.RabbitMqPassword
            };

            using (var con = Factory.CreateConnection())
                using (var model = con.CreateModel())
                {
                    //get rid of old exchanges
                    model.ExchangeDelete(rabbitOptions.RabbitMqControlExchangeName);
                    //create a new one
                    model.ExchangeDeclare(rabbitOptions.RabbitMqControlExchangeName, ExchangeType.Topic, true);

                    //setup a sender chanel for each of the consumers you want to test sending messages to
                    foreach (ConsumerOptions consumer in peopleYouWantToSendMessagesTo)
                    {
                        if (!consumer.QueueName.Contains("TEST."))
                        {
                            consumer.QueueName = consumer.QueueName.Insert(0, "TEST.");
                        }

                        var exchangeName = consumer.QueueName.Replace("Queue", "Exchange");

                        //terminate any old queues / exchanges
                        model.ExchangeDelete(exchangeName);
                        model.QueueDelete(consumer.QueueName);
                        _declaredExchanges.Add(exchangeName);

                        //Create a binding between the exchange and the queue
                        model.ExchangeDeclare(exchangeName, ExchangeType.Direct, true); //durable seems to be needed because RabbitMQAdapter wants it?
                        model.QueueDeclare(consumer.QueueName, true, false, false);     //shared with other users
                        model.QueueBind(consumer.QueueName, exchangeName, "");
                        _declaredQueues.Add(consumer.QueueName);

                        //Create a producer which can send to the
                        var producerOptions = new ProducerOptions
                        {
                            ExchangeName = exchangeName
                        };

                        _sendToConsumers.Add(consumer, _adapter.SetupProducer(producerOptions, true));
                    }
                }
        }
        public void RetryCount_ShouldNotThrowsException_WhenValueIsPositive(int value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act
            options.RetryCount = value;

            // Assert
            Assert.Equal(value, options.RetryCount);
        }
        public void RetryAfter_ShouldNotThrowsException_WhenValueIsPositive(TimeSpan value)
        {
            // Arrange
            var options = new ProducerOptions();

            // Act
            options.RetryAfter = value;

            // Arrange
            Assert.Equal(value, options.RetryAfter);
        }
Ejemplo n.º 21
0
        public DefaultProducer(ProducerOptions options, IntPtr handle, IProducerNativeMethodsFacade producerFacade = null, DiagnosticListener diagnosticListener = null)
        {
            this.Options = options ?? throw new ArgumentNullException(nameof(options));

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(handle));
            }

            this.Handle              = new HandleRef(this, handle);
            this._producerFacade     = producerFacade ?? new ProducerNativeMethodsFacade();
            this._diagnosticListener = diagnosticListener;
        }
Ejemplo n.º 22
0
        static async Task Main(string[] args)
        {
            var client = PulsarClient.Builder().Build();

            var producerOptions = new ProducerOptions("persistent://public/default/mytopic");
            var producer        = client.CreateProducer(producerOptions);

            while (true)
            {
                Console.Write("Please Enter Message: ");
                var message = Console.ReadLine();
                var data    = Encoding.UTF8.GetBytes(message);
                var result  = await producer.Send(data);
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Loads logging, sets up fatal behaviour, subscribes rabbit etc.
        /// </summary>
        /// <param name="globals">Settings for the microservice (location of rabbit, queue names etc)</param>
        /// <param name="rabbitMqAdapter"></param>
        /// <param name="threaded"></param>
        protected MicroserviceHost(
            [NotNull] GlobalOptions globals,
            IRabbitMqAdapter rabbitMqAdapter = null,
            bool threaded = false)
        {
            if (globals == null || globals.FileSystemOptions == null || globals.RabbitOptions == null || globals.LoggingOptions == null)
            {
                throw new ArgumentException("All or part of the global options are null");
            }

            HostProcessName = SmiCliInit.HostProcessName;

            Logger = LogManager.GetLogger(GetType().Name);
            Logger.Info("Host logger created");

            HostProcessID = Process.GetCurrentProcess().Id;
            Logger.Info($"Starting {HostProcessName} (Host={Environment.MachineName} PID={HostProcessID} User={Environment.UserName})");

            // log centrally
            Globals = globals;
            Logger.Debug("Loaded global options:\n" + globals);

            // should also be centralized for non-host uses
            // Ensure this is false in case the default changes
            DicomTypeTranslater.SerializeBinaryData = false;

            _fatalLoggingProducerOptions = new ProducerOptions
            {
                ExchangeName = Globals.RabbitOptions.FatalLoggingExchange
            };

            //TODO This won't pass for testing with mocked filesystems
            //if(!Directory.Exists(options.FileSystemRoot))
            //    throw new ArgumentException("Could not locate the FileSystemRoot \"" + options.FileSystemRoot + "\"");

            OnFatal += (sender, args) => Fatal(args.Message, args.Exception);

            RabbitMqAdapter = rabbitMqAdapter;
            if (RabbitMqAdapter == null)
            {
                ConnectionFactory connectionFactory = globals.RabbitOptions.CreateConnectionFactory();
                RabbitMqAdapter         = new RabbitMqAdapter(connectionFactory, HostProcessName + HostProcessID, OnFatal, threaded);
                _controlMessageConsumer = new ControlMessageConsumer(connectionFactory, HostProcessName, HostProcessID, globals.RabbitOptions.RabbitMqControlExchangeName, this.Stop);
            }

            ObjectFactory = new MicroserviceObjectFactory();
            ObjectFactory.FatalHandler = (s, e) => Fatal(e.Message, e.Exception);
        }
Ejemplo n.º 24
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="rabbitMQOptions">配置对象</param>
 /// <param name="consumerOptions">消费者配置</param>
 /// <param name="producerOptions">生产者配置</param>
 /// <param name="loggerFactory">日志工厂对象</param>
 public ChannelPooledObjectPolicy(IOptions <RabbitMQOptions> rabbitMQOptions, IOptions <ConsumerOptions> consumerOptions, IOptions <ProducerOptions> producerOptions, ILoggerFactory loggerFactory)
 {
     this.rabbitMQOptions = rabbitMQOptions?.Value ?? throw new ArgumentNullException(nameof(rabbitMQOptions));
     this.producerOptions = producerOptions?.Value ?? throw new ArgumentNullException(nameof(rabbitMQOptions));
     this.consumerOptions = consumerOptions?.Value ?? throw new ArgumentNullException(nameof(rabbitMQOptions));
     this.loggerFactory   = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
     logger            = this.loggerFactory.CreateLogger <ChannelPooledObjectPolicy>();
     connectionObjects = new List <ConnectionObject>();
     connectionFactory = new ConnectionFactory
     {
         UserName    = this.rabbitMQOptions.UserName,
         Password    = this.rabbitMQOptions.Password,
         VirtualHost = this.rabbitMQOptions.VirtualHost,
         AutomaticRecoveryEnabled = true,
         TopologyRecoveryEnabled  = true,
         ClientProvidedName       = this.rabbitMQOptions.ExchangeName,
     };
 }
Ejemplo n.º 25
0
        public ProducerChannelFactory(
            Guid correlationId,
            IRegisterEvent eventRegister,
            IConnectionPool connectionPool,
            IExecute executor,
            ProducerOptions options)
        {
            _correlationId  = correlationId;
            _eventRegister  = eventRegister;
            _connectionPool = connectionPool;
            _executor       = executor;

            _commandProducer = new CommandProducer
            {
                ProducerName = options.ProducerName,
                Topic        = options.Topic
            };
        }
Ejemplo n.º 26
0
        internal void Run(string[] args)
        {
            _logger.LogInformation(
                $"Os serviços da aplicação foram iniciados com sucesso no ambiente de {_hostEnvironment.EnvironmentName} por {System.Environment.UserName}");

            ProducerOptions options = _options.Value;

            if (args.Length == 0)
            {
                _app.Run(string.IsNullOrEmpty(options.DefaultCommand) ? "help" : options.DefaultCommand);
            }
            else
            {
                _app.Run(args);
            }

            _logger.LogInformation(
                $"A encerrar aplicação no ambiente de {_hostEnvironment.EnvironmentName} por {System.Environment.UserName}");
        }
Ejemplo n.º 27
0
        protected ProducerOptions GetProducerOptions(string bindingName, RabbitBindingsOptions bindingsOptions, RabbitBindingOptions bindingOptions = null)
        {
            var rabbitProducerOptions = new RabbitProducerOptions();

            rabbitProducerOptions.PostProcess();

            bindingOptions ??= new RabbitBindingOptions();

            bindingOptions.Producer = rabbitProducerOptions;

            bindingsOptions.Bindings.Add(bindingName, bindingOptions);

            var producerOptions = new ProducerOptions()
            {
                BindingName = bindingName
            };

            producerOptions.PostProcess(bindingName);

            return(producerOptions);
        }
Ejemplo n.º 28
0
        public void TestVerifyPopulatedChecks()
        {
            var producerOptions = new ProducerOptions();

            Assert.False(producerOptions.VerifyPopulated());

            producerOptions.ExchangeName = "";
            Assert.False(producerOptions.VerifyPopulated());

            producerOptions.ExchangeName = "Test.ExchangeName";
            Assert.True(producerOptions.VerifyPopulated());

            var consumerOptions = new ConsumerOptions();

            Assert.False(consumerOptions.VerifyPopulated());

            consumerOptions.QueueName = "Test.QueueName";
            Assert.False(consumerOptions.VerifyPopulated());

            consumerOptions.QoSPrefetchCount = 1234;
            Assert.True(consumerOptions.VerifyPopulated());
        }
Ejemplo n.º 29
0
        public void SetUp()
        {
            _testOptions = new GlobalOptionsFactory().Load();

            _testProducerOptions = new ProducerOptions
            {
                ExchangeName = "TEST.TestExchange"
            };

            _testConsumerOptions = new ConsumerOptions
            {
                QueueName        = "TEST.TestQueue",
                QoSPrefetchCount = 1,
                AutoAck          = false
            };

            _mockConsumer = Mock.Of <Consumer <IMessage> >();

            _testAdapter = new RabbitMqAdapter(_testOptions.RabbitOptions.CreateConnectionFactory(), "RabbitMqAdapterTests");

            _tester = new MicroserviceTester(_testOptions.RabbitOptions, _testConsumerOptions);
        }
Ejemplo n.º 30
0
 public Producer(
     Uri serviceUrl,
     ProducerOptions <TMessage> options,
     ProcessManager processManager,
     IHandleException exceptionHandler,
     IConnectionPool connectionPool,
     ICompressorFactory?compressorFactory)
 {
     _state             = new StateManager <ProducerState>(ProducerState.Disconnected, ProducerState.Closed, ProducerState.Faulted);
     ServiceUrl         = serviceUrl;
     Topic              = options.Topic;
     _isDisposed        = 0;
     _options           = options;
     _exceptionHandler  = exceptionHandler;
     _connectionPool    = connectionPool;
     _compressorFactory = compressorFactory;
     _processManager    = processManager;
     _messageRouter     = options.MessageRouter;
     _cts       = new CancellationTokenSource();
     _executor  = new Executor(Guid.Empty, this, _exceptionHandler);
     _producers = new ConcurrentDictionary <int, IProducer <TMessage> >();
     _          = Setup();
 }