Thursday, March 28, 2024
HomeJavaDo not Name System.exit() on Java Internet Software in Tomcat

Do not Name System.exit() on Java Internet Software in Tomcat


I’ve just lately come throughout a code snippet, the place the programmer was utilizing System.exit() if the applying failed to accumulate mandatory sources after a few retries. His reasoning was that the applying can’t perform if important sources just like the database should not accessible or there’s no disk area to jot down data within the File system. Okay, I hear you; however System.exit() in Java Internet utility, which runs inside both internet server or utility server, which itself is Java program just isn’t a good suggestion in any respect. Why? as a result of invoking System.exit() kills your JVM, invoking this from Tomcat or Jetty, won’t solely kill your utility however the probably server itself. This may be doubtlessly harmful if that server additionally hosts different crucial purposes, which isn’t unusual in any respect.

As per my expertise, System.exit() calls are fairly frequent in overly broad try-catch blocks in internet utility start-up code that hundreds
setting variables, properties information, hook up with MQ Sequence, establishes database connection, opens socket connections, and so forth.

That is nonetheless okay if you’re writing a core Java primarily based server, the place every utility has its personal JVM, however with internet utility deployed on Tomcat, JBoss, WebSphere, Weblogic, or some other utility server, utilizing System.exit() is an enormous mistake. In worst case may end up in an outage for plenty of different crucial utility.

Alternatively, there are methods to stop your internet utility from another person’s mistake, by enabling Safety Supervisor. System.exit() and Runtime.exit() each undergo the safety supervisor. Enabling the Safety supervisor will catch these calls and cut back them into an exception reasonably than shutting down the entire VM.

It is not tough to allow the safety supervisor in most utility servers, Tomcat, JBoss each have documented steps to allow safety Supervisor.

Why you shouldn’t use System.exit() in Java Internet utility

I believe it is well-known for senior Java developer that System.exit() should not be used besides in e.g. command-line instruments; Many newbie Java programmers, although accustomed to System.exit(), could not know that utilizing them in a Servlet/JSP code may end up in shutdown of server itself, so you probably have of any of them in your staff them take a while to teach them about coding in Java internet utility.

The truth is coding a Java primarily based internet utility is completely completely different then coding a core Java utility like core issues like threading, object pooling, parsing are all carried out by Internet server and it is prohibited for utility code to create threads.

The truth is use of ThreadLocal variable can create reminiscence leak in Java web-app, so coding in web-app require extra warning than in core Java utility.

By the best way there are different causes, why utilizing System.exit is completely horrible. Particularly whenever you coping with un-managed sources, for those who do not launch sources correctly and hope that OS will do the clean-up for you, then it may result in a short lived useful resource leak, till OS actually clear stuff created by your Java utility.

Using System.exit() in Java Web Application

What does this all imply? Do System.exit() has any authentic use? In fact there are various instances the place use of System.exit is crucial. Someday you actually need to shut your JVM as soon as carried out, particularly if it spawned from scheduling software program like Management-M or Autosys. For instance command line Java purposes and scheduled duties, can use System.exit(). Ideally, if you’re certain that calling System.exit() won’t have any facet impact, you need to use it. Here’s a pattern use of system.exit() in core Java utility :

public static void most important( String[] args ){

    strive
    {
        processFxRatesFileAndPutIntoDatabase();

    }  catch ( Exception e )
    {
        logError(e);
        // if it's worthwhile to exit with a failed standing then System.exit(statusCode)
        // is ok right here.
        // in any other case program will full efficiently, as soon as it return from most important() methodology
    }

 }

Bear in mind, if you’re utilizing Java program as command-line instrument then you may return System.exit(0) or System.exit(1) as whether or not it succeeded or failed. A non-zero exit standing code, often signifies irregular termination.

You possibly can return completely different error codes to point completely different errors e.g. dangerous arguments, cannot discover file, couldn’t hook up with database and so forth. I hope you discover the distinction between utilizing System.exit(1) and letting the Java program full efficiently. When java platform will terminate with System.exit() it is going to achieve this with a non-zero standing (so long as the primary thread ends and there aren’t any operating daemon threads).

That’s all about Why you shouldn’t use System.exit() inside Java utility. It may be harmful and doubtlessly shut down the entire server, which is internet hosting different crucial Java utility. For instance, you probably have multiple utility in tomcat and one in all them name System.exit() then the entire tomcat might be shut down and all different internet purposes may also be shutdown.

As a security internet or Insurance coverage, you have to allow the Safety Supervisor of your Server. It catches lots of safety issues! Together with an intentional and unintentional name to System.exit() or Runtime.exit(), turning it into an exception reasonably than shutting down the entire JVM.

You’re comparatively protected to make use of System.exit() in command-line Java utility, as they run on their very own JVM and can be utilized to sign success and failure by returning completely different standing code in case of failure.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments