public SimpleActiveMQTest( ISimpleLogger <TestConsumer> logger, IConfiguration configuration ) { _logger = logger; //ServiceManager.GetService<ISimpleLogger<SimpleActiveMQTest>>(); _configuration = configuration; //ServiceManager.GetService<IConfiguration>(); #region 消费者 _consumer = new SimpleConsumer(new ActiveMQOptions(_configuration).BrokerUri, MQType.Queue, "Test_PromotionRecharge", message => { _logger.LogInfo($"消费者接收到消息:{message.Text}"); //throw new Exception("异常测试。。。"); }, ex => { _logger.LogError("【消费者】接收消息异常", ex); return(true); }); _consumer.Start(); #endregion #region 生产者 _producer = new SimpleProducer(new ActiveMQOptions(_configuration).BrokerUri, MQType.Queue, "Test_PromotionRecharge", ex => { _logger.LogError("【生产者】发送消息异常", ex); }); #endregion }
public ProducerTest() { this.ports = TestUtils.ChoosePorts(2); this.port1 = this.ports[0]; this.port2 = this.ports[1]; this.config1 = TestUtils.CreateBrokerConfig( BrokerId1, this.port1, idx => new Dictionary <string, string> { { "num.partitons", "4" } }); this.config2 = TestUtils.CreateBrokerConfig( BrokerId2, this.port2, idx => new Dictionary <string, string> { { "num.partitons", "4" } }); this.server1 = this.StartServer(this.config1); this.server2 = this.StartServer(this.config2); this.servers = new List <Process> { this.server1, this.server2 }; this.consumer1 = new SimpleConsumer("localhost", this.port1, 1000000, 64 * 1024, string.Empty); this.consumer2 = new SimpleConsumer("localhost", this.port2, 100, 64 * 1024, string.Empty); this.WaitForServersToSettle(); }
public async Task Should_receive_using_the_first_consumer() { const string name = "Joe"; await InputQueueSendEndpoint.Send(new SimpleMessageClass(name)); SimpleConsumer lastConsumer = await SimpleConsumer.LastConsumer.OrCanceled(TestCancellationToken); lastConsumer.ShouldNotBe(null); SimpleMessageInterface last = await lastConsumer.Last; last.Name .ShouldBe(name); var wasDisposed = await lastConsumer.Dependency.WasDisposed; wasDisposed .ShouldBe(true); //Dependency was not disposed"); lastConsumer.Dependency.SomethingDone .ShouldBe(true); //Dependency was disposed before consumer executed"); SimplerConsumer lasterConsumer = await SimplerConsumer.LastConsumer.OrCanceled(TestCancellationToken); lasterConsumer.ShouldNotBe(null); SimpleMessageInterface laster = await lasterConsumer.Last.OrCanceled(TestCancellationToken); }
private bool RestartConsumer(Dictionary <string, Config.Queue> namesToQueues, List <SimpleConsumer> restartableConsumers, SimpleConsumer consumerArg) { var consumer = consumerArg; namesToQueues.TryGetValue(consumer.Queue, out var queue); if (queue != null && string.IsNullOrEmpty(queue.Name)) { // check to see if a broker-declared queue name has changed var actualName = queue.ActualName; if (!string.IsNullOrEmpty(actualName)) { namesToQueues.Remove(consumer.Queue); namesToQueues[actualName] = queue; consumer = new SimpleConsumer(this, null, null, actualName); } } try { DoConsumeFromQueue(consumer.Queue); return(true); } catch (Exception e) { _logger?.LogError("Cannot connect to server", e); // if (e.getCause() instanceof AmqpApplicationContextClosedException) { // this.logger.error("Application context is closed, terminating"); // this.taskScheduler.schedule(this::stop, new Date()); // } _consumersToRestart.AddRange(restartableConsumers); _logger?.LogTrace("After restart exception, consumers to restart now: " + _consumersToRestart); return(false); } }
private void CancelConsumer(SimpleConsumer consumer) { try { _logger?.LogDebug("Canceling " + consumer); lock (consumer) { consumer.Canceled = true; if (MessagesPerAck > 1) { try { consumer.AckIfNecessary(0L); } catch (IOException e) { _logger?.LogError("Exception while sending delayed ack", e); } } } RabbitUtils.Cancel(consumer.Model, consumer.ConsumerTag, _logger); } finally { _consumers.Remove(consumer); ConsumerRemoved(consumer); } }
private SimpleConsumer HandleConsumeException(string queue, SimpleConsumer consumerArg, Exception e) { var consumer = consumerArg; // if (e.getCause() is ShutdownSignalException && e.getCause().getMessage().contains("in exclusive use")) { // getExclusiveConsumerExceptionLogger().log(logger, // "Exclusive consumer failure", e.getCause()); // publishConsumerFailedEvent("Consumer raised exception, attempting restart", false, e); // } // else if (e.getCause() is ShutdownSignalException && RabbitUtils.isPassiveDeclarationChannelClose((ShutdownSignalException)e.getCause())) { // this.logger.error("Queue not present, scheduling consumer " // + (consumer == null ? "for queue " + queue : consumer) + " for restart", e); // } // else if (this.logger.isWarnEnabled()) // { _logger?.LogWarning("basicConsume failed, scheduling consumer " + consumer == null ? "for queue " + queue.ToString() : consumer.ToString() + " for restart", e); // } if (consumer == null) { AddConsumerToRestart(new SimpleConsumer(this, null, null, queue)); } else { AddConsumerToRestart(consumer); consumer = null; } return(consumer); }
private SimpleConsumer Consume(string queue, Connection.IConnection connection) { R.IModel channel = null; SimpleConsumer consumer = null; try { channel = connection.CreateChannel(IsChannelTransacted); channel.BasicQos(0, (ushort)PrefetchCount, false); // TODO: Verify this consumer = new SimpleConsumer(this, connection, channel, queue); channel.QueueDeclarePassive(queue); consumer.ConsumerTag = channel.BasicConsume( queue, AcknowledgeMode.IsAutoAck(), ConsumerTagStrategy != null ? ConsumerTagStrategy.CreateConsumerTag(queue) : string.Empty, NoLocal, Exclusive, ConsumerArguments, consumer); } // catch (AmqpApplicationContextClosedException e) // { // throw new AmqpConnectException(e); // } catch (Exception e) { RabbitUtils.CloseChannel(channel, _logger); RabbitUtils.CloseConnection(connection, _logger); consumer = HandleConsumeException(queue, consumer, e); } return(consumer); }
public async Task Should_work_with_lifecycle_managed_bus() { var bus = _container.Resolve <IBusControl>(); BusHandle busHandle = await bus.StartAsync(); try { ISendEndpoint endpoint = await bus.GetSendEndpoint(new Uri("loopback://localhost/input_queue")); const string name = "Joe"; await endpoint.Send(new SimpleMessageClass(name)); SimpleConsumer lastConsumer = await SimpleConsumer.LastConsumer; lastConsumer.ShouldNotBe(null); SimpleMessageInterface last = await lastConsumer.Last; last.Name .ShouldBe(name); bool wasDisposed = await lastConsumer.Dependency.WasDisposed; wasDisposed .ShouldBe(true); //Dependency was not disposed"); lastConsumer.Dependency.SomethingDone .ShouldBe(true); //Dependency was disposed before consumer executed"); } finally { await busHandle.StopAsync(); } }
public async void Should_work_with_the_registry() { var bus = _container.GetInstance <IBusControl>(); bus.Start(); ISendEndpoint endpoint = await bus.GetSendEndpoint(new Uri("loopback://localhost/input_queue")); const string name = "Joe"; await endpoint.Send(new SimpleMessageClass(name)); SimpleConsumer lastConsumer = await SimpleConsumer.LastConsumer; lastConsumer.ShouldNotBe(null); SimpleMessageInterface last = await lastConsumer.Last; last.Name .ShouldBe(name); bool wasDisposed = await lastConsumer.Dependency.WasDisposed; wasDisposed .ShouldBe(true); //Dependency was not disposed"); lastConsumer.Dependency.SomethingDone .ShouldBe(true); //Dependency was disposed before consumer executed"); }
private void AddConsumerToRestart(SimpleConsumer consumer) { if (_started) { _consumersToRestart.Add(consumer); _logger?.LogTrace("Consumers to restart now: " + _consumersToRestart.Count); } }
protected ProducerConsumerTestHarness() { this.port = Configs.First().Port; var props = TestUtils.GetProducerConfig( TestUtils.GetBrokerListFromConfigs(Configs), typeof(StaticPartitioner).AssemblyQualifiedName); this.Producer = new Producer <string, string>(props); this.Consumer = new SimpleConsumer(this.host, this.port, 1000000, 64 * 1024, string.Empty); }
public Ninject_Consumer() { _container = new StandardKernel(); _container.Bind <SimpleConsumer>() .ToSelf() .InSingletonScope(); _container.Bind <AnotherMessageConsumer>() .To <AnotherMessageConsumerImpl>() .InSingletonScope(); _simpleConsumer = _container.Get <SimpleConsumer>(); }
public async Task Should_receive_using_the_first_consumer() { const string name = "Joe"; var sendEndpoint = await Bus.GetSendEndpoint(new Uri("loopback://localhost/frankly-simple")); await sendEndpoint.Send(new SimpleMessageClass(name)); SimpleConsumer lastConsumer = await SimpleConsumer.LastConsumer; lastConsumer.ShouldNotBe(null); }
public Ninject_Consumer() { _container = new StandardKernel(); _container.Bind<SimpleConsumer>() .ToSelf() .InSingletonScope(); _container.Bind<AnotherMessageConsumer>() .To<AnotherMessageConsumerImpl>() .InSingletonScope(); _simpleConsumer = _container.Get<SimpleConsumer>(); }
public Task <bool> RegisterConsumeAsync <TModel>(Action <TModel> onConsumedCallback, string rountingKey, bool requeue, ushort balance) { try { return(SimpleConsumer.GetInstance().RegisterAsync( onConsumedCallback: onConsumedCallback, queueName: rountingKey, requeue: requeue, balanceCount: balance)); } catch (Exception ex) { logger.LogE(ex); #if DEBUG throw; #else return(Task.FromResult(false)); #endif } }
internal AbstractFetcherThread( string name, string clientId, Broker sourceBroker, int socketTimeout, int socketBufferSize, int fetchSize, int fetcherBrokerId = -1, int maxWait = 0, int minBytes = 1, bool isInterruptible = true) : base(name, isInterruptible) { this.clientId = clientId; this.sourceBroker = sourceBroker; this.socketTimeout = socketTimeout; this.socketBufferSize = socketBufferSize; this.fetchSize = fetchSize; this.fetcherBrokerId = fetcherBrokerId; this.maxWait = maxWait; this.minBytes = minBytes; this.partitionMapLock = new ReentrantLock(); this.partitionMapCond = this.partitionMapLock.NewCondition(); this.simpleConsumer = new SimpleConsumer( sourceBroker.Host, sourceBroker.Port, socketTimeout, socketBufferSize, clientId); this.brokerInfo = string.Format("host_{0}-port_{1}", sourceBroker.Host, sourceBroker.Port); this.metricId = new ClientIdAndBroker(clientId, this.brokerInfo); this.FetcherStats = new FetcherStats(this.metricId); this.FetcherLagStats = new FetcherLagStats(this.metricId); this.fetchRequestBuilder = new FetchRequestBuilder().ClientId(clientId) .ReplicaId(fetcherBrokerId) .MaxWait(maxWait) .MinBytes(minBytes); }
private static void Consumer(string brokerList, List <string> topics, string mode) { Console.WriteLine($"Started consumer, Ctrl-C to stop consuming"); ConsumerApp.Interfaces.Consumer consumer; //Modes of the producer //single - each line is write in the console //multi - produces a lot of messages if (String.IsNullOrEmpty(mode)) { mode = ConsumerTypes.Database.ToString(); } else { ConsumerTypes producerType; if (Enum.TryParse(mode, out producerType)) { if (Enum.IsDefined(typeof(ConsumerTypes), mode)) { mode = producerType.ToString(); } else { Console.WriteLine("The producer type passed as argument doesn't exits. mode defaults to multi"); mode = ConsumerTypes.Database.ToString(); } } else { mode = ConsumerTypes.Database.ToString(); } } if (mode == ConsumerTypes.single.ToString()) { consumer = new SimpleConsumer(); } else if (mode == ConsumerTypes.manual.ToString()) { consumer = new ManualConsumer(); } else if (mode == ConsumerTypes.EntityFramework.ToString()) { consumer = new EntityFrameworkConsumer(); } else if (mode == ConsumerTypes.Database.ToString()) { consumer = new DatabaseConsumer(); } else { consumer = new DatabaseConsumer(); } CancellationTokenSource cts = new CancellationTokenSource(); Console.CancelKeyPress += (_, e) => { e.Cancel = true; // prevent the process from terminating. cts.Cancel(); }; consumer.Run(brokerList, topics, cts.Token); // switch (mode) // { // case "subscribe": // Run_Consume(brokerList, topics, cts.Token); // break; // case "manual": // Run_ManualAssign(brokerList, topics, cts.Token); // break; // case "multi": // Run_MultiAssign_Multi(brokerList, topics, cts.Token); // break; // case "multijson": // Run_MultiAssign_Json(brokerList, topics, cts.Token); // break; // default: // PrintUsage(); // break; // } }
public void UnregisterAll() { SimpleConsumer.GetInstance().UnregisterAll(); }
protected virtual void ConsumerRemoved(SimpleConsumer consumer) { // Override if needed }
protected override void ConsumerRemoved(SimpleConsumer consumer) { _inUseConsumerChannels.Remove(consumer.Model, out _); _whenUsed.Remove(consumer, out _); }
public bool UnregisterConsume <TModel>(string rountingKey) { return(SimpleConsumer.GetInstance().Unregister <TModel>(rountingKey)); }