簡體   English   中英

獨立Java EE應用程序服務器

[英]Standalone Java EE Application Server

我正在開發獨立應用程序。

應用程序的啟動類似於java -jar myapp.jar ,並且沒有部署在任何地方。 我需要使用嵌入式應用程序服務器。

到目前為止,我只找到了Jetty ,但它不支持所有Java EE功能。 還有其他選擇嗎?

查看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足夠了。

我絕對會和tomee一起去。 基本上是tomcat和j2ee的類固醇有點:p

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

這些是代碼,我在本地筆記本電腦上對其進行了測試,它應該可以工作。 請注意,您需要從此鏈接下載tomee-embedded.jar,並且如果您想了解代碼中實際發生的事情,還可以在此處找到嵌入式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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM