2011-12-20 227 views
1

我一直在尝试修改我的heroku应用程序的嵌入式tomcat配置。我已经使用下面的维基链接安装了heroku应用程序,它配置了一个简单的嵌入式tomcat。修改嵌入式tomcat的配置webapp

http://devcenter.heroku.com/articles/create-a-java-web-application-using-embedded-tomcat

的源代码是在这里:

public static void main(String[] args) throws Exception { 

    String webappDirLocation = "src/main/webapp/"; 
    Tomcat tomcat = new Tomcat(); 

    //The port that we should run on can be set into an environment variable 
    //Look for that variable and default to 8080 if it isn't there. 
    String webPort = System.getenv("PORT"); 
    if(webPort == null || webPort.isEmpty()) { 
     webPort = "8080"; 
    } 

    tomcat.setPort(Integer.valueOf(webPort)); 

    tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); 
    System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath()); 

    tomcat.start(); 
    tomcat.getServer().await(); 

} 

问题:

  1. 由于我使用的是嵌入式的tomcat,我该如何配置默认的会话超时我的web应用程序?由于某种原因,它似乎默认为30分钟?我想设置为像一个星期的东西。
  2. 如果我在eclipse中启动应用程序,我该如何设置autodeploy = true,以便每次修改java代码时都不必编译和重新启动我的应用程序?
  3. 有没有办法设置我的web.xml和server.xml?
  4. 我该如何运行apache tomcat管理器?

因特网上的文档不是很清楚。你能帮忙吗?

在此先感谢.. 基兰

+0

会话超时只是在web.xml中配置的,对吧?使用web.xml进行应用程序配置不应随嵌入式tomcat而改变。 – 2011-12-21 16:52:14

回答

1

使用Context.setSessionTimeout(INT)。 Java文档here。这是同样的主类设置为30天会话超时:

package launch; 
import java.io.File; 
import org.apache.catalina.startup.Tomcat; 
import org.apache.catalina.Context; 


public class Main { 

    public static void main(String[] args) throws Exception { 

     String webappDirLocation = "src/main/webapp/"; 
     Tomcat tomcat = new Tomcat(); 

     //The port that we should run on can be set into an environment variable 
     //Look for that variable and default to 8080 if it isn't there. 
     String webPort = System.getenv("PORT"); 
     if(webPort == null || webPort.isEmpty()) { 
      webPort = "8080"; 
     } 

     tomcat.setPort(Integer.valueOf(webPort)); 

     Context ctx = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath()); 
     ctx.setSessionTimeout(2592000); 
     System.out.println("configuring app with basedir: " + new File("./" + webappDirLocation).getAbsolutePath()); 

     tomcat.start(); 
     tomcat.getServer().await(); 
    } 
} 

通知的Context ctx = ...ctx.setSessionTimeout(...)

对于Tomcat管理器,当您以这种方式将Tomcat嵌入到应用程序中时,无法使用它。我很好奇你想用Tomcat Manager做什么?

您通常会从server.xml开始做的任何事情都可以通过嵌入API完成。嵌入的重点在于你可以通过编程来配置一切。

您仍然可以像平常一样设置自己的web.xml。只需将其添加到WEB-INF目录下的目录中,该目录下的目录为webappDirLocation。但是,我很好奇你想要在web.xml?由于您拥有主应用程序循环,因此您可以根据主要方法设置所需的任何配置。我强烈建议您在主循环中初始化您需要的所有内容,并根据特定环境(例如JDBC url)读取OS环境变量。

最后,对于Eclipse,您不需要热部署,因为您没有使用容器部署模型。您可以简单地在Eclipse中使用“Debug as ...”运行应用程序,并且Eclipse会在您更改代码时自动编译和重新加载代码。它与热部署不完全相似。例如,它不会用新的方法签名重新加载类。但与使用容器相比,循环整个应用的速度要快得多,所以总的来说,我发现它的效率更高。