独立Java EE应用程序服务器

时间:2015-03-24 05:22:09

标签: java java-ee jetty application-server

我正在开发独立应用程序。

应用程序像java -jar myapp.jar一样启动,并未在任何地方部署。我需要使用可嵌入的应用程序服务器。

到目前为止,我发现只有Jetty,但它不支持所有Java EE功能。还有其他选择吗?

2 个答案:

答案 0 :(得分:1)

查看spring-boot项目:

http://projects.spring.io/spring-boot/

它提供了使用嵌入式应用程序服务器构建可执行jar的功能。 Grizzly,Jetty,Tomcat和Undertow都是选择。 spring-boot项目本身提供了各种额外的支持,例如内置指标和开箱即用的健康检查。希望其中一个提供的应用程序服务器满足您的需求。

Undertow(http://undertow.io/)据说可以是一个完整的Java EE servlet 3.1容器,但我还没有亲自使用它。我参与的每个项目都发现Jetty和/或Tomcat足够了。

答案 1 :(得分:1)

我肯定会选择tomee。基本上它是tomcat与j2ee的类固醇:p

=============

这些是代码,我在我的本地笔记本电脑上进行了测试,它应该可行。请注意,您需要从此link下载tomee-embedded.jar,如果您想了解代码中究竟发生了什么,也可以在here中找到嵌入式jar的源代码。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;

import javax.ejb.embeddable.EJBContainer;

import org.apache.openejb.loader.IO;
import org.apache.tomee.embedded.EmbeddedTomEEContainer;


public class Main {

    public static void main(String[] args) {
    EJBContainer container = null;
    try {
        System.out.println("Start");
        final File war = createWar();
            final Properties p = new Properties();
            p.setProperty(EJBContainer.APP_NAME, "test");
            p.setProperty(EJBContainer.PROVIDER, EmbeddedTomEEContainer.class.getName());
            p.put(EJBContainer.MODULES, war.getAbsolutePath());
            p.setProperty(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT, "-1");

            System.out.println(war.getAbsolutePath());

            container = EJBContainer.createEJBContainer(p);
            System.out.println(container);
            System.out.println(container.getContext());
            final URL url = new URL("http://127.0.0.1:" + System.getProperty(EmbeddedTomEEContainer.TOMEE_EJBCONTAINER_HTTP_PORT) + "/test/index.html");
            System.out.println(getOk(url, 2));

        } catch (Throwable e) {
            System.err.println(e.getLocalizedMessage());
        e.printStackTrace();
    } finally {

            if (container != null) {
                container.close();
            }
        }
    }
    private static String getOk(final URL url, final int tries) throws Exception {
        try {
            return IO.readProperties(url).getProperty("ok");
        } catch (final IOException e) {
            if (tries > 0) {
                Thread.sleep(1000);
                return getOk(url, tries - 1);
            } else {
                throw e;
            }
        }
    }

    private static File createWar() throws IOException {
        final File file = new File(System.getProperty("java.io.tmpdir") + "/tomee-" + Math.random());
        if (!file.mkdirs() && !file.exists()) {
            throw new RuntimeException("can't create " + file.getAbsolutePath());
        }

        write("ok=true", new File(file, "index.html"));
        write("<beans />", new File(file, "WEB-INF/classes/META-INF/beans.xml"));
        return file;
    }

    private static void write(final String content, final File file) throws IOException {
        if (!file.getParentFile().mkdirs() && !file.getParentFile().exists()) {
            throw new RuntimeException("can't create " + file.getParent());
        }

        final FileWriter index = new FileWriter(file);
        index.write(content);
        index.close();
    }
}