1

好吧,我完全被卡住了。我想以嵌入式模式运行 Tomcat,这样我就可以测试应用程序,而无需在单独的进程中运行服务器。我错过了一些简单、愚蠢和重要的东西,我需要你的帮助才能看到它。

此测试失败,出现 HTTP 错误 400,错误请求。我试过 MemoryProtocolHandler, context.invoke(), ...我不知道该怎么做。也许你看到了一些简单的东西。

包 ca.jbrains.jsfunit.learning.test;

导入 org.apache.catalina.Container;
导入 org.apache.catalina.Context;
导入 org.apache.catalina.Engine;
导入 org.apache.catalina.connector.Connector;
导入 org.apache.catalina.connector.Request;
导入 org.apache.catalina.realm.MemoryRealm;
导入 org.apache.catalina.startup.Embedded;
导入 org.junit.After;
导入 org.junit.Test;

导入 com.gargoylesoftware.htmlunit.WebClient;

公共类 LearnEmbeddedTomcatTest {
    私有嵌入式嵌入式;
    私有容器主机;
    私人引擎引擎;

    @测试
    公共无效 deploySampleApplicationFromFileSystem() 抛出异常 {
        String tomcatPath = "/Users/jbrains/ThirdParty/apache-tomcat-5.5.28-embed";

        // 创建嵌入式服务器
        嵌入式 = 新嵌入式();
        嵌入式.setCatalinaHome(tomcatPath);
        嵌入的.setRealm(new MemoryRealm());

        // 创建引擎
        引擎 = 嵌入式.createEngine();
        engine.setDefaultHost("localhost");

        // 创建一个默认的虚拟主机
        host = embedded.createHost("localhost", tomcatPath + "/webapps");
        engine.addChild(主机);

        // 创建应用上下文
        上下文上下文 = Embedded.createContext("TddJsfWeb", tomcatPath
                + "/webapps/TddJsfWeb");
        主机.addChild(上下文);

        // 安装组装好的容器层次结构
        嵌入式.addEngine(引擎);

        // 组装并安装一个默认的 HTTP 连接器
        连接器连接器 = embedded.createConnector("localhost", 8080,
                “http”);
        嵌入式.addConnector(连接器);

        // 启动嵌入式服务器
        嵌入式.setAwait(true);
        嵌入的.start();

        WebClient webClient = new WebClient();
        webClient.getPage("http://localhost:8080/TddJsfWeb/static.xhtml");
    }
}

解压后的 .war 肯定位于解压/Users/jbrains/ThirdParty/apache-tomcat-5.5.28-embed/webapps/TddJsfWeb/...static.xhtml的 .war 文件夹的根目录中。

拜托,拜托,让我看看我有多愚蠢。谢谢。

4

3 回答 3

1

我对 Tomcat 也有类似的经历。我最终改用了 Jetty——从代码的角度来看,管理起来要简单得多。

于 2010-01-19T13:48:43.097 回答
0

为此,我想将Jetty置于 Tomcat 之上。要运行 Jetty,我只需要:

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class StartJetty {
       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(8081);
               server.setConnectors(new Connector[] { connector });

               WebAppContext bb = new WebAppContext();
               bb.setServer(server);
               bb.setContextPath("/");
               bb.setWar("src/main/webapp");
               server.addHandler(bb);

               try {
                       System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY
KEY TO STOP");
                       server.start();

                       //Launch browser
                       if (Desktop.isDesktopSupported())
                               if (Desktop.getDesktop().isSupported(Desktop.Action.BROWSE))
                                       try {
                                               Desktop.getDesktop().browse(new URI("http://localhost:8081/"));
                                       }
                                       catch (IOException ioe) {
                                               ioe.printStackTrace();
                                       }
                                       catch (URISyntaxException use) {
                                               use.printStackTrace();
                                       }

                       System.in.read();
                       System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
                       server.stop();
                       server.join();
               }
               catch (Exception e) {
                       e.printStackTrace();
                       System.exit(100);
               }
       }
}
于 2010-01-19T14:03:32.900 回答
0

如果 Jetty 对您来说运行良好,那就太棒了,但是如果您想更深入地了解 Tomcat,您可以在发出请求之前浏览您的应用程序并查看它们是否可用。

for (Container cont : host.findChildren()) {
    if (cont instanceof StandardContext) {
        StandardContext ctx = (StandardContext) cont;
        ServletContext servletContext = ctx.getServletContext();
        log.debug("Context initialized: {}", servletContext.getContextPath());
        String prefix = servletContext.getRealPath("/");
        try {
            if (ctx.resourcesStart()) {
                log.debug("Resources started");
            }
            log.debug("Context - available: {} privileged: {}, start time: {}, reloadable: {}", new Object[] { ctx.getAvailable(), ctx.getPrivileged(), ctx.getStartTime(), ctx.getReloadable() });
...
第三种选择是研究比 Tomcat 或 Jetty 更简单的容器,例如 Winstone:http ://winstone.sourceforge.net/

于 2010-09-06T16:52:23.503 回答