<?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>Solution Hacker &#187; Java</title>
	<atom:link href="http://www.solutionhacker.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.solutionhacker.com</link>
	<description>This blog provides solutions for enterpreneurs!</description>
	<lastBuildDate>Wed, 04 Aug 2010 11:37:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Java 5 Features &#8211; Enum and Annotation</title>
		<link>http://www.solutionhacker.com/implement-your-idea/architect-corner/java-5-features-enum-and-annotation/</link>
		<comments>http://www.solutionhacker.com/implement-your-idea/architect-corner/java-5-features-enum-and-annotation/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 19:01:23 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[2.1. Architect Corner]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[enum]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[tiger]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=244</guid>
		<description><![CDATA[Intent I want to summarize some new and interesting Java 5 features in this article and how they change the way I code. Enum I use int constants to make my life easier b/c it can avoid typo. However, it has several drawbacks: Java doesn&#8217;t provide namespace for int enum groups. I can either prefix [...]]]></description>
			<content:encoded><![CDATA[<h2>Intent</h2>
<p>I want to summarize some new and interesting Java 5 features in this article and how they change the way I code.</p>
<h2>Enum</h2>
<p>I use int constants to make my life easier b/c it can avoid typo. However, it has several drawbacks:</p>
<ol>
<li>Java doesn&#8217;t provide namespace for int enum groups. I can either prefix my constant like ABC_ or using inner interfaces to organize it.</li>
<li>It is compile-time constants. So you need to recompile once changed.</li>
<li>No easy way to translate int enum constants into printable string during debugging.</li>
<li>You cannot iterate over all the int enum easily.</li>
<li>You need a way to validate the enum is an valid int</li>
</ol>
<p>Use new enum type in Java 5:</p>
<blockquote>
<p>public enum Apple {FUJI, PIPPIN, GRANNY_SMITH}</p>
</blockquote>
<p>Enum is full-fledged final class that export one instance for each enumeration constant via a public static final field.</p>
<ol>
<li>Namespace is provided via the enum type name.</li>
<li>You can reorder and add the enumeration constant without recompiling its client.</li>
<li>You can translate enum into printable strings via toString() method.</li>
<li>Enum type provides you values() method to iterate your enumeration constants (based on declaration order).</li>
<li>Type-checking can be used for the validation check</li>
<li>You can associate data with enum constant</li>
<li>Enum is immutable, serializable and comparable.</li>
</ol>
<h2>EnumSet</h2>
<p>If elements of an enumerated types are used primarily in sets, it is traditional to use the int enum pattern, assigning a different power of 2 to each constant like READ = 1 &lt;&lt; 2, WRITE = 1 &lt;&lt;1, EXECUTE = 1 &lt;&lt; 0 to represent permissions per each entity in Unix. This representation lets you use the bitwise OR operation to combine several constants into a set, known as a bit field. The bit field representation also lets you perform set operations such as union and intersection efficiently using bitwise arithmetic. But bit fields have all the disadvantages of int enum mentioned above.</p>
<p>Now, java.util package provides the EnumSet to efficient represent sets of value drawn from single enum type. This class implements Set interface and internally use bit vector to represent set of values. For example, if you enum types has 64 values, the entire EnumSet can be represented as a single long, so its performance is comparable to the bit field.</p>
<p>The EnumSet class provides three benefits a normal set does not:</p>
<ol>
<li>Various creation methods that simplify the construction of a set based on an Enumeration</li>
<li>Guaranteed ordering of the elements in the set based on their order in the enumeration constants are declared</li>
<li>Performance and memory benefits not nearly possible with a regular set implementation</li>
</ol>
<h2>Annotation</h2>
<p>An annotation is a new language feature introduced in J2SE 5.0. Simply put, annotations allow developers to mark classes,                         methods, and members with secondary information that is not part of the operating code.You can see annotation is a way to extend Java language.</p>
<p>Before annotation from Java 5, you may use naming patterns to indicate that some program elements like method demanded special treatment by a tool or a framework. Like JUnit required its users to name the test methods with the pattern like testXXX(). It works but with some big disadvantages:</p>
<ol>
<li>Typo problem</li>
<li>It doesn&#8217;t provide a way to associate parameter values with program elements.</li>
</ol>
<p>Annotation can solve this problem. To use it, you can:</p>
<ol>
<li>Create you own marker annotation (<strong>@interface</strong> is the keyword) or parametized annotation. You can annotate the annotation (ie. meta-annotation). Example: @Retention and @Target. And marker annotation has no parameter associated with it.</li>
<li>Annotate the program elements</li>
<li>Write processor to handle your annotated code. Generally, annotations never change the semantics of the annotated code, but enable it for special treatment by tools. Now, the metadata of Method carries additional info for your job. You can use <strong>Method&#8217;s isAnnotationPresent() </strong>to check if a method is annotated by certain annotation type. If you annotation carried parameter, you can use<strong> Method&#8217;s getAnnotation()</strong> to get the Annotaton object and use <strong>value()</strong> to obtain the parameter.</li>
</ol>
<h2>Reference</h2>
<p>Below are some related articles I feel useful:</p>
<ol>
<li><a class="linkification-ext" href="http://www.javalobby.org/java/forums/t16967.html" title="Linkification: http://www.javalobby.org/java/forums/t16967.html">http://www.javalobby.org/java/forums/t16967.html</a></li>
<li><a href="http://www.ibm.com/developerworks/library/j-annotate1/">Annotation in Tiger &#8211; Part 1 Meta-Annotation</a></li>
<li><a href="http://www.ibm.com/developerworks/library/j-annotate2.html">Annotation in Tiger &#8211; Part 2 Custom Annotation</a></li>
</ol>
<p>&#160;</p>
<p>&#160;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/implement-your-idea/architect-corner/java-5-features-enum-and-annotation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Concurrent Programming &#8211; Part 1 Synchronization</title>
		<link>http://www.solutionhacker.com/implement-your-idea/architect-corner/concurrent-programming-part-1-synchronization/</link>
		<comments>http://www.solutionhacker.com/implement-your-idea/architect-corner/concurrent-programming-part-1-synchronization/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 22:43:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[2.1. Architect Corner]]></category>
		<category><![CDATA[4.1. Store Your Data]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[data race]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java Memory Model]]></category>
		<category><![CDATA[JSR-166]]></category>
		<category><![CDATA[locking]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[synchronization]]></category>
		<category><![CDATA[Thread-safe]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=157</guid>
		<description><![CDATA[Get yourself familiar with concurrency programming When I interview my candidates, I like to ask questions related to multi-threading. I found out that it is a good topic to differentiate out a hardcore programmer from application-oriented programmer. I am not saying I am looking for someone who could write the concurrency library as efficient as [...]]]></description>
			<content:encoded><![CDATA[<h2>Get yourself familiar with concurrency programming</h2>
<p>When I interview my candidates, I like to ask questions related to <strong>multi-threading</strong>. I found out that it is a good topic to differentiate out a hardcore programmer from <strong>application-oriented programmer.</strong> I am not saying I am looking for someone who could write the concurrency library as efficient as the one created by Doug Lea. In fact, I am looking for candidates who has solid understanding of this topic. However, I found out that most candidates have little knowledge in this area apart from the meaning of &#8220;<strong>synchronized</strong>&#8221; keyword in Java syntax. Therefore I decide to write a series of articles to cover some areas of multi-threading that I feel important to understand. Of course, I would start from the basic first.</p>
<p><span id="more-157"></span></p>
<h2>Introduction of Synchronization</h2>
<p><strong>Synchronization is a way to lock an object, so no 2 threads possibly running on the same code at the same time.</strong></p>
<pre name="code" class="java">
public class SynchronizedCounter {
    private double c = 0;

    public synchronized void increment() {
        c++;
    }

    public synchronized void decrement() {
        c--;
    }

    public synchronized int value() {
        return c;
    }

    public void method2() {
        ...
    }
}</pre>
<p>If count is an instance of SynchronizedCounter, then making these methods synchronized has two effects:</p>
<ul>
<li><u><strong>Mutual Exclusion</strong></u> &#8211; It is not possible for two invocations of synchronized     methods on the same object to interleave. When one thread is     executing a synchronized method for an object, all other threads     that invoke synchronized methods for the same object <strong>block</strong>     (suspend execution) until the first thread is done with the     object. <span style="color: rgb(255, 0, 0);">Remember this rule doesn&#8217;t apply to non-synchronized methods.</span> And the thread holds the lock of the object can reenter its synchronized methods (ie. <strong>reentrance</strong>).</li>
<li><u><strong>Memory-Visibility</strong></u> &#8211; When a synchronized method exits, it automatically     establishes a <strong>happens-before relationship</strong> with <i>any subsequent         invocation</i> of a synchronized method for the same object.     This guarantees that changes to the state of the object are     <strong>visible </strong>to all threads. Most of the interviewers miss this one. <img src="../../../../../wp-includes/images/smilies/icon_smile.gif" alt=":smile:" onclick="grin(':smile:');" /> <em>In database, it is like the concept of &#8220;commit&#8221;. If you don&#8217;t commit your changes, others could not see your changes.</em></li>
</ul>
<p>All in all, synchronized methods enable a simple strategy for preventing <strong>thread interference</strong> and <strong>memory consistency</strong> <strong>errors</strong>.  <strong>Interference </strong>happens when two operations, running in different threads, but acting on the same data, <i>interleave</i>. This means that the two operations consist of multiple steps, and the sequences of steps overlap. This will result in unpredictable data lost (hard to fix). Memory consistency error occurs when complier and processor <strong>reorder </strong>statements and uses the <strong>cached </strong>value for better performance</p>
<h2>Problem of Synchronization</h2>
<p>Synchronization will serialize the method calls from different threads. At any given time, only one thread can execute the synchronized method and the other threads need to wait until the object lock releases. This will dramatically diminish the liveness of your application. To minimize the impact, you should:</p>
<ol>
<li><strong>Reduce lock duration &#8211; Synchronized statements</strong> are useful for improving concurrency with fine-grained synchronization</li>
<li><strong>Reduce lock scope &#8211; Mutex variable </strong>in the synchronized lock may help you to avoid locking the whole object. In Java 5 concurrency library, there is class called <strong>ReentrantLock </strong>that provides the same features as synchronized with better performance and flexibility. Here is what is stated in &#8220;Java Concurrency in Practice&#8221;:</li>
</ol>
<blockquote>
<p>Why create a new locking mechanism (ie. ReentrantLock) that is so similar to intrinsic locking (ie. synchronized)? Intrinsic locking works fine in most situations but has some functional limitations. It is not possible to interrupt a thread waiting to acquire a lock, or to attempt to acquire a lock without being willing to wait for it forever. Intrinsic locks also must be released in the same block of code in which they are acquired; this simplifies coding and interacts nicely with exception handling, but makes non-block structured locking disciplines impossible. None of these are reasons to abandon <tt>synchronized</tt>, but in some cases a more flexible locking mechanism offers better liveness or performance.</p>
</blockquote>
<p>So far so good.? Great! lets me ask you 3 questions:</p>
<ol>
<li><u>Question 1:</u> In the example above, if thread A is executing a synchronized method &#8220;increment&#8221;, can another thread execute method2? <span style="color: rgb(0, 0, 255);"><em>Yes. Because method2 is not synchronized</em></span></li>
<li><u>Question 2:</u> If thread A is in the synchronized method xyz , can it invoke another synchronized abc? <span style="color: rgb(0, 0, 255);"><em>Yes. The object lock is reentrant.</em></span></li>
<li><u>Question 3: </u>If I want to make the above class thread-safe without using synchronized object lock, are there any other alternatives? <span style="color: rgb(0, 0, 255);"><em>Yes.</em></span>
<ul>
<li>ReentrantLock &#8211; <a href="http://www.ibm.com/developerworks/java/library/j-jtp10264/">http://www.ibm.com/developerworks/java/library/j-jtp10264/</a></li>
<li>Atomic variable &#8211; <a href="http://www.ibm.com/developerworks/java/library/j-jtp11234/">http://www.ibm.com/developerworks/java/library/j-jtp11234/</a></li>
</ul>
</li>
<li><u>Question 4: </u>How about declare the variable volatile?
<ul>
<li><a href="http://www.ibm.com/developerworks/java/library/j-jtp06197.html">Managing Volatility by Brian Goetz</a></li>
<li><a href="http://www.javalobby.org/java/forums/m91839242.html">Compare Atomic variable, ReentrantLock and Volatile variable.</a></li>
<li>Use int, byte instead of long or double because updating int or byte is an <strong>atomic action</strong>.  Atomic actions cannot be interleaved, so they can be used without fear of thread interference. However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible (for example, thread A updated an variable atomically but it hasn&#8217;t flushed and sync up the main memory, so it is not visible to other threads). Unless the fields in question are declared <code>volatile, </code> the JMM does not require the underlying platform to provide cache coherency or sequential consistency across processors, so it is possible, on some platforms, to read stale data in the absence of synchronization. Look at <a href="http://www.javaperformancetuning.com/news/qotm030.shtml">here </a>for better explanation. However, <strong>volatile can only guarantee atomicity and memory consistency for single variable</strong>. If you want to guarantee that for compound operations, you need to use synchronized block.(or the new java.util.concurrent classes).<em> It is worth pointing out that increment (i.e. ++) and similar operations are not atomic in Java. So incrementing a volatile  variable <code>volatileVar++</code> is NOT thread-safe. If you need thread-safe semantics i.e. no possibility of multiple  threads corrupting the variable value by having the updates unexpectedly interfere with each other, then you need to use  a synchronized block to increment a variable, e.g. <code>synchronized(LOCK){myVar++}</code>, regardless of the overheads this causes &#8211; <a href="http://www.javaperformancetuning.com/news/qotm051.shtml">Java Performance Tuning</a></em></li>
</ul>
</li>
</ol>
<h2>More On Thread Safety</h2>
<p>All the techniques I discussed so far is to show you how to make your code thread safe. They are applicable only if you have to share resources across multiple threads and those threads may modify the resources. That is to say, if you don&#8217;t share any resources or other threads only read but no write, your code are thread safe already. Here are some tips related to not sharing and read-only sharing:</p>
<ol>
<li>You can use <strong>local </strong>variables to carry out logic in the methods if possible (not share)</li>
<li>You can use <strong>TheadLocal </strong>to hold the resources if you want to access it across multiple methods for the same thread (not share)</li>
<li>You can use immutable object (private variable without setXXX methods) for read-only sharing. For example, String and PrimitiveWrappers like Integer. However, make sure the declare <strong>final </strong>for the reference that holds your immutable object.</li>
<li>Most of the time, you use collection classes like HashMap, ArrayList to hold our objects. Those classes are not thread safe. To make it thread safe, you may use Collections.synchronized wrappers or simply use the synchronized version of them like Hashtable and Vector. However, these approaches have 2 problems
<ul>
<li>They are not performed. Why lock every reads only to protect occasional write?</li>
<li>They are just <a href="http://www.ibm.com/developerworks/java/library/j-jtp07233.html">conditionally thread-safe</a>. All individual operations are thread-safe, but sequences of operations where the control flow depends on the results of previous operations may be subject to data races like doing containsKey(), size() and iterator() methods before actually read and write can give you <strong>NullPointerException</strong> and <strong>ConcurrentModificationException </strong>if you don&#8217;t do external synchronization.</li>
<li>Here are the <strong>unconditionally </strong><strong>thread-safe</strong> version like <strong>ConcurrentHashMap, </strong><span style="font-weight: bold;">ConcurrentLinkedQueue and </span><strong>CopyOnWriteArrayList</strong> to achieve thread-safe with good performance number.</li>
<li><em>When you write an unconditionally thread-safe class, consider using <strong>private lock object</strong> in place of synchronized methods. This protect you against synchronization interference by clients and subclasses and gives you the flexibility to adopt a more sophisticated approach to concurrent control in later release &#8211; Joshua Bloch in Effective Java 2nd version p.281.</em></li>
</ul>
</li>
<li>Deal with lazy initialization</li>
<li>Handle denial of service attack that holds the object lock forever</li>
</ol>
<h2>Java Memory Model</h2>
<p>JMM is what causes concurrent programming way more complicated than it should be. Honestly, I am not good to write this part because I cannot understand it in full. All I can do is to provide you a video from Jeremy Manson in Google. Hear what the expert said:</p>
<p><embed id="VideoPlayback" style="width: 400px; height: 326px;" allowfullscreen="true" src="http://video.google.com/googleplayer.swf?docid=8394326369005388010&amp;hl=en&amp;fs=true" type="application/x-shockwave-flash"></embed></p>
<p>&#160;If you still have questions, make sure go to his blog</p>
<p><a href="http://jeremymanson.blogspot.com/">http://jeremymanson.blogspot.com/</a></p>
<h2>Reference</h2>
<p>Below are some of the articles I use:</p>
<ol>
<li><a href="http://www.javaperformancetuning.com/news/qotm030.shtml">What does volatile do?</a></li>
<li><a href="http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html">Sun lesson on concurrency</a></li>
<li><a href="http://www.ibm.com/developerworks/library/j-jtp02244.html">Fixing Java Memory Model &#8211; Brian Goetz &#8211; Part 1,</a> <a href="http://www.ibm.com/developerworks/library/j-jtp03304/">Part 2</a></li>
<li><a href="http://rox-xmlrpc.sourceforge.net/niotut/index.html">Rox Java NIO Tutorial</a></li>
<li><a href="http://www.roseindia.net/javatutorials/blocking_queue.shtml">Blocking Queue</a></li>
<li><a href="http://gee.cs.oswego.edu/dl/cpj/jmm.html">Synchronization and Java Memory Model &#8211; Doug Lea</a>&#160;</li>
</ol>
<p>&#160;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/implement-your-idea/architect-corner/concurrent-programming-part-1-synchronization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tomcat Performance Tuning</title>
		<link>http://www.solutionhacker.com/implement-your-idea/scale-your-website/tomcat-performance-tuning/</link>
		<comments>http://www.solutionhacker.com/implement-your-idea/scale-your-website/tomcat-performance-tuning/#comments</comments>
		<pubDate>Thu, 29 May 2008 20:15:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[2.4. Scale Your Site]]></category>
		<category><![CDATA[2.6. Unleash Your System]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[connection pool]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[HotSpot]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[system]]></category>
		<category><![CDATA[Tomcat]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/?p=147</guid>
		<description><![CDATA[Most companies I have worked for use Tomcat as Servlet Container. It is de facto standard just like how Apache been used as Web Server. However, most of us just drag our war file to the webapp folder and use Tomcat with all the settings as default out of the box. It works fine in [...]]]></description>
			<content:encoded><![CDATA[<p>Most companies I have worked for use Tomcat as Servlet Container. It is de facto standard just like how Apache been used as Web Server. However, most of us just drag our war file to the webapp folder and use Tomcat with all the settings as default out of the box. It works fine in development environment but may not in production. This article will give you advice in several areas:</p>
<ol>
<li>Production Tomcat Architecture</li>
<li>Tuning tomcat for performance</li>
<li>Resolving problems which affect availability</li>
</ol>
<p><span id="more-147"></span></p>
<h2>&#160;<!--more-->Production Tomcat Architecture</h2>
<p>In production Tomcat relies on a number of resources which can impact its overall performance. Understanding the overall system architecture is key to tuning performance and troubleshooting problems.</p>
<p style="text-align: left;"><img height="220" width="471" alt="" src="http://www.solutionhacker.com/wp-content/uploads/tomcatSystemArch(1).jpg" /></p>
<ol>
<li>Hardware: CPU(s), memory, network IO and file IO</li>
<li>OS: SMP (symmetric multiprocessing) and thread support</li>
<li>JVM: version, tuning memory usage, and tuning GC</li>
<li>Tomcat: version (example, Tomcat 6 supports NIO)</li>
<li>Application: Application design can have the largest impact on overall performance</li>
<li>Database: concurrent db connection is allowed (pooling and object caching)</li>
<li>Web Server: Apache can sit in front of Tomcat and serves the static content. It also can do load balancing across multiple Tomcat instances.</li>
<li>Network: Network delays.</li>
<li>Remote Client: How fast is the communication protocol? Content can be compressed.&#160;</li>
</ol>
<h2>Performance Tuning</h2>
<p><u>How to measure and test performance</u></p>
<ul>
<li><strong>Request latency</strong> is key b/c it reflects the responsiveness of your site for visitors.</li>
<li>Test environment should match production as closely as possible.</li>
<li>The <strong>data volume </strong>is important to simulate in database side.</li>
<li>Test HTTP requests with different request parameters (test corner cases)</li>
<li>Use <strong>load test</strong> to simulate the traffics (ex. JMeter)</li>
<li>Final tests should be <strong>over longer periods like days</strong> because JVM performance changes over time and can actually improve if using HotSpot. Memory leaks, db temporary unavailable, etc can only be found when running longer tests.</li>
</ul>
<p><u>JVM version, memory usage and GC</u></p>
<ul>
<li>Sun Java 1.3 and later releases inlcude <strong>HotSpot </strong>profiling optimizer customized for long running server application.</li>
<li><img src="file:///C:/WINDOWS/TEMP/moz-screenshot-1.jpg" alt="" />Tomcat will freeze processing of all requests while the JVM is performing <a target="_blank" href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html">GC</a>. On a poorly tuned JVM this can last 10&#8242;s of seconds. Most GC&#8217;s should take &lt; 1 second and never exceed 10 seconds</li>
<li>Tune the -Xms (min) and -Xmx (max) java<strong> stack memory </strong>(set them to the same value can improve GC performance)</li>
<li>Make sure the java process always keeps the memory it uses resident in physical memory and not swapped out to virtual memory.</li>
<li>Use -Xincgc to enable<strong> incremental garbage collection</strong></li>
<li>Try reducing -Xss thread stack memory usage</li>
</ul>
<p><u>Tomcat version and configuration</u></p>
<ul>
<li>Tomcat 6 supports NIO.</li>
<li>Set &#8220;reloadable&#8221; false &#8211; remove unnecessary detection overhead</li>
<li>Set &#8220;liveDeploy&#8221; to false &#8211; liveDeploy controls whether your webapps directory is periodically checked for new war files. This is done using background thread.</li>
<li>Set &#8220;debug&#8221; to 0</li>
<li>Set &#8220;swallowOutput&#8221; to true &#8211; This makes sure all output to stdout or stderr for a web application gets directed to the web application log rather than the console or catalina.out. This make it easier to troubleshoot problems.</li>
<li>Connector configuration &#8211; minProcessor, maxProcessor, acceptCount, enableLookups. Don&#8217;t set the acceptCount too high b/c this sets the number of pending requests awaiting processing. It is better deny few requests than overload Tomcat and cause problems for all requests. Set &#8220;enableLookups&#8221; to false b/c DNS lookups can add significant delays.</li>
</ul>
<p><u>Database connection pool</u></p>
<ul>
<li>We use connection pool provided by Spring instead</li>
<li>Using middleware to persist and cache objects from your database can significantly improve performance b/c of fewer db calls, less thrashing of the JVM for creation and subsequent GC of object craeted for resultset.</li>
</ul>
<p><u>Application design and profiling</u></p>
<ul>
<li>If the data used to generate a dynamic page rarely changes, modify it to a static page which you regenerate periodically.</li>
<li>Cache dynamic page</li>
<li>Use tool like JProble to profle your web applications during development phase</li>
<li>Look for possible thread synchronization bottlenecks</li>
<li>Date and Time thread synchronization bottleneck&#160;</li>
</ul>
<h2>Troubleshooting</h2>
<p><u>Collecting and analyzing log data</u></p>
<p><u>Common problems</u></p>
<ul>
<li><strong>Broken pipe</strong> &#8211; For HTTP Connector indicates that the remote client aborted the request. For web server JK Connector indicates that the web server process or thread was terminated. These are normal and rarely due to a problem with Tomcat. However, if you have long request, the connectionTimeout may close the connection before you send your response back.</li>
<li><strong>Tomcat freezes or pauses </strong>with no request being processed &#8211; usually due to a long pause of JVM GC. A long pause can cause a cascading effect and high load once Tomcat starts handling requests again. Don&#8217;t set the &#8220;acceptCount&#8221; too high and use java -verbose:gc startup argument to collect GC data.</li>
<li><strong>Out of Memory Exception</strong> &#8211; look into application code to fix the leak (profile tool can help). Increase available memory on the system via -Xmx. Restart tomcat!</li>
<li>Database connection failure &#8211; connection used up when traffic is spike.</li>
<li><strong>Random connection close exception </strong>- when you close your connection twice. First close(), the connection returns to the pool. It may be picked up by another thread. Now, second close() may close a connection that is being used by other thread. Don&#8217;t close connection twice, use JDBC Template from Spring to avoid this problem.&#160;</li>
</ul>
<h2>Reference</h2>
<ol>
<li><a target="_blank" href="http://www.javaworld.com/javaworld/jw-01-2002/jw-0111-hotspotgc.html">JavaWorld GC Article</a></li>
<li><a target="_blank" href="http://java.sun.com/docs/hotspot/index.html">Sun HotSpot Performance Document</a></li>
<li><a target="_blank" href="http://uuu.teetzen.net/uniweb/tomcat_performance.pdf">Tomcat Performance Slides</a></li>
</ol>
<p>&#160;&#160;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/implement-your-idea/scale-your-website/tomcat-performance-tuning/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Will Java Die?</title>
		<link>http://www.solutionhacker.com/implement-your-idea/architect-corner/will-java-die/</link>
		<comments>http://www.solutionhacker.com/implement-your-idea/architect-corner/will-java-die/#comments</comments>
		<pubDate>Fri, 21 Sep 2007 17:48:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[2.1. Architect Corner]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[platform]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[web 2.0]]></category>

		<guid isPermaLink="false">http://www.solutionhacker.com/2007/09/21/will-java-die/</guid>
		<description><![CDATA[Scripting languages dominates Web 2.0 We are seeing signs of age and dissatisfaction of Java recently. PHP and Ruby becomes popular choice for Web 2.0 development. &#34;.NET&#34; has increasing number of usage comparing to J2EE. So, the common question that pops up to everyone&#8217;s mind is &#34;Will Java Die?&#34;. Yes, every language has its lifespan [...]]]></description>
			<content:encoded><![CDATA[<h2>Scripting languages dominates Web 2.0</h2>
<p>We are seeing signs of age and dissatisfaction of Java recently. <strong>PHP </strong>and <strong>Ruby </strong>becomes popular choice for <strong>Web 2.0</strong> development. <strong>&quot;.NET&quot;</strong> has increasing number of usage comparing to J2EE. So, the common question that pops up to everyone&#8217;s mind is <strong>&quot;Will Java Die?&quot;</strong>. Yes, every language has its lifespan and I believe Java will not live forever. The key is whether it will die soon or whether it can evolve and continue to sustain.  To answer the question, I need to temporarily put myself to a position to pick a language to learn. Then I see the reason why PHP and Ruby becomes so popular. </p>
<p><span id="more-72"></span></p>
<h3>Advantage of PHP or Ruby</h3>
<ol>
<li>Easier to learn</li>
<li>Most of web hosting company supports them in <strong>cheap </strong>price. You hardly find a web hosting company provides full support of J2EE under $10 US dollars. </li>
<li>You can find lots of php components available on the Web and it is very easy to install. Looks like PHP is designed for Web development from ground up. </li>
</ol>
<p>Honestly, I will choose it for Web 2.0 development if you ask me. Go check my site: <a href="http://www.justproposed.com/">www.justproposed.com</a>. It is created in PHP. Now, whether I lose my confidence in Java anymore? Not at all. Why? Because<strong> I don&#8217;t see Java as a language but a platform.</strong> </p>
<h2>Java is more than a language</h2>
<p>PHP is good but it is more on ad hoc web development for prototyping the concept. Once your site grows big, there are many issues related to enterprise-scale development like <strong>clustering, caching, transactional integrity, thread-pooling, security.</strong>..etc. PHP is pretty bad at this stuff. I don&#8217;t see a platform as rich as Java for these tasks. On the other hand, .NET is the one with head to head competition with Java. However, it is <strong>not</strong> platform independent plus it is a very close environnment. If there is any problem in the platform level, you are pretty much out of luck.  On the other hand, Java is not sitting still. You can see the scripting support in Java 1.6 that gives you freedom to pick the language syntax you like but continues to leverage the power of Java platform. I would see more and more people using JRuby or JPython in the future. Yes! They can co-exist, why reinvent the wheel?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.solutionhacker.com/implement-your-idea/architect-corner/will-java-die/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
