java - Logging duplicate messages once -
i'm in unique situation receiving duplicate messages 10-25 sources @ same time or @ same time. code came counter was:
private queue<string> messages = new concurrentlinkedqueue<>(); public void log(string message) { if(!messages.contains(message)) { if(messages.size() >= 5) { messages.remove(0); } messages.add(message); main.getlogger().info(message); } }
the problem code since method being called concurrently, log messages 2-4 times instead of once.
consider putting actual writes logger in separate thread reads incoming log messages thread safe queue , checks duplicates before writing. apps perform logging write log messages queue , writer monitor queue new messages. when message comes in, it's checked uniqueness against number of previous messages , if unique, it's both written queue , stashed in set of older messages check against (potentially pushing out older message).
Comments
Post a Comment