/*
 * Copyright (c) 2010-2021. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.messaging.deadletter;

import org.axonframework.messaging.Message;

import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.stream.Stream;

/**
 * In memory implementation of the {@link DeadLetterQueue}. Maintains a {@link PriorityQueue} per unique sequence
 * identifier contained in the {@link DeadLetter}.
 *
 * @author Steven van Beelen
 * @since 4.6.0
 */
public class InMemoryDeadLetterQueue<T extends Message<?>> implements DeadLetterQueue<T> {

    private final ConcurrentNavigableMap<String, Queue<DeadLetter<T>>> deadLetters = new ConcurrentSkipListMap<>();

    @Override
    public void add(DeadLetter<T> deadLetter) {
        deadLetters.computeIfAbsent(deadLetter.sequenceIdentifier(),
                                    sequenceId -> new PriorityQueue<>(DeadLetter::compare))
                   .add(deadLetter);
    }

    @Override
    public boolean contains(String sequenceIdentifier) {
        return deadLetters.containsKey(sequenceIdentifier);
    }

    @Override
    public boolean isEmpty() {
        return deadLetters.isEmpty();
    }

    @Override
    public Stream<DeadLetter<T>> peek() {
        return isEmpty() ? Stream.empty() : deadLetters.firstEntry()
                                                       .getValue()
                                                       .stream();
    }

    @Override
    public Stream<DeadLetter<T>> poll() {
        return isEmpty() ? Stream.empty() : deadLetters.pollFirstEntry()
                                                       .getValue()
                                                       .stream();
    }
}

@@ -694,16 +694,112 @@
 essage;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport java.lang.invoke.MethodHandles;
 %0Aimport 
@@ -1296,16 +1296,121 @@
 e%3CT%3E %7B%0A%0A
+    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());%0A%0A
     priv
@@ -1576,24 +1576,86 @@
 adLetter) %7B%0A
+        logger.debug(%22Adding dead letter %5B%7B%7D%5D.%22, deadLetter);%0A
         dead
@@ -1914,24 +1914,119 @@
 entifier) %7B%0A
+        logger.debug(%22Validating existence of sequence identifier %5B%7B%7D%5D.%22, sequenceIdentifier);%0A
         retu

Add logging