<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jimmy&#039;s Blog &#187; shutdownhook</title>
	<atom:link href="http://jimmod.com/blog/tag/shutdownhook/feed/" rel="self" type="application/rss+xml" />
	<link>http://jimmod.com/blog</link>
	<description>My name is Jimmy, and this is my story... ;)</description>
	<lastBuildDate>Fri, 14 Oct 2011 17:26:09 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Application &#8211; Make sure only single/one instance running &#8211; with File Lock and ShutdownHook</title>
		<link>http://jimmod.com/blog/2008/07/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/</link>
		<comments>http://jimmod.com/blog/2008/07/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 09:51:14 +0000</pubDate>
		<dc:creator>jimmy</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[file lock]]></category>
		<category><![CDATA[instance]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[one]]></category>
		<category><![CDATA[shutdownhook]]></category>
		<category><![CDATA[single]]></category>

		<guid isPermaLink="false">http://jimlife.wordpress.com/2008/07/21/java-application-make-sure-only-singleone-instance-running-with-file-lock-shutdownhook/</guid>
		<description><![CDATA[ 
				
			 
				
			 
				Tweet 
			
		Often we want to make sure that only 1 instance our application running.
Because something terrible could happen when more than 1 instance running (for example the whole server would exploded and that would make you fired   )
Nevertheless what the reason, one of the way to make this happen is [...]


Related posts:<ol><li><a href='http://jimmod.com/blog/2011/02/jimmys-blog-send-receiving-on-specific-port-with-j2me-application/' rel='bookmark' title='Permanent Link: Jimmy&#8217;s Blog &#8211; Send &#038; Receiving SMS on specific Port with J2ME Application'>Jimmy&#8217;s Blog &#8211; Send &#038; Receiving SMS on specific Port with J2ME Application</a></li><li><a href='http://jimmod.com/blog/2007/11/simple-log4j-implementation/' rel='bookmark' title='Permanent Link: Simple Log4J implementation'>Simple Log4J implementation</a></li><li><a href='http://jimmod.com/blog/2007/11/change-junit-run-file-main-class-working-directory-at-netbeans-55-web-project/' rel='bookmark' title='Permanent Link: Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project'>Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project</a></li></ol>]]></description>
			<content:encoded><![CDATA[<div style="height:33px; padding-top:2px; padding-bottom:2px; clear:both;" class="really_simple_share"><div style="float:left; width:100px; " class="really_simple_share_facebook_like"> 
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjimmod.com%2Fblog%2F2008%2F07%2Fjava-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;height=27" 
					scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe>
			</div><div style="float:left; width:90px; padding-left:10px;" class="really_simple_share_google1"> 
				<g:plusone size="medium" href="http://jimmod.com/blog/2008/07/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/" ></g:plusone>
			</div><div style="float:left; width:110px; padding-left:10px;" class="really_simple_share_twitter"> 
				<a href="http://twitter.com/share" class="twitter-share-button" data-count="none" 
					data-text="Java Application &#8211; Make sure only single/one instance running &#8211; with File Lock and ShutdownHook" data-url="http://jimmod.com/blog/2008/07/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/">Tweet</a> 
			</div></div>
		<div style="clear:both;"></div><p>Often we want to make sure that only 1 instance our application running.<br />
Because something terrible could happen when more than 1 instance running (for example the whole server would exploded and that would make you fired <img src='http://jimmod.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )</p>
<p>Nevertheless what the reason, one of the way to make this happen is by creating a lock file as a sign that an instance is currently running.<br />
So you application will have this flow:<span id="more-60"></span></p>
<ol>
<li>Check if the lock file exists.</li>
<li>Try to delete the lock file to check if there&#8217;s really a running process lock the file.</li>
<li>Get the file lock.</li>
<li>If failed to get the lock file, show error that there&#8217;s already an instance running</li>
<li>If successfully get the lock then go on with your application want to do.</li>
<li>When application ended/closed release the lock and delete the file lock.</li>
</ol>
<p><em> </em></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">test.jimmy.filelock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.File</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.RandomAccessFile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.nio.channels.FileChannel</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.nio.channels.FileLock</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyApp <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">File</span> f<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> FileChannel channel<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> FileLock lock<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    f <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;RingOnRequest.lock&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// Check if the lock exist</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// if exist try to delete it</span>
    f.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// Try to get the lock</span>
  channel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RandomAccessFile</span><span style="color: #009900;">&#40;</span>f, <span style="color: #0000ff;">&quot;rw&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getChannel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  lock <span style="color: #339933;">=</span> channel.<span style="color: #006633;">tryLock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lock <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// File is lock by other application</span>
    channel.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Only 1 instance of MyApp can run.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">// Add shutdown hook to release lock when application shutdown</span>
  ShutdownHook shutdownHook <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ShutdownHook<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">Runtime</span>.<span style="color: #006633;">getRuntime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">addShutdownHook</span><span style="color: #009900;">&#40;</span>shutdownHook<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">//Your application tasks here..</span>
  <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Running&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">Thread</span>.<span style="color: #006633;">sleep</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InterruptedException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not start process.&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> unlockFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// release and delete file lock</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lock <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    lock.<span style="color: #006633;">release</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    channel.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    f.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> ShutdownHook <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    unlockFile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Technorati Tags: <a class="performancingtags" rel="tag" href="http://technorati.com/tag/java">java</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/file%20lock">file lock</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/shutdownhook">shutdownhook</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/lock">lock</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/single">single</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/one">one</a>, <a class="performancingtags" rel="tag" href="http://technorati.com/tag/intance">intance</a></p>


<p>Related posts:<ol><li><a href='http://jimmod.com/blog/2011/02/jimmys-blog-send-receiving-on-specific-port-with-j2me-application/' rel='bookmark' title='Permanent Link: Jimmy&#8217;s Blog &#8211; Send &#038; Receiving SMS on specific Port with J2ME Application'>Jimmy&#8217;s Blog &#8211; Send &#038; Receiving SMS on specific Port with J2ME Application</a></li><li><a href='http://jimmod.com/blog/2007/11/simple-log4j-implementation/' rel='bookmark' title='Permanent Link: Simple Log4J implementation'>Simple Log4J implementation</a></li><li><a href='http://jimmod.com/blog/2007/11/change-junit-run-file-main-class-working-directory-at-netbeans-55-web-project/' rel='bookmark' title='Permanent Link: Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project'>Change junit &amp; run file (main class) working directory at Netbeans 5.5 Web Project</a></li></ol></p>]]></content:encoded>
			<wfw:commentRss>http://jimmod.com/blog/2008/07/java-application-make-sure-only-singleone-instance-running-with-file-lock-ampampampampamp-shutdownhook/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

