Geronimo入門與進階

本文來自於本人在www.matrix.org.cn網站翻譯的文章

原文爲 這裏

譯文爲 這裏

原文作者Kunal Jaggi 是一個獨立的Java諮詢顧問,主要關注Java技術中的企業解決方案。 


Geronimo是一個最新的Apache發起的開源的應用服務器.Geronimo使用Apache V2.0 許可證,它是一個通過Java EE 1.4 認證的應用服務器。


在這篇文章中,我們將主要涵蓋如下內容 :
    1 . 爲什麼要Geronimo
    2 . A Look Under the Hood
    3 . 使用Geronimo
    4 . 應用程序範例
    5 . 部署的計劃
    6 . 寫你的第一個Geronimo程序
    7 . 構建這個程序
    8 . 部署這個程序
    9 . 測試這個程序


Geronimo是基於一些最好的開源的技術的集合,一個蓬勃發展的社區支持的一個開源服務器,它給整個企業市場帶來了風暴。這篇文章將介紹Geronimo以及在Geronimo上開發和部署一個簡單的Java EE 的web程序所需要的基本點。


爲什麼需要Geronimo?

Geronimo 不是市場上唯一的一個開源的應用服務器。還有一些諸如ObjectWeb的JOnAS等其他開源的Java EE 應用服務器。然而,Geronimo完成了一個其他應用服務器沒有做到的需求。我們可以使用Geronimo來很容易的集成其他組件。它的主要目標就是從 支持傳統的構建過程調整到對具體的應用程序的要求。Geronimo提供了一些選擇。例如,如果你不需要事務管理,你可以使用一個web應用層的服務器, 比如所Tomcat或者Jetty。Geronimo可以很容易的支持傳統組件的聚合。簡而言之,你可是使得它成爲你需要的那種樣子。這就意味着 Geronimo 不僅僅只是一個應用服務器,它提供了一個框架來將不同的組件結合在一起。
Geronimo並沒有做一些重複的工作,它也不是現有的開源框架或是工具的模仿。相反,Geronimo繼承了許多開源的工程,下面列出其中的一些:

Tomcat Jetty OpenEJB ActiveMQ HOWL

Geronimo最優秀的一個特性就是它不需要重啓。和其他web層和business層的容器在改變任何配置之後都需要重啓不同,Geronimo從一開始就設計成爲支持動態class裝載,能夠像一個不中斷的服務那樣一直運行。

A Look Under the Hood

Geronimo 是一個叫做Geronimo beans 或者就是GBeans的組件的鬆散的結合。GBeans 是Geronimo的構建塊,在Ioc容器的概念上的可管理的beans,他們用於向Geronimo內核增加服務。Geronimo中幾乎所有的東西都 是一個GBean.

通過部署新的GBeans,我們可以很容易的擴展Geronimo。GBeans定義了服務器的能力,當它被部署 的時候通時註冊到內中中。Gbean組件遵循一個特性的生命週期,內核創建他們, injects dependencies, 事件通知, 然後調用他們的方法。每一個GBean都可以維持一個狀態,依賴於其他的GBeans,包含了定義它的功能的邏輯。GBean架構時一個DI配置和管理系 統。一個GBean時通過一個plan插入到內核中的,這個plan就是基於XML的配置文件。此外,組件可以通過修改配置文件來很輕鬆的移除。想要了解 更多的關於 Geronimo 的GBean架構和Ioc容器,請參閱 資源 一節


開始使用Geronimo

Geronimo要求Java SE 1.4(或者更高的版本,但是必須早於SE 5)。最新的milestone 發佈版本是M5。將Geronimo和Tomcat容器解壓到某個地方,然後將環境變量中的GERONIMO_HOME設爲這個地方。

