0%

Jetty一段时间出现404错误,提示jsp找不到(linux/tmp/)

##一、问题及原因分析

最近一个老的应用被部署到jetty后定时出现404错误,

老应用的代码比较糟糕,也出过不少问题,本以为是程序问题、

查询负载、线程、dump文件、io都没有明显异常,挺奇怪一直没找到原因

查看了jetty启动是war解压目录/tmp/jetty-0.0.0.0-8100-hrs-web.war-_hrs-any-/webapp

因为在/tmp目录,应该是有问题,linux会清除/tmp目录。

至于/tmp目录多久清除一次,看

cat /etc/cron.daily/tmpwatch
flags=-umc                                                                                                                                                              
/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
        -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
        -X '/tmp/hsperfdata_*' 720 /tmp
/usr/sbin/tmpwatch "$flags" 720 /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
    if [ -d "$d" ]; then
        /usr/sbin/tmpwatch "$flags" -f 720 "$d"
    fi
done

二、解决方法

问题找到了,那如何指定war解压目录? 在网上找到一份详细说明,如下:

http://docs.codehaus.org/display/JETTY/Temporary+Directories

Jetty itself has no temporary directories, but each web application can be assigned a directory into which the war is unpacked, JSPs compiled on-the-fly etc.

The algorithm for determining a webapp's temporary directory location is as follows:

    Try to use an explicit directory specifically for this webapp:
        Iff webapp.getTempDirectory() is set, use it. Do NOT delete it on jvm exit.
        Iff javax.servlet.context.tempdir context attribute is set for this webapp && exists && writeable, then use it. Do NOT delete on jvm exit.
    Create a directory based on global settings. The new directory will be called "Jetty_"host""port""context""+virtualhost :
        Iff $(jetty.home)/work exists create the directory there. Do NOT delete on jvm exit. Do NOT delete contents if dir already exists.
        Iff WEB-INF/work exists create the directory there. Do NOT delete on jvm exit. Do NOT delete contents if dir already exists.
        Else create dir in $(java.io.tmpdir). Set delete on jvm exit. Delete contents if dir already exists.

It is important to note that a temporary directory will have its contents deleted when the webapp is stopped unless either:

    it is called "work"
    it pre-existed the deployment of the webapp

Once a tempory directory has been allocated, a File instance for it is set and retrievable as the javax.servlet.context.tempdir attribute of the web application.

意思是如果$(jetty.home)/work文件夹存在,则Jetty会使用该目录作为工作目录。所以,最后我选择了在$(jetty.home)/work下建立一个统一的work目录,这样最方便部署,jetty 每台机子统一安装就可以。

另外也可以直接通过改变JAVA_OPTIONS指定临时目录:

jetty.sh

#####################################################
# Add jetty properties to Java VM options.
#####################################################
JAVA_OPTIONS+=("-Djetty.home=$JETTY_HOME" "-Djava.io.tmpdir=$TMPDIR")
  • 本文作者: 6x
  • 本文链接: https://6xyun.cn/article/26
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-ND 许可协议。转载请注明出处!