GBeans

A GBean is the basic unit in Apache Geronimo. It is a wrapper that is used to wrap or implement different services that are deployed in the kernel. GBeans are similar to MBeans from JMX. A GBean has attributes that store its state and references to other GBeans, and can also register dependencies on other GBeans. GBeans also have lifecycle callback methods and metadata. The Geronimo architects decided to invent the concept of GBeans instead of using MBeans in order to keep the Geronimo architecture independent from JMX. This ensured that they did not need to push in all of the functionality required for the IoC container (that forms Geronimo kernel) into the JMX implementation. Even though GBeans are built on top of MBeans, they can be moved to some other framework as well. A user who is writing a GBean has to follow certain conventions. A sample GBean is shown below:

import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
public class TestGBean implements GBeanLifecycle{
private String name;
public TestGBean(String name){
this.name = name;
}
public void doFail() {
System.out.println("Failed.............");
}
public void doStart() throws Exception {
System.out.println("Started............"+name);
}
public void doStop() throws Exception {
System.out.println("Stopped............"+name);
}
public static final GBeanInfo GBEAN_INFO;
static {
GBeanInfoBuilder infoBuilder = GBeanInfoBuilder .createStatic(TestGBean .class, "TestGBean");
infoBuilder.setPriority(2);
infoBuilder.addAttribute("name", String.class, true);
infoBuilder.setConstructor(new String[]{"name"});
GBEAN_INFO = infoBuilder.getGBeanInfo();
}
public static GBeanInfo getGBeanInfo() {
return GBEAN_INFO;
}
}

You will notice certain characteristics that this GBean has from the previous section. We will list these characteristics as follows:

  • All GBeans should have a static getGBeanInfo method, which returns a GBeanInfo object that describes the attributes and references of GBean as well as the interfaces it can implement.

  • All GBeans will have a static block where a GBeanInfoBuilder object is created and linked to that GBean. All of the metadata that is associated with this GBean is then added to the GBeanInfoBuilder object. The metadata includes descriptions of the attributes, references, interfaces, and constructors of GBean.

We can add GBeans to configurations either programmatically, using methods exposed through the configuration manager and kernel, or by making an entry in the plan for the GBean, as follows:

<gbean name="TestGBean" class="TestGBean">
<attribute name="name">Nitya</attribute>
</gbean>

We need to specify the attribute values in the plan, and the kernel will inject those values into the GBean at runtime. There are three attributes for which we need not specify values. These are called the magic attributes, and the kernel will automatically inject these values when the GBeans are being started. These attributes are abstractName, kernel, and classLoader. As there is no way to specify the values of these attributes in the deployment plan (an XML file in which we provide Geronimo specific information while deploying a configuration), we need not specify them there. However, we should declare these attributes in the GBeanInfo and in the constructor. If the abstractName attribute is declared, then the Geronimo kernel will inject the abstractName of the GBean into it. If it is the kernel attribute, then a reference to the kernel that loaded this GBean is injected. If we declare classLoader, then the class loader for that configuration is injected. More on how to do this will be discussed in Chapter 14, Geronimo Internals.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset