zoukankan      html  css  js  c++  java
  • Executable WARs with Jetty « EclipseSource Blog

    Executable WARs with Jetty « EclipseSource Blog

    on Oct 2nd, 2009Executable WARs with Jetty

    Today I want to talk about one of the younger members in the Eclipse family: Jetty. It is great to have such an interesting project on board and it is yet another example of how Eclipse has become more than just an IDE.

    What I wanted to with jetty was to create an executable, standalone and self-contained WAR. I first encountered this concept in Hudson. The hudson.war contains an embedded Winstone servlet container, which makes it possible to run the application by executing

    java -jar hudson.war

    This makes test driving the application really simple. The idea was to do the same with Jetty. Embedding the Jetty runtime in the war proved to be the easy part, as it was just a matter of declaring the jetty dependencies in the maven pom.xml.

    The tricky part was telling jetty where to find the war-file to serve. My first try was to hardcode the filename, but that left a foul aftertaste. Finding a solution took quite some time, which is why I am posting this for future reference. This is the Main-Class used to bootstrap Jetty (adapted from the Wicket quickstart archetype):

    import org.mortbay.jetty.Connector;
    import org.mortbay.jetty.Server;
    import org.mortbay.jetty.bio.SocketConnector;
    import org.mortbay.jetty.webapp.WebAppContext;
     
    public class Start {
     
      public static void main(String[] args) throws Exception {
        Server server = new Server();
        SocketConnector connector = new SocketConnector();
     
        // Set some timeout options to make debugging easier.
        connector.setMaxIdleTime(1000 * 60 * 60);
        connector.setSoLingerTime(-1);
        connector.setPort(8080);
        server.setConnectors(new Connector[] { connector });
     
        WebAppContext context = new WebAppContext();
        context.setServer(server);
        context.setContextPath("/");
     
        ProtectionDomain protectionDomain = Start.class.getProtectionDomain();
        URL location = protectionDomain.getCodeSource().getLocation();
        context.setWar(location.toExternalForm());
     
        server.addHandler(context);
        try {
          server.start();
          System.in.read();
          server.stop();
          server.join();
        } catch (Exception e) {
          e.printStackTrace();
          System.exit(100);
        }
      }
    }

    The interesting bit is the getProtectionDomain()/getCodeSource() part, which tells us the location of the war-file. That’s all there is to it. Presto, executable web-application powered by Jetty in jar.

    Edit: Added the import statements as per Tim’s suggestion.

  • 相关阅读:
    python用于web题里写解密脚本
    改变checkbox和radio的默认样式
    div内元素垂直居中
    icheck.js插件
    glyphicons字形图标
    没有内容的span元素下掉问题
    临界区保护
    信号量的使用&生产者消费者问题
    空闲线程和钩子函数
    线程的时间片轮询调度
  • 原文地址:https://www.cnblogs.com/lexus/p/2386122.html
Copyright © 2011-2022 走看看