...
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 |
---|
title | SimpleServerGBean POJO |
---|
| java |
---|
|
public class SimpleServerGBean implements GBeanLifecycle, InterfaceNamed{
private final String gbeanName;
private int port;
private boolean started = false;
private ServerSocket serversocket;
public SimpleServerGBean(String gbeanName, int port) {
this.gbeanName= gbeanName;
this.port = port;
}
public String getName() {
return this.gbeanName;
}
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.
Code Block |
java |
---|
| java |
---|
title | SimpleServerGBean GBeanInfo |
---|
| java |
---|
|
private static final GBeanInfo GBEAN_INFO;
static {
GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(
SimpleServerGBean.class.getName(), SimpleServerGBean.class);
infoFactory.addAttribute("gbeanName", String.class, false);
infoFactory.addAttribute("port", int.class, true);
infoFactory.addOperation("isStarted", "String");
infoFactory.addInterface(InterfaceNamed.class);
infoFactory.setConstructor(new String[] { "gbeanName", "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.
Code Block |
java |
---|
| java |
---|
title | SimpleServerGBean Lifecycle |
---|
| java |
---|
|
public void doFail() {
started = false;
printConsoleLog("GBean " + gbeanName+ " 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 " + gbeanName
+ " started and it's listening on port:" + port);
}
public void doStop() throws Exception {
started = false;
serversocket.close();
printConsoleLog("GBean " + gbeanName+ " stopped");
}
}
|
...
Before deploying your GBean, you still have to compile the GBean class 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 |
---|
|
<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>
|
...
- Deploy the GBean package. There are two methods to deploy a GBean package to Geronimo.
- 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
- Mannually copy the jar file to the GERONIMO_HOME/repository with a path and package name rule.
- Path :
GroupId/ArtifactId/Version/ArtifactId-Version.Type
- Sample:
<GERONIMO_HOME>/repository/org/apache/geronimo/sample/simpleServer/1.0/simpleServer-1.0.jar
Note: If the GBean depends on an external library, you have to deploy both .jars into the repository, and list both of them in the <dependencies> section of the deployment plan described below.
- The next step is to develop a GBean deployment plan. A deployment plan is to define a Geronimo module as an .xml file that includes the descriptions of one or more instances of exsiting GBeans.
Code Block |
---|
| xml | xml |
---|
title | simpleServer_deployment_plan.xml |
---|
| 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="gbeanName">simpleServer</attribute>
</gbean>
</module>
|
Let's briefly go through this plan. - The <moduleId> element is used to provide the configuration name for the module as deployed in the Geronimo server. It contains elements for the groupId, artifactId, version and type. Module IDs are normally printed with slashes between the four components, such as
GroupID/ArtifactID/Version/Type
. In this example, The module ID will be org.apache.geronimo.sample/simpleServer/1.0/car
. - The <dependencies> element is used to provide the configurations on which the module is dependent upon. In this example, the module is dependent on
org.apache.geronimo.sample/simpleServer/1.0
.
- Deploy the deployment plan to the server. There are two methods to deploy a GBean Module with a plan to Geronimo.
- Using the Deploy New portlet in the administrative console.
- Using the deployer tools or GShell command.
- If success, you can visit http://localhost:7777Image Removed and will get an echo from the server.
...
Other GBeans can be used in a GBean by injecting Reference methods. For instance, the GBean SimpleServerGBean can make use of another GBean named EchoMessageGBean with its POJO class and GBeanInfo implemented like the following:
Code Block |
---|
| java | java |
---|
title | EchoMessageGBean |
---|
| java |
---|
|
package org.apache.geronimo.sample;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
public class EchoMessageGBean implements EchoMessageInterface {
private final String msg;
public EchoMessageGBean(String name) {
this.msg = name;
}
private static final GBeanInfo GBEAN_INFO;
static {
GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(
EchoMessageGBean.class);
infoFactory.addAttribute("msg", String.class, false);
infoFactory.addInterface(EchoMessageInterface.class);
infoFactory.setConstructor(new String[] { "msg" });
GBEAN_INFO = infoFactory.getBeanInfo();
}
public static GBeanInfo getGBeanInfo() {
return GBEAN_INFO;
}
public String getEchoMessage() {
return msg;
}
}
|
...
- SimpleServerGBean now has more private members and a different constructor:
Code Block |
java |
---|
| java |
---|
title | Excerpt from SimpleServerGBean POJO |
---|
| java |
---|
|
private final String gbeanName;
private int port;
/* A new private member */
private EchoMessageInterface echo;
private boolean started = false;
private ServerSocket serversocket;
/* Constructor changed */
public SimpleServerGBean(String gbeanName, int port, EchoMessageInterface echomsg) {
this.gbeanName= gbeanName;
this.port = port;
this.echo= echomsg;
}
|
- In doStart() of the SimpleServerGBean implementation, we now retrieves messages from EchoMessageGBean and reorganizes the reponse message:
Code Block |
---|
| java | java |
---|
title | Excerpt from SimpleServerGBean doStart() |
---|
| java |
---|
|
String appendMessage = echo.getEchoMessage();
String responseMessage = appendMessage + bufferedReader.readLine();
|
- Add a Reference method to GBeanInfo:
Code Block |
java |
---|
| java |
---|
title | SimpleServerGBean GBeanInfo |
---|
| java |
---|
|
private static final GBeanInfo GBEAN_INFO;
static {
GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(
SimpleServerGBean.class.getName(), SimpleServerGBean.class);
infoFactory.addAttribute("gbeanName", String.class, false);
infoFactory.addAttribute("port", int.class, true);
infoFactory.addOperation("isStarted", "String");
infoFactory.addInterface(InterfaceNamed.class);
/* Add a reference */
infoFactory.addReference("echoMessenger",EchoMessageInterface.class);
/* A different constructor */
infoFactory.setConstructor(new String[] { "gbeanName", "port", "echoMessenger"});
GBEAN_INFO = infoFactory.getBeanInfo();
}
|
- Correspondingly, the dployment plan for SimpleServerGBean has to be modified.
Code Block |
xml |
---|
| xml |
---|
title | Excerpt from simpleServer_deployment_plan.xml |
---|
| xml |
---|
|
<gbean name="echoserver" class="org.apache.geronimo.sample.SimpleServerGBean">
<attribute name="port">7777</attribute>
<attribute name="gbeanName">simpleServer</attribute>
<reference name="echoMessenger">
<name>msgAppender</name>
</reference>
</gbean>
<gbean name="msgAppender" class="org.apache.geronimo.sample.EchoMessageGBean">
<attribute gbeanName="msg">[Echo from server]:</attribute>
</gbean>
|
After we add the .jar file to the server, and deploy the plan, the test class which initiates a socket on port 7777 will get a reponse message from the server.
No Format |
---|
|
[Echo from server]: Hello simple server |
The GBean SimpleServerGBean can reference different instances of EchoMessageGBean. For instance, the SimpleServerGBean listens on port 7778 instead, and gets messages from the instance msgAppender2.
Code Block |
---|
| xml | xml |
---|
title | Excerpt from simpleServer_deployment_plan2.xml |
---|
| xml |
---|
|
<gbean name="echoserver" class="org.apache.geronimo.sample.SimpleServerGBean">
<attribute name="port">7778</attribute>
<attribute name="gbeanName">simpleServer</attribute>
<reference name="echoMessenger">
<!-- Reference another instance of EchoMessageGBean -->
<name>msgAppender2</name>
</reference>
</gbean>
<gbean name="msgAppender" class="org.apache.geronimo.sample.EchoMessageGBean">
<attribute name="msg">[Echo from server]:</attribute>
<!--Another instance of EchoMessageGBean -->
<gbean name="msgAppender2" class="org.apache.geronimo.sample.EchoMessageGBean">
<attribute name="msg">[Echo from server 2]:</attribute>
</gbean>
|
...