public async Task TestAcknowledgeFailsAfterSessionIsClosed() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.ClientAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithNullContent(), count: 1); testPeer.ExpectEnd(); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); IMessage receivedMessage = await consumer.ReceiveAsync(TimeSpan.FromSeconds(6)); Assert.NotNull(receivedMessage, "Message was not received"); await session.CloseAsync(); Assert.CatchAsync <NMSException>(async() => await receivedMessage.AcknowledgeAsync(), "Should not be able to acknowledge the message after session closed"); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(3000); } }
public async Task TestClientAcknowledgeMessagesAsync() { using (TestAmqpPeer testPeer = new TestAmqpPeer()) { int msgCount = 3; IConnection connection = await EstablishConnectionAsync(testPeer); await connection.StartAsync(); testPeer.ExpectBegin(); ISession session = await connection.CreateSessionAsync(AcknowledgementMode.ClientAcknowledge); IQueue queue = await session.GetQueueAsync("myQueue"); testPeer.ExpectReceiverAttach(); testPeer.ExpectLinkFlowRespondWithTransfer(message: CreateMessageWithNullContent(), count: msgCount); IMessageConsumer consumer = await session.CreateConsumerAsync(queue); CountdownEvent latch = new CountdownEvent(3); IMessage lastReceivedMessage = null; consumer.Listener += message => { lastReceivedMessage = message; latch.Signal(); }; Assert.True(latch.Wait(2000)); for (int i = 0; i < msgCount; i++) { testPeer.ExpectDispositionThatIsAcceptedAndSettled(); } await lastReceivedMessage.AcknowledgeAsync(); testPeer.WaitForAllMatchersToComplete(2000); testPeer.ExpectClose(); await connection.CloseAsync(); testPeer.WaitForAllMatchersToComplete(2000); } }
private async Task handleMessage(IMessage <T> message) { if (_stoppingToken.IsCancellationRequested) { return; } bool?isRequeue = null; try { using (IServiceScope scope = _serviceProvider.CreateScope()) { var messageHandler = scope.ServiceProvider.GetRequiredService <IMessageHandler <T> >(); await messageHandler.HandleMessageAsync(message.Item, _stoppingToken); } await message.AcknowledgeAsync(); _logger.Verbose($"Acknowledge: {typeof(T).Name}."); } catch (OperationCanceledException ex) { _logger.Warning(ex, $"The operation was canceled. The {typeof(T).Name} will be requeue."); isRequeue = true; } catch (InvalidOperationException ex) { _logger.Error(ex, $"The message handler is not present in the DI container for the {typeof(T).Name}."); isRequeue = false; } catch (MessageException ex) { _logger.Error(ex, $"Failed to acknowledge the {typeof(T).Name}."); isRequeue = false; } catch (Exception ex) { _logger.Error(ex, $"An exception occurred during processing the {typeof(T).Name}."); isRequeue = false; } // If an exception occurred. if (isRequeue.HasValue) { try { await message.RejectAsync(requeue : isRequeue.Value); _logger.Verbose($"Reject: {typeof(T).Name}, Requeue: {isRequeue.Value}."); } catch (Exception ex) { _logger.Error(ex, $"Failed to reject the {typeof(T).Name}."); } } }