将嵌入式Jetty服务器从版本6迁移到版本7

时间:2010-05-26 11:46:26

标签: java jetty embedded-jetty

我有一个嵌入式servlet,我在单元测试中使用,看起来像这样:

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }

    public void handle(String url, HttpServletRequest request, HttpServletResponse response, int arg3) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        if(content != null) {
            final ServletOutputStream stream = response.getOutputStream();

            stream.print(content);
            stream.flush();
            stream.close();
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

    ....
}

这是使用Jetty的6.1.24版本编写的。我尝试切换到使用Jetty 7.1.1.v20100517,并将该代码更新为:

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }


    public void handle(String url, Request request, HttpServletRequest servletRequest, HttpServletResponse response) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        request.setHandled(true);

        response.setContentType("application/json");

        if(content != null) {
            response.setStatus(HttpServletResponse.SC_OK);
            final Writer stream = response.getWriter();

            stream.append(content);
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

但每当我尝试向服务器发出请求时,它都会无限期挂起。有没有人经历过类似的事情?它还将其打印到日志中:

log4j:WARN No appenders could be found for logger (org.eclipse.jetty.util.log).
log4j:WARN Please initialize the log4j system properly.
org.eclipse.jetty.server.Server@670655dd STOPPED
 +-UnitTestWebservices@50ef5502 started

1 个答案:

答案 0 :(得分:0)

似乎该特定版本可能存在问题。我使用此代码更改为版本7.0.2.v20100331

public class UnitTestWebservices extends AbstractHandler {

    private Server server;

    private Map<Route,String> data = new HashMap<Route,String>();

    public UnitTestWebservices(int port) throws Exception {

        server = new Server(port);
        server.setHandler(this);
        server.start();

    }

    public void handle(String url, final Request rawRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        final Route route = Route.valueOf(request.getMethod(), url);

        final String content = data.get(route);

        if(content != null) {
            final ServletOutputStream stream = response.getOutputStream();

            stream.print(content);
            stream.flush();
            stream.close();
        }
        else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

现在一切似乎都很美妙。