2010-08-11 34 views
6

Đối với đơn vị xét nghiệm của tôi, tôi sử dụng một thử nghiệm-server đơn giản dựa trên Jetty:Servlet 3.0 hỗ trợ trong Jetty nhúng 8,0

package eu.kostia.textanalysis.webservices.jetty; 

import java.awt.Desktop; 
import java.net.URI; 

import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.webapp.WebAppContext; 

public class TestServer { 
static private final String CONTEXT_PATH = "/webservice"; 
static private final String PROJECT_HOME = System.getenv("MY_WORKSPACE_HOME") + "/WebServices"; 
static public final int PORT = 8080; 

private Server server; 
private Exception startException; 

private static class SingletonHolder { 
    private static final TestServer INSTANCE = new TestServer(); 
} 

/** 
    * Returns the singleton instance of the test server. 
    * 
    * @return the singleton instance of the test server. 
    */ 
public static TestServer getInstance() { 
    return SingletonHolder.INSTANCE; 
} 

private TestServer() { 
    server = new Server(PORT); 

    WebAppContext context = new WebAppContext(); 

    context.setDescriptor(PROJECT_HOME + "/web/WEB-INF/web.xml"); 
    context.setResourceBase(PROJECT_HOME + "/web"); 
    context.setContextPath(CONTEXT_PATH); 
    context.setParentLoaderPriority(true); 


    server.setHandler(context); 
} 

/** 
    * Start the test server. This method returns only when the server is 
    * complete started. There is no effect when you invoke this method and the 
    * server is already running. 
    */ 
public void start() { 
    if (!server.isRunning()) { 
    startException = null; 
    new Thread("TestServer") { 
    public void run() { 
    try { 
     server.start(); 
     server.join(); 
    } catch (Exception exc) { 
     startException = exc; 
    } 
    } 
    }.start(); 

    while (true) { 
    if (startException != null) { 
    throw new Error(startException); 
    } 

    // Block this method call until the server is started 
    if (server.isStarted()) { 
    return; 
    } 
    } 
    } 
} 

/** 
    * Stop the test server. 
    */ 
public void stop() { 
    try { 
    if (server.isRunning()) { 
    server.stop(); 
    } 
    } catch (Exception e) { 
    throw new Error(e); 
    } 
} 

/** 
    * Returns {@code true} is the server is running. 
    * 
    * @return {@code true} is the server is running. 
    */ 
public boolean isRunning() { 
    return server.isRunning(); 
} 

public static void main(String[] args) throws Exception { 
    TestServer.getInstance().start(); 
    Desktop.getDesktop().browse(new URI("http://localhost:8080/webservice/")); 
} 

} 

Nó hoạt động rất tốt cho servlet cấu hình trong web.xml nhưng tôi muốn bây giờ như thế để sử dụng cú pháp chú thích mới được giới thiệu bởi Đặc tả Servlet 3.0, ví dụ:

@WebServlet(urlPatterns = {"/hello"}) 
public class HelloServlet extends HttpServlet { 
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
       PrintWriter writer = response.getWriter(); 
       writer.print("<h1>HttpServlet using Servlet 3.0</h1>"); 
     } 
} 

Tôi có thể cấu hình Jetty trong lớp TestServer để xử lý các servlet dựa trên chú thích không?

Trả lời

0

Jetty 8 đang triển khai đặc tả servlet 3.0 nhưng vẫn còn thử nghiệm.

Bạn cũng có thể sử dụng plugin thủy tinh nhúng 3 để chạy thử nghiệm của mình. Xem các liên kết dưới đây để biết một số thông tin: http://wikis.sun.com/display/GlassFish/3.1EmbeddedOnePager http://ocpsoft.com/java/using-embedded-glassfish-with-maven/ http://embedded-glassfish.java.net/

Tôi nhận ra khi tôi viết những dòng này mà không có nguồn có thẩm quyền cho sử dụng plugin Glassfish theo cách mà Jetty thường được sử dụng. Tuy nhiên nó hoạt động theo cách tương tự.

Tôi hy vọng điều này sẽ giúp ít nhất một chút.

8

Thêm vào mã của bạn

context.setConfigurations(new Configuration[] { 
       new AnnotationConfiguration(), new WebXmlConfiguration(), 
       new WebInfConfiguration(), new TagLibConfiguration(), 
       new PlusConfiguration(), new MetaInfConfiguration(), 
       new FragmentConfiguration(), new EnvConfiguration() }); 

Bạn chỉ cần thiết lập các AnnotationConfiguration để có được những tính năng tự động phát hiện các lớp chú thích để làm việc. Phần còn lại của cấu hình là để bạn có thể bật các khía cạnh khác của vùng chứa. Giả sử bạn sẽ có thể làm điều này từ dòng lệnh, sử dụng OPTIONS = chú thích, jsp, (vv ...), nhưng tôi chưa bao giờ làm việc đó. Ít nhất theo cách này, nó sẽ nhận các lớp chú thích của bạn đúng cách trong môi trường nhúng.

Cũng giống như một lưu ý phụ, có vẻ như dự án cầu cảng Eclipse đã tắt chú thích theo mặc định, trong khi các yêu cầu riptide để chúng được bật theo mặc định. Tôi đoán đây là một sự khác biệt trong các tập tin cấu hình.

+0

Tôi nhận thấy bài đăng là cổ, nhưng tôi thấy rằng tôi đã gặp sự cố tương tự với máy chủ được nhúng ngay cả một năm sau bài đăng này. –

+1

Tôi thấy AsyncContext không hoạt động tốt trong Jetty. Tôi đã từ bỏ nó trong thời gian có lợi cho Embedded Tomcat 7. Có vẻ như nó đang hoạt động tốt trong thời gian này. –

1

Trả lời thêm một năm sau đó.

Trong phiên bản hiện tại của Jetty (8.1), bạn có thể thực hiện chính xác những gì bạn muốn với dòng lệnh:

java -jar start.jar OPTIONS=annotations,plus etc/jetty-plus.xml 

gọi từ thư mục cầu cảng nhà.

Các vấn đề liên quan