2013-06-22 13 views
10

私はJavaサーブレットを提供するためにTomcatを使用しています。サーブレットリクエストのみ、静的コンテンツもJSPもないので、アプリケーションに埋め込むことができるサーブレットコンテナを探していました。 Jettyを取り除いてサーブレットコンテナとして使用すると、それを感じることができました。これはスケーラビリティとメモリ占有量を小さくすることができます。[私はJettyのWebサーバーとその他のパーツは必要ありません。しかし、私はいくつかの質問をしましたが、サーブレットコンテナとしてのJettyの埋め込み

  1. サーブレットリクエストだけを提供するためにアプリケーションコードにJettyを埋め込むにはどうしたらいいですか?
  2. アプリケーションコードにJettyコードを埋め込むと、Jetty Versionsを簡単にアップグレードできますか?私は1つは、私はソースから使用する必要があります私のアプリ、中突堤のサーブレットコンテナを埋め込む必要がある場合
  3. 私は、ここに突堤コードを持っ http://git.eclipse.org/c/jetty/org.eclipse.jetty.project.git/snapshot/jetty-9.0.3.v20130506.tar.bz2、 桟橋-9.0.3.v20130506 /桟橋 - サーブレットや桟橋 - 9.0 .3.v20130506/jetty-servlets

私は自分のアプリケーションでAPIリクエストを提供するつもりで、パフォーマンスとスケーラビリティを主な制約として探しています。もちろんサーブレット3.0のサポート。

答えて

16

あなたが探しているのは、埋め込まれたシナリオでJettyを実行することです。

ゴールを達成するために必要なさまざまな要素を結びつける方法を示す例がたくさんあります。

embedded examples in the jetty source treeをチェックしてください。

記録のために、jettyスタンドアロンは実際に起動とクラスパスに関連するいくつかのブートストラップで埋め込まれています。これは同じコードであり、基本的に同じ方法で組み立てられています。

サーブレット3.0が必要であり、JSPに関心がないと述べたので、これはセットアップが簡単です。 (JSPはセットアップが面倒ですが、可能です)。

サーブレット3.0固有の埋め込みについては、githubでホストされている完全なサンプルプロジェクトがあります。要するに

https://github.com/jetty-project/embedded-servlet-3.0

、次の初期化コードを持っています。

package com.company.foo; 

import org.eclipse.jetty.annotations.AnnotationConfiguration; 
import org.eclipse.jetty.plus.webapp.EnvConfiguration; 
import org.eclipse.jetty.plus.webapp.PlusConfiguration; 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.webapp.Configuration; 
import org.eclipse.jetty.webapp.FragmentConfiguration; 
import org.eclipse.jetty.webapp.MetaInfConfiguration; 
import org.eclipse.jetty.webapp.TagLibConfiguration; 
import org.eclipse.jetty.webapp.WebAppContext; 
import org.eclipse.jetty.webapp.WebInfConfiguration; 
import org.eclipse.jetty.webapp.WebXmlConfiguration; 

public class EmbedMe { 
    public static void main(String[] args) throws Exception { 
     int port = 8080; 
     Server server = new Server(port); 

     String wardir = "target/sample-webapp-1-SNAPSHOT"; 

     WebAppContext context = new WebAppContext(); 
     // This can be your own project's jar file, but the contents should 
     // conform to the WAR layout. 
     context.setResourceBase(wardir); 
     // A WEB-INF/web.xml is required for Servlet 3.0 
     context.setDescriptor(wardir + "WEB-INF/web.xml"); 
     // Initialize the various configurations required to auto-wire up 
     // the Servlet 3.0 annotations, descriptors, and fragments 
     context.setConfigurations(new Configuration[] { 
          new AnnotationConfiguration(), 
          new WebXmlConfiguration(), 
          new WebInfConfiguration(), 
          new TagLibConfiguration(), 
          new PlusConfiguration(), 
          new MetaInfConfiguration(), 
          new FragmentConfiguration(), 
          new EnvConfiguration() }); 

     // Specify the context path that you want this webapp to show up as 
     context.setContextPath("/"); 
     // Tell the classloader to use the "server" classpath over the 
     // webapp classpath. (this is so that jars and libs in your 
     // server classpath are used, requiring no WEB-INF/lib 
     // directory to exist) 
     context.setParentLoaderPriority(true); 
     // Add this webapp to the server 
     server.setHandler(context); 
     // Start the server thread 
     server.start(); 
     // Wait for the server thread to stop (optional) 
     server.join(); 
    } 
} 
+1

jetty 9とservlet api 3.1を使用してバージョンを更新しました:https://github.com/jetty-project/embedded-servlet-3.1 – Kapep

関連する問題