Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

A GBean is a Java object that has a method called getGBeanInfo() that returns an instance of the GBeanInfo class. GBeanInfo has three main purposes. One is to define the methods that should be exposed to the other subsystems, the second is to define attributes and references so that they can injected at runtime and the third is to define information about the GBean so that it can be located later. Each of these will be discussed in detail below. When defining the instance of the GBeanInfo object for the GBean, GBeanInfo shouldn't be constructed directly. Instead there is a GBeanInfoBuilder class that will simplify the process (see //1 below). Below is an example of creating a GBeanInfo from the Log4jService class and then returning it in the getGBeanInfo() method:

Code Block
borderStylesolid
titleLog4jService.java
borderStylesolid
//...
static {
        GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(Log4jService.class, "SystemLog"); //1

        infoFactory.addAttribute("configFileName", String.class, true);//2
        infoFactory.addAttribute("refreshPeriodSeconds", int.class, true);
        infoFactory.addAttribute("configuration", String.class, false);
        infoFactory.addAttribute("rootLoggerLevel", String.class, false);

        infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");//3

        infoFactory.addOperation("reconfigure");//4
        infoFactory.addOperation("setLoggerLevel", new Class[]{String.class, String.class});
        infoFactory.addOperation("getLoggerLevel", new Class[]{String.class});
        infoFactory.addOperation("getLoggerEffectiveLevel", new Class[]{String.class});
        infoFactory.addInterface(SystemLog.class);//5

        infoFactory.setConstructor(new String[]{"configFileName", "refreshPeriodSeconds", "ServerInfo"});//6

        GBEAN_INFO = infoFactory.getBeanInfo();
}

public static GBeanInfo getGBeanInfo() {
       return GBEAN_INFO;
}
//...

...

Although implementing any interfaces for a GBean is not required it is often useful to be able to execute code when a GBean starts up, or shuts down. Implementing the GBeanLifecycle interface will allow the GBean to be notified on startup, shutdown and failure. The required methods to implement in the GBeanLifecycle interface are: doStart(), doStop() and doFail(). Below is some example code from the same Log4jService GBean:

solid
Code Block
borderStyle
titleLog4jService.java
borderStylesolid
public void doStart() {
        LogFactory logFactory = LogFactory.getFactory();
        if (logFactory instanceof GeronimoLogFactory) {
            synchronized (this) {
                timer = new Timer(true);

                // Periodically check the configuration file
                schedule();

                // Make sure the root Logger has loaded
                Logger logger = LogManager.getRootLogger();

                reconfigure();

                File file = resolveConfigurationFile();
                if (file != null) {
                    lastChanged = file.lastModified();
                }
                logEnvInfo(logger);
            }

            // Change all of the loggers over to use log4j
            GeronimoLogFactory geronimoLogFactory = (GeronimoLogFactory) logFactory;
            synchronized (geronimoLogFactory) {
                if (!(geronimoLogFactory.getLogFactory() instanceof CachingLog4jLogFactory)) {
                    geronimoLogFactory.setLogFactory(new CachingLog4jLogFactory());
                }
            }
        }

        synchronized (this) {
            running = true;
        }
    }

    public synchronized void doStop() {
        running = false;
        if (monitor != null) {
            monitor.cancel();
            monitor = null;
        }
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    public void doFail() {
        doStop();
    }

...