Deploying the GBean

The gbean-sample-1.0.jar can be packaged in a WAR file or EAR file, and the GBean declaration XML can be added to the deployment plan of those applications. We will deploy a service module with four instances of the MySampleGBean. Before deploying the service module, upload the gbean-sample-1.0.jar to the server repository by using the Repository portlet. Select the file for upload. This will populate the artifactId, version, and type fields. Enter packt-samples for the groupId, and click on Install to upload the JAR.

Use the Deploy New portlet to deploy the service module with the plan file provided under the samples. The same deployment plan is given below:

<module xmlns="http://geronimo.apache.org/xml/ns/deployment-1.2">
<environment>
<moduleId>
<groupId>packt-samples</groupId>
<artifactId>mygbean</artifactId>
<version>1.0</version>
<type>car</type>
</moduleId>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.framework</groupId>
<artifactId>j2ee-system</artifactId>
<type>car</type>
</dependency>
<dependency>
<groupId>packt-samples</groupId>
<artifactId>gbean-sample</artifactId>
<type>jar</type>
</dependency>
</dependencies>
</environment>
<gbean name="mysamplegbean" class="packtsamples.MySampleGBean" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
<attribute name="name">mysamplegbean</attribute>
<reference name="ServerInfo">
<name>ServerInfo</name>
</reference>
</gbean>
<gbean name="mysamplegbean2" class="packtsamples.MySampleGBean" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
<attribute name="name">mysamplegbean2</attribute>
<reference name="ServerInfo">
<name>ServerInfo</name>
</reference>
</gbean>
<gbean name="mysamplegbean3" class="packtsamples.MySampleGBean" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
<attribute name="name">mysamplegbean3</attribute>
<reference name="ServerInfo">
<name>ServerInfo</name>
</reference>
<dependency>
<name>mysamplegbean4</name>
</dependency>
</gbean>
<gbean name="mysamplegbean4" class="packtsamples.MySampleGBean" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2">
<attribute name="name">mysamplegbean4</attribute>
<reference name="ServerInfo">
<name>ServerInfo</name>
</reference>
</gbean>
</module>

Here we have created four instances of MySampleGBean with GBean names mysamplegbean, mysamplegbean2, mysamplegbean3, and mysamplegbean4. Notice that under the dependencies element, we have added a dependency on packt-samples/gbean-sample//jar, which contains our GBean definition. Also notice that the dependency on org.apache.geronimo.framework/j2ee-system//car contains the ServerInfo GBean that we are using as a reference. The console output upon deploying this plan is as shown in the screenshot below:

Usually, the GBeans in the module are started in the same order as they are defined in the deployment plan. When the instances of MySampleGBean are started, a message is displayed in the Geronimo console window, giving the name of the GBean. Note that even though we have not configured the magic attributes in our deployment plan, the Geronimo server has provided values for these attributes. These attribute values are displayed in the command window. Notice that even though mysamplegbean3 is defined before mysamplegbean4 in the deployment plan, mysamplegbean4 is started before mysamplegbean3. This is because of the dependency mysamplegbean3 has on mysamplegbean4, as declared in the deployment plan.

Testing the GBean with GBean web app sample

In order to test the MySampleGBean, we have provided a gbean-webapp web application in the samples. This sample contains a GBeanServlet, which is shown below:

public class GBeanServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Kernel kernel;
@Override
public void init(ServletConfig config) {
kernel = KernelRegistry.getSingleKernel();
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println("GBeanServlet");
out.println("Query using interface name MySampleInterface...");
try {
AbstractNameQuery query = new AbstractNameQuery (MySampleInterface.class.getName());
GBeantesting, GBean web app sample usedSet gbeanNames = kernel.listGBeans(query);
out.println("number of gbeans = "+gbeanNames.size());
for(Iterator itr = gbeanNames.iterator(); itr.hasNext(); ) {
out.println(itr.next());
}
gbeanNames = kernel.listGBeans(query);
AbstractName gbeanName = (AbstractName) gbeanNames.iterator() .next();
out.println("
Obtaining gbean instance using abstract name " +gbeanName);
MySampleInterface gbean = (MySampleInterface) kernel.getGBean(gbeanName);
out.println(gbean);
out.println("
Invoking gbean method via proxy object...");
out.println((String)gbean.myMethod1());
out.println("
Invoking set attribute on gbean "+gbeanName);
kernel.setAttribute(gbeanName, "name", ((String) kernel.getAttribute(gbeanName, "name")).toUpperCase());
kernel.setAttribute(gbeanName, "name2", ((String) kernel.getAttribute(gbeanName, "name2")).toUpperCase());
out.println("
Invoking invoke on gbean "+gbeanName);
out.println((String)kernel.invoke(gbeanName, "myMethod1"));
} catch (Exception e) {
out.println(e);
}
out.flush();
}
@Override
GBean, deployingtesting, GBean web app sample usedpublic void destroy() {
}
}

This servlet obtains an instance of kernel by using KernelRegistry, queries the kernel for instances of MySampleGBean, and executes operations on the GBeans.

Deploy the gbean-webapp-1.0.war by using the Deploy New portlet, and access the application at http://localhost:8080/gbean-webapp/. The output from the servlet is shown in the screenshot below:

Observe the following in this output:

  • The query for GBeans using the interface MySampleInterface has yielded 4 results.

  • The servlet obtains the GBean MySampleGBean by using the getGBean API and invokes myMethod1() using the GBean object. Notice that myMethod1() has retrieved the server details using ServerInfo.

  • The servlet has invoked the setAttribute kernel API on MySampleGBean to set the name attribute to MYSAMPLEGBEAN and name2 attribute to MYSMPLGBN. Notice that the following XML fragment gets added under the packt-samples/mygbean/1.0/car module element in the config.xml file, as shown below:

    <gbean name="packt-samples/mygbean/1.0/car?ServiceModule=packt- samples/mygbean/1.0/car,j2eeType=GBean,name=mysamplegbean">
    <attribute name="name">MYSAMPLEGBEAN</attribute>
    </gbean>
    
  • The attribute value change for name2 is not reflected in config.xml as name2 is not a manageable attribute.

  • The servlet then invokes the GBean operation myMethod1 on MySampleGBean. Notice that the value of name is now displayed in uppercase in the output.

Now launch the System Modules portlet and stop the configuration packt-samples/mygbean/1.0/car. Notice the log messages displayed in the console by the doStop() lifecycle method.

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

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