2012-06-10 71 views
1

我有一個應用程序運行在服務器上的Glassfish v2.1.1,我正在尋找生成一個便攜式(閱讀:沒有安裝要求)版本。我不想移動到另一個容器(Glassfish 3,Tomcat等),因爲它會引入新的問題和錯誤。我更願意堅持使用同一個容器。嵌入Glassfish v2.1.1

我已經開始拆分Glassfish發行版,並且有一些引用提示需要安裝的路徑。有沒有人成功生成基於v2.1.1的便攜式Glassfish?

回答

-1

嵌入GlassFish和應用程序部署到嵌入的GlassFish的基本例子:

完整的個人資料尤伯杯罐子:http://download.java.net/maven/glassfish/org/glassfish/extras/glassfish-embedded-all/3.1/glassfish-embedded-all-3.1.jar 的Web Profile超級

這些例子都可以與無論是在你的CLASSPATH以下罐子運行罐子:http://download.java.net/maven/glassfish/org/glassfish/extras/glassfish-embedded-web/3.1/glassfish-embedded-web-3.1.jar 安裝GlassFish的外殼罐子:$ GF_INSTALLATION/lib目錄/嵌入式/與GlassFish嵌入式靜電shell.jar

一旦你與你的上述jar文件中的任何一個,GlassFish的可通過簡單地做內嵌在應用程序中:

import org.glassfish.embeddable.*; 

/** Create and start GlassFish */ 
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(); 
glassfish.start(); 

讓我們說,你想8080 Web容器端口,同時嵌入GlassFish的開始,那麼你必須這樣做:

import org.glassfish.embeddable.*; 

/** Create and start GlassFish which listens at 8080 http port */ 
GlassFishProperties gfProps = new GlassFishProperties(); 
gfProps.setPort("http-listener", 8080); // refer JavaDocs for the details of this API. 

GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(gfProps); 
glassfish.start(); 

一旦你有在GlassFish嵌入式和運行過程中,你可能想用下面的代碼來部署一個預建的Java EE檔案:

import org.glassfish.embeddable.*; 

// Obtain the deployer from the glassfish which is embedded via the piece of code  above. 
Deployer deployer = glassfish.getDeployer(); 

// syntax of deployment params are same as how they are passed to 'asadmin deploy' command. 
deployer.deploy(new File("simple.war"), "--contextroot=test", "--name=test", "--force=true"); 

// if you have no deployment params to pass, then simply do this: 
deployer.deploy(new File("simple.war")); 

如果你的檔案沒有預建,而不是它的成分是分散在多個目錄,那麼你可能有興趣使用散射歸檔的API:

import org.glassfish.embeddable.*; 
import org.glassfish.embeddable.archive.*; 

Deployer deployer = glassfish.getDeployer(); 

// Create a scattered web application. 
ScatteredArchive archive = new ScatteredArchive("testapp", ScatteredArchive.Type.WAR); 
// target/classes directory contains my complied servlets 
archive.addClassPath(new File("target", "classes")); 
// resources/sun-web.xml is my WEB-INF/sun-web.xml 
archive.addMetadata(new File("resources", "sun-web.xml")); 
// resources/MyLogFactory is my META-INF/services/org.apache.commons.logging.LogFactory 
archive.addMetadata(new File("resources", "MyLogFactory"), "META-  INF/services/org.apache.commons.logging.LogFactory"); 

deployer.deploy(archive.toURI()) 

同樣,散企業應用程序(EAR型)可以部署這樣的:

import org.glassfish.embeddable.*; 
import org.glassfish.embeddable.archive.*; 

Deployer deployer = glassfish.getDeployer(); 

// Create a scattered web application. 
ScatteredArchive webmodule = new ScatteredArchive("testweb",  ScatteredArchive.Type.WAR); 
// target/classes directory contains my complied servlets 
webmodule.addClassPath(new File("target", "classes")); 
// resources/sun-web.xml is my WEB-INF/sun-web.xml 
webmodule.addMetadata(new File("resources", "sun-web.xml")); 

// Create a scattered enterprise archive. 
ScatteredEnterpriseArchive archive = new ScatteredEnterpriseArchive("testapp"); 
// src/application.xml is my META-INF/application.xml 
archive.addMetadata(new File("src", "application.xml")); 
// Add scattered web module to the scattered enterprise archive. 
// src/application.xml references Web module as "scattered.war". Hence specify the name   while adding the archive. 
archive.addArchive(webmodule.toURI(), "scattered.war"); 
// lib/mylibrary.jar is a library JAR file. 
archive.addArchive(new File("lib", "mylibrary.jar")); 
// target/ejbclasses contain my compiled EJB module. 
// src/application.xml references EJB module as "ejb.jar". Hence specify the name while  adding the archive. 
archive.addArchive(new File("target", "ejbclasses"), "ejb.jar"); 

deployer.deploy(archive.toURI()) 

最後,對應用程序的結束,你想停止/配置嵌入式的GlassFish:

import org.glassfish.embeddable.*; 

/** Stop GlassFish */ 
glassfish.stop(); // you can start it again. 

/** Dispose GlassFish */ 
glassfish.dispose(); // you can not start it again. But you can embed a fresh glassfish  with GlassFishRuntime.bootstrap().newGlassFish() 
+2

廣泛的答案,但我特別強調了有關GlassFish 2.1.1的解決方案,而不是移動到胃腸阿蘇菲斯3 – Yon

相關問題