通過啓動Geronimo 的內核來啓動Geronimo 。一旦Geronimo 開始運行了,它將裝載所以預定義的模塊以及上面提到的其他應用模塊(爲了簡要起見,並沒有列出所有的域)
D:/>cd %GERONIMO_HOME%
D:/geronimo-1.0>java -jar bin/server.jar
Booting Geronimo Kernel (in Java 1.4.2_08)...
Starting Geronimo Application Server
[********************] 100%  44s Startup complete
  Listening on Ports:
    1527 0.0.0.0 Derby Connector
    8080 0.0.0.0 Tomcat Connector HTTP
    8443 0.0.0.0 Tomcat Connector HTTPS

  Started Application Modules:
    EAR: geronimo/daytrader-derby-tomcat/1.0/car
    EAR: geronimo/uddi-tomcat/1.0/car
    WAR: geronimo/welcome-tomcat/1.0/car

  Web Applications:
    http://WINKJ185011-NZR:8080/
    http://WINKJ185011-NZR:8080/console
Geronimo Application Server started


此 後,通過訪問網址http://localhost:8080/console 來顯示Geronimo 控制窗口 來檢驗Geronimo 的安裝,如圖一所示。使用默認的用戶名(system)和密碼(manager)。停止Geronimo 也是很容易的,按下Ctrl+C 將停止應用服務器。

image
Figure 1. Apache Geronimo Console

範例
對於一篇這樣的介紹性文章,我們將創建一個簡單的web應用程序,並將它部署在Geronimo上。請確定如下的環境變量都正確的設置了。
Environment variable         Purpose
JAVA_HOME         Indicates the base directory of your standard Java SE installation.
GERONIMO_HOME         Set to the root directory of your Geronimo installation.
ANT_HOME         Set to the root directory of your Ant installation.
GERONIMO_DEV         Indicates the base directory of application development space.
PATH                         Ensure that ANT_HOME/bin is in your PATH.


在GERONIMO_DEV路徑下創建如下的目錄:
•        etc: All deployment plans are kept inside this directory.
•        src: This directory is used for placing any servlets and other Java source files.
•        web: All public JSP documents are placed inside this directory.


部署plan

我們需要兩個部署plan,叫做web.xml和geronimo-web.xml。在我們進入這些文件之前,我先來介紹一下他們的差別。下面的一張表將會幫助你區別這兩個部署plan之間的不同以及他們所扮演的角色。

web.xml : 標準Java EE web 部署描述符號

geronimo-web.xml : Geronimo-specific 的部署描述.

編碼第一個Geronimo程序
我們通過創建一個簡單的JSP頁面來歡迎用戶並顯示當前的日期和時間。創建如下的hello.jsp文件,並把它放在GERONIMO_DEV/web 目錄中。

……
<jsp:useBean id="currentdate"
    class="java.util.Date" />


……
<h3>Hello world from Geronimo!! on
${currentdate}</h3>
……


隨後,創建如下的兩個部署plan,並將他們放在GERONIMO_DEV/etc目錄中。第一個部署paln,web.xml 是一個標準的Java EE部署描述符 ,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

    <display-name> Hello JSP Web App
    </display-name>
    <description>First Geronimo App
    </description>
</web-app>


下面的代碼段顯示了Geronimo具體的部署 plan, geronimo-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.0"
xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.0"
configId="com/onjava/safari/myApp">


  <context-root>/myApp</context-root>
  <context-priority-classloader>false/
  </context-priority-classloader>
</web-app>


configId屬性描述了我們程序的名字,com/onjava/safari/myApp。之後我們設置上下文(/myApp),這個目錄就是我們的程序部署到Geronimo中存放的地方了。

構建這個程序
之後我們使用ant做一個增量編譯。我們需要一個Ant的文件(build.xml),放置在GERONIMO_DEV路徑下。build.xml是一個格式良好的XML文件,它描述了我們程序的構建過程。
<project name="GeronimoApp" default="war"
basedir=".">

  <property environment="env"/>

<path id="cp">
      <pathelement path="${java.class.path}"/>
      <fileset dir="${env.GERONIMO_HOME}/
      repository/org.apache.geronimo.specs/jars">
        <include name="*.jar"/>
      </fileset>
    </path>

