Bug 42017

Summary: InstanceAlreadyExistsException using MBean
Product: Log4j - Now in Jira Reporter: Francesco Dalan <f.dalan>
Component: OtherAssignee: log4j-dev <log4j-dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 1.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows 2000   
Attachments: Adds a test before register a MBean
Adds a test before register a MBean

Description Francesco Dalan 2007-04-02 03:06:53 UTC
Hi, when I use the MBean architecture with mx4j implementation to manage the 
logging parameters, I have an InstanceAlreadyExistsException. This happen when 
the org.apache.log4j.jmx.AppenderDynamicMBean try to register a layout 
(registerLayoutMBean method) because do not check if the MBean is already 
registered. Here there is the code that I use

MBeanServer server = MBeanServerFactory.createMBeanServer();
HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
...
LoggerRepository r = LogManager.getLoggerRepository();
Enumeration enum = r.getCurrentLoggers();
Logger logger = null;
while (enum.hasMoreElements()) {
  logger = (Logger) enum.nextElement();
  ObjectName dummy = new ObjectName("log4j", "logger", logger.getName());
  if (!server.isRegistered(dummy))
    hdm.addLoggerMBean(logger.getName());
}

and the log4j.properties

# Console is set to be a ConsoleAppender.
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d %-5p %c{1}.%M - %m%n

# FileConsole is set to be a FileAppender.
log4j.appender.FileConsole.File=\\wcubecoils.log
log4j.appender.FileConsole=org.apache.log4j.FileAppender
log4j.appender.FileConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.FileConsole.layout.ConversionPattern=%d %-5p %c{1}.%M - %m%n

# the root logger
log4j.rootLogger=INFO, FileConsole

# wcube logging configuration...
log4j.logger.it.mbm.wcube.processmessage=TRACE, FileConsole
log4j.logger.it.mbm.wcube.mq.bo.action=TRACE, FileConsole
Comment 1 Francesco Dalan 2007-04-02 03:46:02 UTC
Created attachment 19866 [details]
Adds a test before register a MBean

The patch have been marked by the patch comment
Comment 2 Francesco Dalan 2007-04-02 03:46:31 UTC
Created attachment 19867 [details]
Adds a test before register a MBean

The patch have been marked by the patch comment
Comment 3 Paul Smith 2007-04-23 15:10:19 UTC
1.2.15 candidate
Comment 4 Paul Smith 2007-04-23 16:09:36 UTC
Done in revision 531643