Don't Run System.Exit() On Coffee Spider Web Application

I lead maintain of late come upward across a code snippet, where programmer was using System.exit() if application failed to larn necessary resources later distich of retry. His reasoning was that since, application cannot function, if essential resources similar database is non available or in that place 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 programme is non a proficient persuasion at all. Why? because invoking System.exit() kills your JVM, invoking this from Tomcat or Jetty, volition non alone kill your application simply most probable server itself. This tin locomote 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 nevertheless ok, if you lot are writing meat Java based server, where each application has their ain JVM, simply amongst spider web application deployed on Tomcat, JBoss, WebSphere, Weblogic or whatever other application server, using System.exit() is big mistake. In worst instance tin termination inwards outage for lots of other critical application. On the other hand, in that place are ways to foreclose your spider web application from mortal else’s mistake, yesteryear enabling Security Manager. System.exit() in addition to Runtime.exit() both goes through the security manager. Enabling Security managing director volition grab these calls in addition to trim them into an exception rather than shutting downward the whole VM. It's non hard to enable the security managing director inwards most application servers, Tomcat, JBoss both has documented steps to enable security Manager.




Why you lot should non utilization System.exit() inwards Java Web application

I recollect it's good known for senior Java developer that System.exit() shouldn't locomote 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 termination inwards shutdown of server itself, hence if you lot lead maintain of whatever of them inwards your squad them lead maintain to a greater extent than or less fourth dimension to educate them well-nigh coding inwards Java spider web application. In fact coding a Java based spider web application is totally dissimilar in addition to hence coding a meat Java application e.g. meat concerns similar threading, object pooling, parsing are all done yesteryear Web server in addition to it's prohibited for application code to exercise threads. In fact utilization of ThreadLocal variable tin exercise retention leak inwards Java web-app, hence coding inwards web-app require to a greater extent than caution than inwards meat Java application. By the agency in that place are other reasons, why using System.exit is absolutely terrible. Especially when you lot dealing amongst un-managed resources, if you lot don't unloose resources properly in addition to promise that OS volition exercise the clean-up for you, in addition to hence it could atomic number 82 to a temporary resources leak, until OS actually build clean materials created yesteryear your Java application.

 I lead maintain of late come upward across a code snippet Don't utilization System.exit() on Java Web Application


What does this all mean? Do System.exit() has whatever legitimate use? Of course of pedagogy in that place are many cases where utilization of System.exit is imperative. Sometime you lot actually desire to unopen your JVM 1 time done, peculiarly if it spawned from scheduling software similar Control-M or Autosys. For instance ascendence employment Java applications in addition to scheduled tasks, tin utilization System.exit(). Ideally, if you lot are certain that calling System.exit() volition non lead maintain whatever side effect, you lot tin utilization it. Here is a sample utilization of system.exit() inwards meat Java application :

public static void main( String[] args ){      try     {         processFxRatesFileAndPutIntoDatabase();      }  catch ( Exception e )     {         logError(e);         // if you lot demand to larn out amongst a failed condition in addition to hence System.exit(statusCode) is fine here.         // otherwise programme volition consummate successfully, 1 time it furnish from main() method     }   }

Remember, if you lot are using Java programme equally ascendence employment tool in addition to hence you lot tin furnish System.exit(0) or System.exit(1) equally whether it succeeded or failed. H5N1 non-zero larn out condition code, normally indicates abnormal termination. You tin furnish dissimilar error codes to dot dissimilar errors e.g. bad arguments, can't respect file, could non connect to database etc. I promise you lot notice divergence betwixt using System.exit(1) in addition to letting the Java programme consummate successfully. When coffee platform volition terminate amongst System.exit() it volition exercise hence amongst a non-zero condition (as long equally the primary thread ends in addition to in that place are no running daemon threads).


That’s all well-nigh Why you lot should non utilization System.exit() within Java application. It tin locomote unsafe in addition to potentially shutdown whole server, which is hosting other critical Java application. For instance if you lot lead maintain to a greater extent than than 1 application inwards tomcat in addition to 1 of them telephone telephone System.exit() in addition to hence whole tomcat volition locomote shutdown in addition to all other spider web application volition also locomote shutdown. As a security internet or Insurance, you lot must enable Security Manager of your Server. It catches a lot of security problems! Including intentional in addition to unintentional telephone telephone to System.exit() or Runtime.exit(), turning it into an exception rather than shutting downward the whole JVM. You are relatively rubber to utilization System.exit() inwards ascendence employment Java application, equally they run on their ain JVM in addition to tin locomote used to signal a success in addition to 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 in addition to JDBC for Beginners: Build a Database App



Sumber https://javarevisited.blogspot.com/

0 Response to "Don't Run System.Exit() On Coffee Spider Web Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel