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

I receive got of late come upward across a code snippet, where programmer was using System.exit() if application failed to larn necessary resources afterwards pair of retry. His reasoning was that since, application cannot function, if essential resources similar database is non available or at that topographic point is no disk infinite to write records inwards File system. Ok, I listen you; only System.exit() inwards Java Web application, which runs within either spider web server or application server, which itself is Java programme is non a expert visit at all. Why? because invoking System.exit() kills your JVM, invoking this from Tomcat or Jetty, volition non alone kill your application only most probable server itself. This tin sack live on 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 even therefore ok, if you lot are writing nub Java based server, where each application has their ain JVM, only amongst spider web application deployed on Tomcat, JBoss, WebSphere, Weblogic or whatsoever other application server, using System.exit() is big mistake. In worst illustration tin sack final result inwards outage for lots of other critical application. On the other hand, at that topographic point are ways to preclude your spider web application from somebody else’s mistake, past times enabling Security Manager. System.exit() as well as Runtime.exit() both goes through the security manager. Enabling Security managing director volition choose grip of these calls as well as cut them into an exception rather than shutting downwardly 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 occupation System.exit() inwards Java Web application

I recollect it's good known for senior Java developer that System.exit() shouldn't live on 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 sack final result inwards shutdown of server itself, therefore if you lot receive got of whatsoever of them inwards your squad them receive got around fourth dimension to educate them most coding inwards Java spider web application. In fact coding a Java based spider web application is totally unlike therefore coding a nub Java application e.g. nub concerns similar threading, object pooling, parsing are all done past times Web server as well as it's prohibited for application code to practise threads. In fact occupation of ThreadLocal variable tin sack practise retentiveness leak inwards Java web-app, therefore coding inwards web-app require to a greater extent than caution than inwards nub Java application. By the agency at that topographic point 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 as well as promise that OS volition practise the clean-up for you, therefore it could Pb to a temporary resources leak, until OS actually construct clean materials created past times your Java application.

 I receive got of late come upward across a code snippet Don't occupation System.exit() on Java Web Application


What does this all mean? Do System.exit() has whatsoever legitimate use? Of course of written report at that topographic point are many cases where occupation of System.exit is imperative. Sometime you lot actually desire to unopen your JVM in 1 lawsuit done, peculiarly if it spawned from scheduling software similar Control-M or Autosys. For illustration ascendance occupation Java applications as well as scheduled tasks, tin sack occupation System.exit(). Ideally, if you lot are certain that calling System.exit() volition non receive got whatsoever side effect, you lot tin sack occupation it. Here is a sample occupation of system.exit() inwards nub 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 therefore System.exit(statusCode) is fine here.         // otherwise programme volition consummate successfully, in 1 lawsuit it provide from main() method     }   }

Remember, if you lot are using Java programme equally ascendance occupation tool therefore you lot tin sack provide System.exit(0) or System.exit(1) equally whether it succeeded or failed. H5N1 non-zero larn out condition code, ordinarily indicates abnormal termination. You tin sack provide unlike error codes to signal unlike errors e.g. bad arguments, can't discovery file, could non connect to database etc. I promise you lot notice departure betwixt using System.exit(1) as well as 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 equally the primary thread ends as well as at that topographic point are no running daemon threads).


That’s all most Why you lot should non occupation System.exit() within Java application. It tin sack live on unsafe as well as potentially shutdown whole server, which is hosting other critical Java application. For illustration if you lot receive got to a greater extent than than 1 application inwards tomcat as well as 1 of them telephone telephone System.exit() therefore whole tomcat volition live on shutdown as well as all other spider web application volition also live on shutdown. As a security cyberspace or Insurance, you lot must enable Security Manager of your Server. It catches a lot of security problems! Including intentional as well as unintentional telephone telephone to System.exit() or Runtime.exit(), turning it into an exception rather than shutting downwardly the whole JVM. You are relatively rubber to occupation System.exit() inwards ascendance occupation Java application, equally they run on their ain JVM as well as tin sack live on used to signal a success as well as failure past times returning unlike condition code inwards illustration of failure.

Further Learning
Spring Framework 5: Beginner to Guru
Java Web Fundamentals By Kevin Jones
JSP, Servlets as well as JDBC for Beginners: Build a Database App


Belum ada Komentar untuk "Don't Role System.Exit() On Coffee Spider Web Application"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel