public void Publish(Event @event) { if (!_connection.IsConnected) { _connection.TryConnect(); } using var channel = _connection.CreateModel(); var eventName = @event.GetType() .Name; channel.ExchangeDeclare(BrokerName, "direct"); var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); Policies.WaitRetryPolicy(3).Execute(() => { var properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; channel.BasicPublish(BrokerName, eventName, true, properties, body); }); }
private IModel CreateNewChannel(string exchangeName) { if (!_connection.IsConnected) { _connection.TryConnect(); } return(_connection.CreateModel(exchangeName)); }
private void SubsManager_OnEventRemoved(object sender, string eventName) { if (!_connection.IsConnected) { _connection.TryConnect(); } using (var channel = _connection.CreateModel()) { channel.QueueUnbind(_queueName, _brokerName, eventName); if (!SubscriptionManager.IsEmpty) { return; } _queueName = string.Empty; _consumerChannel?.Close(); } }
public async Task StartAsync(CancellationToken cancellationToken) { await _connection.TryConnect(); if (!_connection.IsConnected) { throw new InvalidOperationException("Could not connect to RabbitMQ"); } // var eventName = typeof(T).Name; // Console.WriteLine($"Event Name: {eventName}"); _channel = _connection.CreateModel(); _channel.ExchangeDeclare(exchange: RabbitMqEventBus.EXAMPLE_EXCHANGE_NAME, type: "direct"); _channel.QueueDeclare(queue: _queueName, durable: true, exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(_channel); consumer.Received += async(model, ea) => { var eventName = ea.RoutingKey; var message = Encoding.UTF8.GetString(ea.Body); Console.WriteLine($" [x] Received {message}, {eventName}"); T convertedMessage = JsonConvert.DeserializeObject <T>(message); _eventHandler.Handle(convertedMessage); _channel.BasicAck(ea.DeliveryTag, multiple: false); }; _channel.BasicConsume(queue: _queueName, autoAck: false, consumer: consumer); //_channel.QueueDeclare(queue: _queueName, // "hello", // durable: false, // exclusive: false, // autoDelete: false, // arguments: null); //var consumer = new EventingBasicConsumer(_channel); //consumer.Received += (model, ea) => { // var body = ea.Body; // var message = Encoding.UTF8.GetString(body); // Console.WriteLine(" [x] Received {0}", message); // T convertedMessage = JsonConvert.DeserializeObject<T>(message); // _eventHandler.Handle(convertedMessage); //}; //_channel.BasicConsume(queue: _queueName, // "hello", // autoAck: true, // consumer: consumer); }
public async Task PublishAsync(string routingKey, object model) { if (!_rabbitMqConnection.IsConnected) { _rabbitMqConnection.TryConnect(); } var policy = Policy.Handle <BrokerUnreachableException>() .Or <SocketException>() .WaitAndRetryAsync(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { _logger.LogWarning(ex.ToString()); }); using (var channel = _rabbitMqConnection.CreateModel()) { channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct"); var message = JsonConvert.SerializeObject(model); var body = Encoding.UTF8.GetBytes(message); await policy.ExecuteAsync(async() => { var properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; //持久化 channel.BasicPublish( exchange: BROKER_NAME, routingKey: routingKey, mandatory: true, basicProperties: properties, body: body); //return Task.CompletedTask; }); } }
public void EnqueueEvent <TEvent>(Event <TEvent> @event) where TEvent : Event <TEvent> { if (_connection.IsConnected is false) { _connection.TryConnect(); } using var channel = _connection.CreateModel(); var eventName = @event.GetType().Name; channel.ExchangeDeclare(_exchangeName, _exchangeType); var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); var properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; properties.CorrelationId = @event.MicroServiceId; try { _logger.LogInformation("Publishing in queue: {event}", @event); channel.BasicPublish(_exchangeName, eventName, true, properties, body); } catch (Exception ex) { _logger.LogInformation(ex, "Error publishing in queue: {event}", @event); } }