static void normalizeAndRunQueuedCommands() { if (commandQueue == null) return; // Synchronizing on commandQueue to ensure that even if 2 threads try to poll, only one can process the commands // that were scheduled at a time (happens if queue is full or during unit tests). // The latter is important to ensure that the command calling order is kept intact as parallel polling would destroy it. synchronized (commandQueue) { final Map<String, ManagementCommand> mappedCommands = new LinkedHashMap<String, ManagementCommand>(commandQueue.size()); ManagementCommand command; while ((command = commandQueue.poll()) != null) { String name = command.getName(); ManagementCommand previousCommand = mappedCommands.put(name, command); if (previousCommand instanceof ManagementRegisterCommand) { // Avoid that we have unbound un-register commands in the work queue. if (command instanceof ManagementUnregisterCommand && !((ManagementRegisterCommand) previousCommand).isReplace()) mappedCommands.remove(name); } else if (previousCommand instanceof ManagementUnregisterCommand) { // We already have this MBean, flagging it for replacement. if (command instanceof ManagementRegisterCommand) ((ManagementRegisterCommand) command).setReplace(true); } } for (ManagementCommand c : mappedCommands.values()) c.run(); } }
static void normalizeAndRunQueuedCommands() { if (commandQueue == null) return; // Synchronizing on commandQueue to ensure that even if 2 threads try to poll, only one can process the commands // that were scheduled at a time (happens if queue is full or during unit tests). // The latter is important to ensure that the command calling order is kept intact as parallel polling would destroy it. synchronized (commandQueue) { final Map<String, ManagementCommand> mappedCommands = new LinkedHashMap<String, ManagementCommand>(commandQueue.size()); ManagementCommand command; while ((command = commandQueue.poll()) != null) { String name = command.getName(); ManagementCommand previousCommand = mappedCommands.put(name, command); if (previousCommand instanceof ManagementRegisterCommand) { // Avoid that we have unbound un-register commands in the work queue. if (command instanceof ManagementUnregisterCommand && !((ManagementRegisterCommand) previousCommand).isReplace()) mappedCommands.remove(name); } else if (previousCommand instanceof ManagementUnregisterCommand) { // We already have this MBean, flagging it for replacement. if (command instanceof ManagementRegisterCommand) ((ManagementRegisterCommand) command).setReplace(true); } } for (ManagementCommand c : mappedCommands.values()) c.run(); } }