可以使用Java程序的退出代码来检测磁盘空间不足异常吗? [英] Can one use the exit code of a Java program to detect out of disk space exceptions?

查看:211
本文介绍了可以使用Java程序的退出代码来检测磁盘空间不足异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java程序是从Windows脚本中调用的.

My Java program is called from a windows script.

在仍从JAR文件加载类文件的同时,是否可以使用Java退出代码来确定Java程序是否由于磁盘空间不足而提前终止?

Is it possible to use the Java exit code to determine whether the Java program was terminated prematurely due to out of disk space while it is still loading the class files from the JAR file?

我尝试了内存不足异常,它返回退出代码1,但磁盘空间不足则返回退出代码0.这是正确的行为吗?

I tried out of memory exception and it returns an exit code 1 but out of disk space returns exit code 0. Is this correct behaviour?

子类方法:

public int executeBatch() {
        logger.info("executeBatch() - Send Email Alert Start");
        try {
            alertTransactionMgr.sendEmailAlert();
        } catch (Exception e) {
            throw new Exception(e);
        }
        logger.info("executeBatch() - Send Email Alert End");
        return 0;
    } 

父方法:

public int execute() {

        this.trx = createTransaction();

        try {
            returnCode = executeBatch();

        } catch (Exception e) {
            printLogErrorMsg("Job Failed caused by the Exception.", e);
            returnCode = -1;
            trx.setStatus("Failure");
            updateBatchTransaction(trx);

        }
        return returnCode;
    }

Windows批处理脚本

Windows batch script

@echo off

set ERRLVL=0

java -cp %CLASSPATH% com.test.runner.MainBatchRunner
if not (%ERRORLEVEL%)==() (
    set ERRLVL=%ERRORLEVEL%
)

echo Delete Files that are more than 30 old
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c echo del %BATCH_LOG_DIR%\@file"
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c del %BATCH_LOG_DIR%\@file"

echo Program exit %ERRLVL%
echo Program exit %ERRLVL% >> %BATCH_LOG_FILE%

exit /B %ERRLVL%

OutOfMemory的输出: [INFO] [2015-06-29 18:05:01,960] [org.springframework.context.support.ClassPathXmlApplicationContext]-刷新org.springframework.context.support.ClassPathXmlApplicationContext@4b222f:显示名称[org.springframework.context.support .ClassPathXmlApplicationContext @ 4b222f];启动日期[SGT 2015年6月29日星期一:18:05:01];上下文层次结构的根 [INFO] [2015-06-29 18:05:02,050] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-从文件[D:\ batch \ dev \ batch_home \ bin \ spring \ applicationContext中加载XML bean定义-test.xml]

Output for OutOfMemory: [INFO ][2015-06-29 18:05:01,960][org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b222f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b222f]; startup date [Mon Jun 29 18:05:01 SGT 2015]; root of context hierarchy [INFO ][2015-06-29 18:05:02,050][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\batch\dev\batch_home\bin\spring\applicationContext-test.xml]

删除超过30个旧文件

del D:\ batch \ dev \ batch_home \ log \"TEST_20150629_173016.log" 程序出口1

del D:\batch\dev\batch_home\log\"TEST_20150629_173016.log" Program exit 1

磁盘空间不足的输出: [INFO] [2015-06-29 19:05:01,960] [org.springframework.context.support.ClassPathXmlApplicationContext]-刷新org.springframework.context.support.ClassPathXmlApplicationContext@4b222f:显示名称[org.springframework.context.support .ClassPathXmlApplicationContext @ 4b222f];启动日期[SGT 2015年6月29日星期一:19:05:01];上下文层次结构的根 [INFO] [2015-06-29 19:05:02,050] [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-从文件[D:\ batch \ dev \ batch_home \ bin \ spring \ applicationContext中加载XML bean定义-test.xml]

Output for Out of disk space: [INFO ][2015-06-29 19:05:01,960][org.springframework.context.support.ClassPathXmlApplicationContext] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b222f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@4b222f]; startup date [Mon Jun 29 19:05:01 SGT 2015]; root of context hierarchy [INFO ][2015-06-29 19:05:02,050][org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from file [D:\batch\dev\batch_home\bin\spring\applicationContext-test.xml]

删除超过30个旧文件

del D:\ batch \ dev \ batch_home \ log \"TEST1_20150629_180030.log" 程序出口0

del D:\batch\dev\batch_home\log\"TEST1_20150629_180030.log" Program exit 0

推荐答案

您的批处理文件已损坏. ERRORLEVEL永远不会为空,除非其中有些愚蠢的操作(例如将其设置为字符串),否则它总是包含数字.

Your batch file is broken. ERRORLEVEL is never empty, it's always got a number in it, unless you do something stupid like set it to a string.

@echo off

set ERRLVL=0

java -cp %CLASSPATH% com.test.runner.MainBatchRunner
@rem if not (%ERRORLEVEL%)==() (
@rem    set ERRLVL=%ERRORLEVEL%
@rem)
if %ERRORLEVEL% != 0 set ERRLVL=%ERRORLEVEL%

echo Delete Files that are more than 30 old
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c echo del %BATCH_LOG_DIR%\@file"
forfiles /p "%BATCH_LOG_DIR%" /s /m %2*.log /d -%ARCHIVE_DAYS% /c "cmd /c del %BATCH_LOG_DIR%\@file"

echo Program exit %ERRLVL%
echo Program exit %ERRLVL% >> %BATCH_LOG_FILE%

exit /B %ERRLVL%

这篇关于可以使用Java程序的退出代码来检测磁盘空间不足异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