简体   繁体   中英

Standalone Java EE Application Server

I am developing standalone application.

Application is started like java -jar myapp.jar and is not deployed anywhere. I need to use an embeddable application server.

So far I found only Jetty , but it doesn't support all Java EE features. Is there any other alternatives?

Look into the spring-boot project:

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

It provides features for building executable jars with embedded application servers. Grizzly, Jetty, Tomcat and Undertow are options. The spring-boot project itself provides various additional support such as built in metrics and health checks out of the box. Hopefully one of the provided application servers meets your needs.

Undertow ( http://undertow.io/ ) supposedly can be a full Java EE servlet 3.1 container, but I have not personally worked with it yet. Every project I have worked on found Jetty and/or Tomcat to be sufficient.

I would definitely go with tomee . basically it's tomcat with bit of steroid of j2ee :p

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

These are the code, I tested it in my local laptop and it should works. Note that you need to download the tomee-embedded.jar from this link and also the source code for the embedded jar can be found in here if you want to understand what exactly happen in the code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM