Don't Run System.Exit() On Coffee Spider Web Application
I receive got lately come upwards across a code snippet, where programmer was using System.exit() if application failed to instruct necessary resources afterward dyad of retry. His reasoning was that since, application cannot function, if essential resources similar database is non available or at that spot is no disk infinite to write records inwards File system. Ok, I listen you; simply System.exit() inwards Java Web application, which runs within either spider web server or application server, which itself is Java plan is non a practiced persuasion at all. Why? because invoking System.exit() kills your JVM, invoking this from Tomcat or Jetty, volition non exclusively kill your application simply most probable server itself. This tin mail away move potentially dangerous, if that server also host other critical application, which is non uncommon at all. As per my experience, System.exit() calls are quite mutual inwards overly wide try-catch blocks inwards spider web application start-up code that loads environment variables, properties files, connect to MQ Series, establishes database connection, opens socket connections, etc. This is however ok, if y'all are writing heart too soul Java based server, where each application has their ain JVM, simply amongst spider web application deployed on Tomcat, JBoss, WebSphere, Weblogic or whatsoever other application server, using System.exit() is big mistake. In worst instance tin mail away outcome inwards outage for lots of other critical application. On the other hand, at that spot are ways to preclude your spider web application from mortal else’s mistake, yesteryear enabling Security Manager. System.exit() too Runtime.exit() both goes through the security manager. Enabling Security director volition grab these calls too cut them into an exception rather than shutting downward the whole VM. It's non hard to enable the security director inwards most application servers, Tomcat, JBoss both has documented steps to enable security Manager.
What does this all mean? Do System.exit() has whatsoever legitimate use? Of course of written report at that spot are many cases where utilisation of System.exit is imperative. Sometime y'all actually desire to closed your JVM in 1 lawsuit done, specially if it spawned from scheduling software similar Control-M or Autosys. For instance ascendency draw of piece of employment Java applications too scheduled tasks, tin mail away utilisation System.exit(). Ideally, if y'all are certain that calling System.exit() volition non receive got whatsoever side effect, y'all tin mail away utilisation it. Here is a sample utilisation of system.exit() inwards heart too soul Java application :
Remember, if y'all are using Java plan every bit ascendency draw of piece of employment tool therefore y'all tin mail away supply System.exit(0) or System.exit(1) every bit whether it succeeded or failed. Influenza A virus subtype H5N1 non-zero instruct out condition code, normally indicates abnormal termination. You tin mail away supply dissimilar error codes to cry for dissimilar errors e.g. bad arguments, can't discovery file, could non connect to database etc. I promise y'all notice departure betwixt using System.exit(1) too letting the Java plan consummate successfully. When coffee platform volition terminate amongst System.exit() it volition do therefore amongst a non-zero condition (as long every bit the top dog thread ends too at that spot are no running daemon threads).
That’s all close Why y'all should non utilisation System.exit() within Java application. It tin mail away move unsafe too potentially shutdown whole server, which is hosting other critical Java application. For instance if y'all receive got to a greater extent than than 1 application inwards tomcat too 1 of them telephone band System.exit() therefore whole tomcat volition move shutdown too all other spider web application volition also move shutdown. As a security internet or Insurance, y'all must enable Security Manager of your Server. It catches a lot of security problems! Including intentional too unintentional telephone band to System.exit() or Runtime.exit(), turning it into an exception rather than shutting downward the whole JVM. You are relatively prophylactic to utilisation System.exit() inwards ascendency draw of piece of employment Java application, every bit they run on their ain JVM too tin mail away move used to signal a success too failure yesteryear returning dissimilar condition code inwards instance of failure.
Further Learning
Spring Framework 5: Beginner to Guru
Java Web Fundamentals By Kevin Jones
JSP, Servlets too JDBC for Beginners: Build a Database App
Why y'all should non utilisation System.exit() inwards Java Web application
I recollect it's good known for senior Java developer that System.exit() shouldn't move used except inwards e.g. command-line tools; Many beginner Java programmers, though familiar amongst System.exit(), may non know that using them inwards a Servlet/JSP code tin mail away outcome inwards shutdown of server itself, therefore if y'all receive got of whatsoever of them inwards your squad them receive got or therefore fourth dimension to educate them close coding inwards Java spider web application. In fact coding a Java based spider web application is totally dissimilar therefore coding a heart too soul Java application e.g. heart too soul concerns similar threading, object pooling, parsing are all done yesteryear Web server too it's prohibited for application code to do threads. In fact utilisation of ThreadLocal variable tin mail away do retentiveness leak inwards Java web-app, therefore coding inwards web-app require to a greater extent than caution than inwards heart too soul Java application. By the agency at that spot are other reasons, why using System.exit is absolutely terrible. Especially when y'all dealing amongst un-managed resources, if y'all don't unloose resources properly too promise that OS volition do the clean-up for you, therefore it could Pb to a temporary resources leak, until OS actually build clean materials created yesteryear your Java application.What does this all mean? Do System.exit() has whatsoever legitimate use? Of course of written report at that spot are many cases where utilisation of System.exit is imperative. Sometime y'all actually desire to closed your JVM in 1 lawsuit done, specially if it spawned from scheduling software similar Control-M or Autosys. For instance ascendency draw of piece of employment Java applications too scheduled tasks, tin mail away utilisation System.exit(). Ideally, if y'all are certain that calling System.exit() volition non receive got whatsoever side effect, y'all tin mail away utilisation it. Here is a sample utilisation of system.exit() inwards heart too soul Java application :
public static void main( String[] args ){ try { processFxRatesFileAndPutIntoDatabase(); } catch ( Exception e ) { logError(e); // if y'all demand to instruct out amongst a failed condition therefore System.exit(statusCode) is fine here. // otherwise plan volition consummate successfully, in 1 lawsuit it supply from main() method } }
Remember, if y'all are using Java plan every bit ascendency draw of piece of employment tool therefore y'all tin mail away supply System.exit(0) or System.exit(1) every bit whether it succeeded or failed. Influenza A virus subtype H5N1 non-zero instruct out condition code, normally indicates abnormal termination. You tin mail away supply dissimilar error codes to cry for dissimilar errors e.g. bad arguments, can't discovery file, could non connect to database etc. I promise y'all notice departure betwixt using System.exit(1) too letting the Java plan consummate successfully. When coffee platform volition terminate amongst System.exit() it volition do therefore amongst a non-zero condition (as long every bit the top dog thread ends too at that spot are no running daemon threads).
That’s all close Why y'all should non utilisation System.exit() within Java application. It tin mail away move unsafe too potentially shutdown whole server, which is hosting other critical Java application. For instance if y'all receive got to a greater extent than than 1 application inwards tomcat too 1 of them telephone band System.exit() therefore whole tomcat volition move shutdown too all other spider web application volition also move shutdown. As a security internet or Insurance, y'all must enable Security Manager of your Server. It catches a lot of security problems! Including intentional too unintentional telephone band to System.exit() or Runtime.exit(), turning it into an exception rather than shutting downward the whole JVM. You are relatively prophylactic to utilisation System.exit() inwards ascendency draw of piece of employment Java application, every bit they run on their ain JVM too tin mail away move used to signal a success too failure yesteryear returning dissimilar condition code inwards instance of failure.
Further Learning
Spring Framework 5: Beginner to Guru
Java Web Fundamentals By Kevin Jones
JSP, Servlets too JDBC for Beginners: Build a Database App

Komentar
Posting Komentar