I have some kind of high thoughput message handler receiving ordered messages. Now the task is - adding monitoring stuff in it in order to recognize wrong input messages, more exactly: it must notify about missed messages and just ignore the stale ones (with smaller order).Due to high thoughput - it mustn't block.So it must:not use locksfind missing messagesfind wrong ordered messagesLet's have some long counter in message for ordering - here is my initial version:public class OrderedNonBlockingProcessor implements YetAnotherMessageHandler {    private static Logger log = Logger.getLogger(OrderedNonBlockingProcessor.class);    private final AtomicLong messageCounter = new AtomicLong(0);    @Override    public boolean onSomeHighThroughputMessage(final YetAnotherMessage message) {        final long expected = messageCounter.getAndSet(message.getCounter()) + 1;        if (expected == message.getCounter()) {            processBusinessStuff(message);            return true;        } else if (expected > message.getCounter()) {            /* wrong message, attempt to restore the sequence to prevent an error on next good message             * TODO: fix ABA problem here             */            messageCounter.compareAndSet(message.getCounter(), expected - 1);            log.error(String.format(messaging system ordering bug: got stale message %s while expected %s!,                    message.getCounter(), expected));            // some other notifying stuff...        } else if (expected < message.getCounter()) {            log.error(String.format(got forward message %s while expected %s, missed: %s,                    message.getCounter(), expected, message.getCounter() - expected));            // some other notifying stuff...        }        return false;    }    private void processBusinessStuff(YetAnotherMessage message) {        log.info(String.format(process message %s, message.getCounter()));        // some business logic...    }}(Here is a junit for that)How is it? Are there any races/concurrency issues I missed?P.S. Next step is fixing ABA problem there.  I'm thinking about AtomicStampedReference[Long] for that.
Non blocking high performance message processor
java;optimization