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

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -