Developing and Deploying a Geronimo GBean has been edited by Ying Tang (Feb 24, 2009).

(View changes)

Content:

Geronimo is a system framework that can be used to build a variety of tailored infrastructure services, with GBean providing it with a loosely-coupled and configurable runtime environments. GBean is an implementation of Inversion of Control (IoC), or the dependency injection, which allows the automatic injection of references as they become available.

Geronimo Kernel and GBean

Geronimo kernel

The Geronimo kernel is a framework for kernel services, and controls the basic server components with the following services:

  • Component configuration
  • Component repository
  • Dependency management
  • Lifecycle management

The basic server componenets, namely security, logging, transactions, and naming, work with the kernel services to build Java EE server components, such as Tomcat, Jetty, and OpenEJB. All these components, cooperate with the kernel to support Java EE applications.

GBeans

Almost everything in Geronimo is a GBean: containers, connectors, adapters, applications and so on.

A GBean relies on the kernel for IoC and dependency injection at runtime through a configuration file called a plan. Dependencies on other GBeans can be configured through the attributes and references in plans. Geronimo's configuration builders construct GBeans out of these plans, group them as configurations, and store them in the configuration store.

A configuration is a logically coupled collection of one or more GBeans and a classloader. The configurations are loaded with the server. The configuration store is the standard Geronimo storage mechanism.

Applications that run in a Java EE container, described by custom deployment descriptors and Java classes, will be parsed and converted into GBeans by the configuration builders of the Geronimo Deployer. These GBeans are also stored in the configuration store.

GBean Lifecycle

A GBean can be in any of the three states: stored, loaded, or running.

  • Stored: The GBean exists in a plan, or configuration store.
  • Loaded: The GBean is mapped to a non-persistent name when loaded by the kernel, allowing multiple instances of the same GBean to exist.
  • Running: The GBean is in the running state, coordinating the application classes that it contains.

If the optional interface GBeanLifecycle for controlling the GBean's state is implemented for the GBean, the implementation will get lifecycle callbacks from the kernel.

package org.apache.geronimo.gbean;

public interface GBeanLifecycle{
void doStart() throws Exception;
void doStop() throws Exception;
void doFail();
}

Developing a GBean

For writing a GBean, you at least have to go through the following steps:

  1. Write a POJO class
  2. Adding the GBeanInfo part to expose attributes,methods, interfaces, and constructors
  3. Implement GbeanLifecycle inteface to use the Gbean Kernel lifecycle callback
  4. Define a deployment plan for the Gbean instance.

A sample GBean

The sample GBean SimpleServerGBean simply starts a socket on the Geronimo server, and retrieves the name of the GBean and the socket port. The following code snippet implements the Plain Old Java Object (POJO) section:

SimpleServerGBean POJO
public class SimpleServerGBean implements GBeanLifecycle, InterfaceNamed {

    private final String name;
    private int port;

    private boolean started = false;
    private ServerSocket serversocket;

    public SimpleServerGBean(String name, int port) {
	this.name = name;
	this.port = port;
    }

    public String getName() {

        return this.name;
    }

    public boolean isStarted() {

	 return started;
    }

    private void printConsoleLog(String log) {

	 System.out.println(" LOG : " + log);
    }

In the following code snippet, the GBean exposes its attributes, operations, interfaces and constructor for the GBEAN_INFO static initializer.

SimpleServerGBean GBeanInfo
private static final GBeanInfo GBEAN_INFO;

        static {
	    GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(
	    SimpleServerGBean.class.getName(), SimpleServerGBean.class);

	    infoFactory.addAttribute("name", String.class, false);
	    infoFactory.addAttribute("port", int.class, true);

	    infoFactory.addOperation("isStarted", "String");

	    infoFactory.addInterface(InterfaceNamed.class);

	    infoFactory.setConstructor(new String[] { "name", "port" });

	    GBEAN_INFO = infoFactory.getBeanInfo();
        }

    public static GBeanInfo getGBeanInfo() {
		return GBEAN_INFO;
    }

The SimpleServerGBean Gbean is simple to start up and shutdown. During startup, it simply accepts an incoming socket connection requests, and sends out the echo message. When being stopped, the GBean closes the resouces that it consumes.

SimpleServerGBean Lifecycle
public void doFail() {
    
    started = false;
    printConsoleLog("GBean " + name + " failed");
    
    }

    public void doStart() throws Exception {

        serversocket = new ServerSocket(port);
	started = true;

        Thread simpleServerThread = new Thread(new Runnable() {

	 Socket socket;
	 InputStream is;
	 OutputStream os;

	 public void run() {

	     while (started) {
  		 try {
  		     socket = serversocket.accept();
  		     is = socket.getInputStream();
  	             os = socket.getOutputStream();
  
  		     BufferedReader bufferedReader = new BufferedReader(
  		     new InputStreamReader(is));
  
  		     String responseMessage = "simpleServer response :"
  		         + bufferedReader.readLine();
  
  		     os.write(responseMessage.getBytes());
  
  		     bufferedReader.close();
  		     if (os != null) {
  		         os.close();
  		     }
  		     if (socket != null && !socket.isClosed()) {
  			 socket.close();
  		     }

		 } catch (Exception e) {
		     //ingore

		 }
             }
         }
    });

        simpleServerThread.start();

	printConsoleLog("GBean " + name
	    + " started and it's listening on port:" + port);
    }

	public void doStop() throws Exception {
            started = false;
	    serversocket.close();
	    printConsoleLog("GBean " + name + " stopped");
    } 
}

For this sample, we still need a class to test the SimpleServerGBean. This section creates a socket and sends a connection request to the SimpleServerGBean. After the connection has been established, it sends out messages and retrieves the echo.

package org.apache.geronimo.sample;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.junit.Ignore;
import org.junit.Test;

public class SimpleServerTest {

    @Test
    @Ignore

        public void connectToSimpleServer() throws Exception {
		

        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("127.0.0.1", 7777));
		
        OutputStream os = socket.getOutputStream();
        String requestMessage = "Hello simple server";
        os.write(requestMessage.getBytes());
        os.write("\n".getBytes());
        os.flush();

        InputStream is = socket.getInputStream();
        BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is));
        String responseMessage = bufferReader.readLine();
        System.out.println(responseMessage);

        socket.close();
    }	

}

The next step is to write the GBean and dependencies into a plan.

simpleServer_deployment_plan.xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="http://geronimo.apache.org/xml/ns/deployment-1.2">
    <environment>
        <moduleId>
	    <groupId>org.apache.geronimo.sample</groupId>
	        <artifactId>simpleServerModule</artifactId>
		<version>1.0</version>
		<type>car</type>
	</moduleId>

	<dependencies>
	    <dependency>
	        <groupId>org.apache.geronimo.sample</groupId>
		<artifactId>simpleServer</artifactId>
		<version>1.0</version>
	    </dependency>
	</dependencies>

	<hidden-classes />

	<non-overridable-classes />

    </environment>

    <gbean name="echoserver" class="org.apache.geronimo.sample.SimpleServerGBean">
        <attribute name="port">7777</attribute>
	 <attribute name="name">simpleServer</attribute>
    </gbean>
</module>

Deploying a GBean

Follow these steps to deploy your GBean to Geronimo.

  1. Before deploying your GBean, you still have to compile it and build a proper .jar file. Maven is used to build Geronimo, so it can be used to build your GBean project. You have to write a pom.xml file. See Maven website for more information about the file.
    pom.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.apache.geronimo.sample</groupId>
        <artifactId>simpleServer</artifactId>
        <packaging>jar</packaging>
        <version>1.0</version>
        <name>sample GBean as a simple server</name>
    
    
        <dependencies>
            <dependency>
    	    <groupId>org.apache.geronimo.framework</groupId>
    	    <artifactId>geronimo-kernel</artifactId>
    	    <version>2.1.3</version>
    	</dependency>
    
    	<dependency>
    	    <groupId>junit</groupId>
    	    <artifactId>junit</artifactId>
    	    <version>4.1</version>
    	    <scope>test</scope>
    	</dependency>
        </dependencies>
    
        <build>
            <plugins>
            
                <plugin>
    	        <artifactId>maven-compiler-plugin</artifactId>
    		<configuration>
    		    <source>1.5</source>
    		    <target>1.5</target>
    		</configuration>
    	    </plugin>
    
            </plugins>
        </build>
    
    </project>
  2. Deploy the .jar to the server via the JAR repository portlet in Geronimo administrative console. For example, you can deploy the SimpleServerGBean with the following information:
    • GroupId: org.apache.geronimo.sample
    • ArtifactId: simpleServer
    • Version: 1.0
    • Type: jar
  3. Deploy the deployment plan to the server using the Deploy New portlet in the administrative console. Or you can deploy the plan using the deployer tools or GShell command.
  4. If success, you can visit http://localhost:7777 and will get an echo from the server.

Note: If a GBean implementation (possibly a .jar) already exisits in the Geronimo server, you only have to develop a deployment plan and deploy it to the server for the GBean instance to work.

Geronimo Bootstrap

When Geronimo starts, the <GERONIMO_HOME>/bin/server.jar is executed with org.apache.geronimo.cli.daemon.DaemonCLI as the main class. This will boot the Geronimo kernel, and load initial GBeans from module j2ee-system-2.1.3.car into the kenel.

Powered by Atlassian Confluence (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request

Unsubscribe or edit your notifications preferences