嵌入Glassfish v2.1.1

嵌入Glassfish v2.1.1,glassfish,Glassfish,我有一个运行在服务器上的Glassfish v2.1.1上的应用程序,我希望生成一个可移植(阅读:无需安装)版本。我不想转移到另一个容器(GlassFish3、Tomcat等),因为它会带来新的问题和bug。我更喜欢用同一个容器 我已经开始分解Glassfish发行版,其中有一些路径参考,提示需要安装。是否有人成功地基于v2.1.1生成了便携式Glassfish?嵌入Glassfish并将应用程序部署到嵌入式Glassfish的基本示例: import org.glassfish.embedda

我有一个运行在服务器上的Glassfish v2.1.1上的应用程序,我希望生成一个可移植(阅读:无需安装)版本。我不想转移到另一个容器(GlassFish3、Tomcat等),因为它会带来新的问题和bug。我更喜欢用同一个容器


我已经开始分解Glassfish发行版,其中有一些路径参考,提示需要安装。是否有人成功地基于v2.1.1生成了便携式Glassfish?

嵌入Glassfish并将应用程序部署到嵌入式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()
这些示例可以在类路径中使用以下JAR之一运行:

uber jar的完整配置文件: Web配置文件uber jar: 已安装GlassFish的shell jar:$GF_INSTALLATION/lib/embedded/GlassFish-embedded-static-shell.jar

一旦您随身携带了上述任何一个jar文件,GlassFish就可以通过以下操作嵌入到您的应用程序中:

import org.glassfish.embeddable.*;

/** Create and start GlassFish */
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish();
glassfish.start();
假设您希望在嵌入GlassFish时启动8080 web容器端口,那么您必须执行以下操作:

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()

答案很全面,但我特别要求使用Glassfish 2.1.1解决方案,而不是使用Glassfish 3