<?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>Anand Rajasekar &#187; Java</title>
	<atom:link href="http://www.anandsekar.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.anandsekar.com</link>
	<description>ரெங்கசாமி ஆனந்த் சேகர் ராஜசேகர்</description>
	<lastBuildDate>Thu, 03 Jun 2010 05:30:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Modifying Classpath at Runtime</title>
		<link>http://www.anandsekar.com/2008/08/27/modifying-classpath-at-runtime/</link>
		<comments>http://www.anandsekar.com/2008/08/27/modifying-classpath-at-runtime/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 20:19:35 +0000</pubDate>
		<dc:creator>anandsekar</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.anandsekar.com/?p=52</guid>
		<description><![CDATA[A hackish way of modifying the classpath at runtime can be done in a way described at the java forums Modifying Classpath at Runtime
A cleaner approach is to configure your own classloader and set it as the threads context classloader




URL&#91;&#93; urls=new URL&#91;2&#93;;


URLClassLoader classLoader=new URLClassLoader&#40;urls&#41;;


Thread.currentThread&#40;&#41;.setContextClassLoader&#40;classLoader&#41;; 




This is extremely useful if your unit test cases have dependencies [...]]]></description>
			<content:encoded><![CDATA[<p>A hackish way of modifying the classpath at runtime can be done in a way described at the java forums <a href="http://forums.sun.com/thread.jspa?threadID=300557&#038;forumID=32">Modifying Classpath at Runtime</a><br />
A cleaner approach is to configure your own classloader and set it as the threads context classloader</p>
<div class="codesnip-container" >
<div class="codesnip">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?q=allinurl%3AURL+java.sun.com&#038;bntl=1"><span class="kw3">URL</span></a><span class="br0">&#91;</span><span class="br0">&#93;</span> urls=<span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3AURL+java.sun.com&#038;bntl=1"><span class="kw3">URL</span></a><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?q=allinurl%3AURLClassLoader+java.sun.com&#038;bntl=1"><span class="kw3">URLClassLoader</span></a> classLoader=<span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3AURLClassLoader+java.sun.com&#038;bntl=1"><span class="kw3">URLClassLoader</span></a><span class="br0">&#40;</span>urls<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a>.<span class="me1">currentThread</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">setContextClassLoader</span><span class="br0">&#40;</span>classLoader<span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
<p>This is extremely useful if your unit test cases have dependencies that you would like to load at runtime</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anandsekar.com/2008/08/27/modifying-classpath-at-runtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>InheritableThreadLocal and Tomcat</title>
		<link>http://www.anandsekar.com/2008/05/08/inheritablethreadlocal-and-tomcat/</link>
		<comments>http://www.anandsekar.com/2008/05/08/inheritablethreadlocal-and-tomcat/#comments</comments>
		<pubDate>Thu, 08 May 2008 05:18:40 +0000</pubDate>
		<dc:creator>anandsekar</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://www.anandsekar.com/2008/05/08/inheritablethreadlocal-and-tomcat/</guid>
		<description><![CDATA[InheritableThreadLocal does not work with tomcat as tomcat thread pool does not clear the thread local contacts after a request has been executed. This results in no good way to share information information between the parent and the child threads that are created using thread local variables. 
One way to solve this problem is to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/InheritableThreadLocal.html">InheritableThreadLocal</a> does not work with tomcat as tomcat thread pool does not clear the thread local contacts after a request has been executed. This results in no good way to share information information between the parent and the child threads that are created using thread local variables. </p>
<p>One way to solve this problem is to maintain a reference to the parent thread by writing your own thread that is constructed by a thread factory.</p>
<div class="codesnip-container" >
<div class="codesnip">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> MyThread <span class="kw2">extends</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> parent;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> MyThread<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> parent<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">this</span>.<span class="me1">parent</span> = parent;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> getParent<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> parent;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
<div class="codesnip-container" >
<div class="codesnip">
<ol>
<li class="li1">
<div class="de1"><span class="co2">import java.util.concurrent.ThreadFactory;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> MyThreadFactory <span class="kw2">implements</span> ThreadFactory <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; @Override</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> newThread<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3ARunnable+java.sun.com&#038;bntl=1"><span class="kw3">Runnable</span></a> arg0<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> <span class="kw2">new</span> MyThread<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a>.<span class="me1">currentThread</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
<div class="codesnip-container" >
<div class="codesnip">
<ol>
<li class="li1">
<div class="de1"><span class="co2">import java.util.HashMap;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co2">import java.util.Map;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">public</span> <span class="kw2">class</span> ClassNeedingInheritableThreadLocal <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?q=allinurl%3AMap+java.sun.com&#038;bntl=1"><span class="kw3">Map</span></a> inheritableThreadLocal=<span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3AHashMap+java.sun.com&#038;bntl=1"><span class="kw3">HashMap</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&#038;bntl=1"><span class="kw3">Object</span></a> getInheritableThreadContext<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> inheritableThreadLocal.<span class="me1">get</span><span class="br0">&#40;</span>getThread<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> setInheritableThreadContext<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&#038;bntl=1"><span class="kw3">Object</span></a> context<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inheritableThreadLocal.<span class="me1">put</span><span class="br0">&#40;</span>getThread<span class="br0">&#40;</span><span class="br0">&#41;</span>, context<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> getThread<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> currentThread=<a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a>.<span class="me1">currentThread</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>currentThread instanceof MyThread<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentThread=getParent<span class="br0">&#40;</span><span class="br0">&#40;</span>MyThread<span class="br0">&#41;</span> currentThread<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> currentThread;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> getParent<span class="br0">&#40;</span>MyThread myThread<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AThread+java.sun.com&#038;bntl=1"><span class="kw3">Thread</span></a> parent=myThread.<span class="me1">getParent</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span>parent instanceof MyThread<span class="br0">&#41;</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> getParent<span class="br0">&#40;</span><span class="br0">&#40;</span>MyThread<span class="br0">&#41;</span> parent<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> parent;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span> </div>
</li>
</ol>
</div>
</div>
<p>Create a executor service by using our thread factory</p>
<div class="codesnip-container" >
<div class="codesnip">
<ol>
<li class="li1">
<div class="de1">ExecutorService service=Executors.<span class="me1">newFixedThreadPool</span><span class="br0">&#40;</span><span class="nu0">10</span>, <span class="kw2">new</span> MyThreadFactory<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
<p>Remember that you would need to clean up all the objects that were put in the thread local. This can be done with a filter or by using AOP.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.anandsekar.com/2008/05/08/inheritablethreadlocal-and-tomcat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
