如何在不重启的情况下删除Apache Axis临时文件

3
我们正在使用Apache Axis与Web服务进行通信。它运行良好,但在一天的过程中,我们会生成1GB的临时文件。如果我们重新启动服务,这些文件将被删除,但每天需要重新启动服务以避免磁盘空间不足似乎有点愚蠢。
有没有简单的解决方法?

问题在axis2 1.6.2中仍然存在。 - Adam Siemion
7个回答

5
我们找到了一种编程方式来避免生成这些文件 - 这听起来像是更好的维护方法。我在AXIS2-3919问题的评论中发布了此链接,但为了保险起见,我在这里也复制一份:
实际上,每次创建AXIS配置上下文时,文件都会部署到临时文件夹中。此外,似乎生成的存根可以通过构造函数接受现有的配置对象。因此,以下Spring配置帮助我们解决了这个问题(省略了不相关的bean和类名):
<bean id="....Stub" factory-bean="...." factory-method="...." scope="request">
    <!-- this next element effects the proxying of the surrounding bean, 
        needed because .... will try to set the stub out of request scope -->
    <aop:scoped-proxy/>
    <constructor-arg index="0" >
        <!-- The WS stub is created here, and passed to the factory-method of ... as a parameter -->
        <bean class="com......ws.....Stub" scope="prototype">
            <constructor-arg ref="axisConfigContext" />
        </bean>
    </constructor-arg>
</bean>

<!-- Exists to avoid deployment of axis jar into temp dir for each request. See     AXIS2-3919 for more details. -->
<bean id="axisConfigContext" 
        class="org.apache.axis2.context.ConfigurationContextFactory" 
        factory-method="createConfigurationContextFromFileSystem">
    <constructor-arg index="0"><null /></constructor-arg>
    <constructor-arg index="1"><null /></constructor-arg>
</bean>

很好的发现,这对我来说完美无缺。我使用了不同的Spring / Autowire方法,但最终结果是相同的。不再需要在每个请求上重新部署JAR文件。 - John Strickler

2

我尝试了由proudandhonour提供的创建静态块的解决方案,它非常有效。它仅为第一个请求创建临时文件。我还将Linux服务器上的uilimit更新为16000。现在我不需要删除临时文件了。

private static ConfigurationContext configContext; 
static{   
   configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);  
}

欢迎来到 StackOverflow。请正确格式化您的问题!将与您的 Java 代码相对应的部分标记为代码块。 - Bruno Peres

2

每当创建新的ConfigurationContext时,都会创建临时文件。因此,请在静态块中仅创建一次ConfigurationContext,并在所有后续调用中使用它。

private static ConfigurationContext configContext;

static {
     configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
}

public callService() {
    Stub stub = new Stub(configContext, END_POINT_URL);
    //code
}

如果您不传递上下文,axis2将在每次调用时创建一个新的上下文。

1

0

我最近遇到了这个确切的问题,它对我们来说是一个停滞点。我有几个解决方案:

  1. 将java.io.tmpdir设置为不存在的目录。这样做可能会产生一些不良后果!
  2. 创建org.apache.axis2.deployment.util.Utils的定制版本并编辑createClassLoader函数。在axis库之前部署此函数到类路径中。此函数接受一个布尔值extractJars,如果无法复制文件(例如如果临时目录不存在),则会使用jar文件的原始副本进行工作。同样会产生一些不良后果。这个主题有许多变化,我还尝试添加一些代码来创建一个仅包含单个jar文件的副本,但我认为这个解决方案更好,因为它使用了现有的功能。以下是我的函数版本:

    public static ClassLoader createClassLoader (URL[] urls,
                                        ClassLoader serviceClassLoader,
                                        boolean extractJars,
                                        File tmpDir,
                                        boolean  isChildFirstClassLoading) {
    List embedded_jars = Utils.findLibJars(urls[0]);
    return createDeploymentClassLoader(urls, serviceClassLoader,
                                   embedded_jars, isChildFirstClassLoading);
    }
    

希望这能有所帮助。


我希望它能够做到,但是我不能保证一个jar在Web应用程序的类路径中是否在另一个jar之前。另一方面,任何有这些更改的更新的jar的人都将成为救星。 - abdelrahman-sinno

0
如果您知道临时目录的路径,可以通过cron每6、12等时间运行rm -rf /path/to/temp/*命令来清空它。
或者,如果您正在Linux系统上运行,可以编写bash脚本以在达到特定大小时清空此目录,并将其放入crontab中。

谢谢您的回复。我本来希望能避免这种笨拙的方法,但既然这是我得到的唯一答案,我会接受它。 - Programming Guy

0

除了每天重启,我找不到其他的方法。除非停止该进程,否则无法删除tmp文件。我已经更改了系统属性"java.io.tmpdir"到另一个具有更多磁盘空间的位置,这样至少它不会因为磁盘空间不足而停止运行。

System.setProperty("java.io.tmpdir","/opt/Axis2Temp"); 

其次,这些文件还存在另一个问题,运行应用程序几个小时后,会抛出“打开文件过多”异常,如下所示:
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-7 org.apache.axis2.deployment.util.Utils.[createClassLoader] (856) - Exception extracting jars into temporary directory : java.io.FileNotFoundException: /tmp/axis2-tmp-9161756920591296931.tmp/axis21477916618765108874addressing-1.6.0.mar (Too many open files) : switching to alternate class loading mechanism
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-7 org.apache.axis2.deployment.util.Utils.[createClassLoader] (860) - java.io.FileNotFoundException: /tmp/axis2-tmp-9161756920591296931.tmp/axis21477916618765108874addressing-1.6.0.mar (Too many open files)
为了解决这个问题,我已经将打开文件的ulimit增加到4000。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接