我在服务器上运行Glassfish v2.1.1上的应用程序,我正在寻找生成便携式(读取:无需安装)版本的应用程序。我不想转移到另一个容器(Glassfish 3,Tomcat等),因为它会引入新的问题和错误。我更喜欢坚持使用同一个容器。
我已经开始拆分Glassfish发行版,并且有一些引用路径,暗示需要安装。有没有人成功地生成基于v2.1.1的便携式Glassfish?
答案 0 :(得分:-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 Web个人资料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-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()