public void Publish(IntegrationEvent @event) { if (!_persistentConnection.IsConnected) { _persistentConnection.TryConnect(); } using (var channel = _persistentConnection.CreateModel()) { var eventName = @event.GetType() .Name; channel.ExchangeDeclare(exchange: BrokerName, type: "direct"); var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); var properties = channel.CreateBasicProperties(); properties.DeliveryMode = 2; // persistent channel.BasicPublish(exchange: BrokerName, routingKey: eventName, mandatory: true, basicProperties: properties, body: body); } }
public void CreateModel_ConnectionIsClosed_ThrowsInvalidOperationException( DefaultRabbitMQPersistentConnection sut ) { //Arrange //Act Action act = () => sut.CreateModel(); //Assert act.Should().Throw <InvalidOperationException>(); }
public void CreateModel_ConnectionIsOpen_ReturnsModel( [Frozen] Mock <IConnectionFactory> mockConnectionFactory, [Frozen] Mock <IConnection> mockConnection, DefaultRabbitMQPersistentConnection sut ) { //Arrange mockConnection.Setup(_ => _.IsOpen) .Returns(true); mockConnectionFactory.Setup(_ => _.CreateConnection()) .Returns(mockConnection.Object); //Act sut.TryConnect(); var result = sut.CreateModel(); //Assert result.Should().NotBeNull(); }
public void Test1() { var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); }); ILogger <DefaultRabbitMQPersistentConnection> logger = loggerFactory.CreateLogger <DefaultRabbitMQPersistentConnection>(); IConnectionFactory connectionFactory = new ConnectionFactory() { HostName = "localhost" }; IRabbitMQPersistentConnection rabbitMqPersistentConnection = new DefaultRabbitMQPersistentConnection(connectionFactory, logger); rabbitMqPersistentConnection.TryConnect(); var isconnect = rabbitMqPersistentConnection.IsConnected; using (var channel = rabbitMqPersistentConnection.CreateModel()) { channel.ExchangeDeclare(exchange: "testEX5", type: "direct"); channel.QueueDeclare(queue: "testQueue5", durable: true, exclusive: false, autoDelete: false, arguments: null); string message = "Hello World!"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "testEX5", routingKey: "testEX5", basicProperties: null, body: body); } }