<target name="prepare">
    <mkdir dir="dist"/>
    <mkdir dir="classes"/>
    <mkdir dir="build"/>

    <delete>
     <fileset dir="classes"
       includes="**/*.class"/>
    </delete>

    <mkdir dir="build/WEB-INF"/>
    <mkdir dir="build/WEB-INF/lib"/>

    <copy file="etc/web.xml" todir="build/WEB-INF"/>
    <copy file="etc/geronimo-web.xml"
    todir="build/WEB-INF" />

    <copy todir="build">
      <fileset dir="web"/>
    </copy>

</target>

<target name="compile" depends="prepare">
    <javac srcdir="src" destdir="classes"
    debug="on" deprecation="off">
      <classpath refid="cp"/>
    </javac>
</target>

<target name="war" depends="compile">
   <war destfile="dist/safari.war"
   webxml="build/WEB-INF/web.xml">
    <classes dir="classes" />
      <fileset dir="build">
        <exclude name="WEB-INF/web.xml"/>
      </fileset>
    </war>
</target>

</project>


下面是我們運行完ant腳本後的輸出。
D:/>cd %GERONIMO_DEV%

D:/gdev>ant
Buildfile: build.xml

prepare:
  [mkdir] Created dir: D:/gdev/dist
  [mkdir] Created dir: D:/gdev/classes
  [mkdir] Created dir: D:/gdev/build
  [mkdir] Created dir: D:/gdev/build/WEB-INF
  [mkdir] Created dir: D:/gdev/build/WEB-INF/lib
   [copy] Copying 1 file to D:/gdev/build/WEB-INF
   [copy] Copying 1 file to D:/gdev/build/WEB-INF
   [copy] Copying 1 file to D:/gdev/build

compile:

war:
      [war] Building war: D:/gdev/dist/safari.war

BUILD SUCCESSFUL
Total time: 3 seconds


在/dist目錄中創建好了一個.war的文件,這個文件可以部署在Geronimo應用服務器裝載的Tomcat容器裏。

部署這個程序


這 兒我們使用Geronimo的部署器來部署.war文件。Geronimo爲這個功能提供了一個”deploy“的命令行。裏必須在命令行使用管理員帳戶 登陸(system/manager)。如果沒有顯式的說明,部署器將會向你提出這個要求。下面的命令部署了這個.war文件。
D:/>cd %GERONIMO_HOME%

D:/geronimo-1.0>java -jar bin/deployer.jar --user
system --password manager deploy
%GERONIMO_DEV%/dist/safari.war
    Deployed com/onjava/safari/myApp @
    http://WINKJ185011-NZR:8080/myApp


這 個部署器可以很智能的指出所部署的組件的類型。我們只需要給它我們的.war文件,然後這個部署器將會把這個文件當作一個WEb程序來部署。 deploy.jar文件將基於提供給Geronimo服務器的部署plan(geronimo-web.xml)來部署這個應用模塊。
作爲另一種選擇,你還可以通過Geronimo的控制檯窗口來手動的部署.war文件。從左邊的控制檯面板中選擇”Deploy New link“,找到你的WAR文件,然後點擊”install“按鈕。

image
Figure 2. Deploy new web app in Geronimo

一旦部署完畢,這個web程序將出現在Geronimo的控制檯窗口的”Installed Web Applications“的列表中。如圖3所示。

image
Figure 3. Installed Web Applications

測試這個程序


現在可是成功的運行它了。開啓一個瀏覽器,然後嘗試這樣的URL”http://localhost:8080/myApp/hello.jsp.“ Tomcat Web容器將處理並執行JSP,如下圖所示:

image
Figure 4. Test JSP running on Geronimo app server

結論
Geronimo天生就是模塊化的。這使得像Apache Derby這些Geronimo包含的模塊可以很容易的裝載。像Spring這樣的集成框架是沒有痕跡的。所以,是時候來嘗試一下Geronimo了。

資源
本文代碼:下載

發佈了34 篇原創文章 · 獲贊 0 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章