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

Advertisement

Masukkan script iklan 970x90px

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

Rabu, 01 Juli 2020

I receive got lately come upwards across a code snippet, where programmer was using System.exit() if application failed to instruct necessary resources afterwards duo 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; exactly System.exit() inwards Java Web application, which runs within either spider web server or application server, which itself is Java programme is non a skillful stance at all. Why? because invoking System.exit() kills your JVM, invoking this from Tomcat or Jetty, volition non alone kill your application exactly most probable server itself. This tin last 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 nonetheless ok, if you lot are writing marrow Java based server, where each application has their ain JVM, exactly 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 resultant inwards outage for lots of other critical application. On the other hand, at that spot are ways to foreclose your spider web application from mortal else’s mistake, past times 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.




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

I retrieve it's good known for senior Java developer that System.exit() shouldn't last 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 resultant inwards shutdown of server itself, therefore if you lot receive got of whatever of them inwards your squad them receive got unopen to fourth dimension to educate them nearly coding inwards Java spider web application. In fact coding a Java based spider web application is totally dissimilar too therefore coding a marrow Java application e.g. marrow concerns similar threading, object pooling, parsing are all done past times Web server too it's prohibited for application code to practise threads. In fact utilization of ThreadLocal variable tin practise retentivity leak inwards Java web-app, therefore coding inwards web-app require to a greater extent than caution than inwards marrow Java application. By the means at that spot 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 too promise that OS volition practise the clean-up for you, too therefore it could Pb to a temporary resources leak, until OS actually build clean materials created past times your Java application.

 I receive got lately come upwards 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 education at that spot are many cases where utilization of System.exit is imperative. Sometime you lot actually desire to unopen your JVM in i lawsuit done, peculiarly if it spawned from scheduling software similar Control-M or Autosys. For illustration dominance draw of piece of occupation Java applications too scheduled tasks, tin utilization System.exit(). Ideally, if you lot are certain that calling System.exit() volition non receive got whatever side effect, you lot tin utilization it. Here is a sample utilization of system.exit() inwards marrow Java application :

public static void main( String[] args ){      try     {         processFxRatesFileAndPutIntoDatabase();      }  catch ( Exception e )     {         logError(e);         // if you lot demand to travel out amongst a failed condition too therefore System.exit(statusCode) is fine here.         // otherwise programme volition consummate successfully, in i lawsuit it provide from main() method     }   }

Remember, if you lot are using Java programme every bit dominance draw of piece of occupation tool too therefore you lot tin provide System.exit(0) or System.exit(1) every bit whether it succeeded or failed. Influenza A virus subtype H5N1 non-zero travel out condition code, commonly indicates abnormal termination. You tin provide 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 deviation betwixt using System.exit(1) too letting the Java programme consummate successfully. When coffee platform volition terminate amongst System.exit() it volition practise therefore amongst a non-zero condition (as long every bit the primary thread ends too at that spot are no running daemon threads).


That’s all nearly Why you lot should non utilization System.exit() within Java application. It tin last unsafe too potentially shutdown whole server, which is hosting other critical Java application. For illustration if you lot receive got to a greater extent than than i application inwards tomcat too i of them telephone cry upwards System.exit() too therefore whole tomcat volition last shutdown too all other spider web application volition also last 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 too unintentional telephone cry upwards to System.exit() or Runtime.exit(), turning it into an exception rather than shutting downward the whole JVM. You are relatively prophylactic to utilization System.exit() inwards dominance draw of piece of occupation Java application, every bit they run on their ain JVM too tin last used to signal a success too failure past times 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