Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 into the GBean 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.

...

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.

Code Block
java
java
package org.apache.geronimo.gbean;

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

...

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:

Code Block
java
java
titleSimpleServerGBean 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);
    }

...

Code Block
java
java
titleSimpleServerGBean lifecycle partGBeanInfo
 
   
    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;
    }

...

Code Block
java
java
titleSimpleServerGBean GBeanInfoLifecycle
 

    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.

Code Block
java:titleSimpleServerTest

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.

...

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.
    Code Block
    xml
    xml
    titlepom.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.