1

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

我已经开始拆解 Glassfish 发行版,那里有一些路径参考暗示需要安装。有没有人成功地生成了基于 v2.1.1 的便携式 Glassfish?

4

1 回答 1

-1

嵌入 GlassFish 并将应用程序部署到嵌入式 GlassFish 的基本示例:

这些示例可以在您的 CLASSPATH 中使用以下任一 jar 运行:

完整配置文件 uber jar:http: //download.java.net/maven/glassfish/org/glassfish/extras/glassfish-embedded-all/3.1/glassfish-embedded-all-3.1.jar 网络配置文件 uber jar:http:// /download.java.net/maven/glassfish/org/glassfish/extras/glassfish-embedded-web/3.1/glassfish-embedded-web-3.1.jar 安装了 GlassFish 的 shell jar:$GF_INSTALLATION/lib/embedded/glassfish-embedded-静态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()
于 2012-06-15T06:09:00.137 回答