<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/4.0.5" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Daniel Amadei</title>
		<link>http://amadei.com.br/blog/index.php</link>
		<atom:link rel="self" type="application/rss+xml" href="http://amadei.com.br/blog/index.php?tempskin=_rss2" />
		<description>A blog about Java, Integration, SOA and Oracle products in general.</description>
		<language>en-US</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=4.0.5"/>
		<ttl>60</ttl>
				<item>
			<title>OSB non blocking HTTP: extreme scalability</title>
			<link>http://amadei.com.br/blog/index.php/osb-non-blocking-http-extreme</link>
			<pubDate>Thu, 26 Apr 2012 00:59:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="alt">Java</category>
<category domain="main">SOA &amp; Integration</category>			<guid isPermaLink="false">49@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;Oracle Service Bus provides a non blocking HTTP invoking mechanism, releasing the thread used to make the request until a response is received, so that a new thread is allocated and takes care of the response handling, providing extreme scalability. I aim to show you in this post how it works and provide some theoretical info on why it&amp;#8217;s important.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why do we scale more if we use less threads?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Threads are costly resources and are limited in the sense of how many you can create and still have a responsive system. More threads represent more context switching at the CPU and more work executed by the system, leaving fewer resources to execute what matters: application code. Threads are also expensive to be created, that&amp;#8217;s why we generally see pools of them, so releasing a thread quicker will enable you to be able to handle more requests and will, certainly, improve the scalability of your entire system.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does OSB provide?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When routing to a service which has a non blocking capable transport, depending on the scenario (protocol, type of routing, etc), OSB has the capability to send the request and release the thread until a response is received. This is extremely useful in cases where the response may be too slow so that the released thread may be used to serve other request instead of keeping blocked for a long time. When the callee service has a response to deliver, OSB borrows a thread from WebLogic&amp;#8217;s thread pool and uses it to handle the response. OSB has support for a lot of protocols as non blocking and we will explore how this works with HTTP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Seeing it in practice&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating a slow service&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To see what happens, we first need a slow service, which will allow us to route to it and see the request thread being released back to the pool. This is achieved by the following JAX-WS service which sleeps for 60 Seconds before delivering the response:&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
package br.com.amadei.osb.nbhttp;
import java.util.Date;
import javax.jws.WebService;

@WebService
public class SlowWS {

	public String verySlowOperation() {
		try {
			Thread.sleep(60000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return new Date().toString();
	}
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Deploy the WAR containing this Web Service to the same WLS running OSB.&lt;/p&gt;

&lt;p&gt;We will have another class, which will be called via a Java Callout from OSB to show the current thread. The class is as follows:&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;
package br.com.amadei.osb.nbhttp;

public class LogThread {

	public static void logCurrThread() {
		System.out.println(&quot;***************************** &quot;
				+ Thread.currentThread());
	}
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Compile this class and create a JAR. In OSB, create an OSB project and copy this JAR into it. It will be used later.&lt;/p&gt;

&lt;p&gt;Now, import the WSDL and XSD of the SlowWS to OSB and create a business service for it as can be seen bellow. This OSB business service should point to the created &quot;SlowWS&quot; JAX-WS web service, deployed as a WAR.&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-01.png?mtime=1335130341&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-01.png?mtime=1335130341&quot; width=&quot;898&quot; height=&quot;336&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Create a proxy service based on the same WSDL and add a Routing to the created business service. Inside the request and response actions of the Routing, add a java callout to the &lt;code&gt;LogThread.logCurrThread&lt;/code&gt; method call. This can be seen bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-02.png?mtime=1335130596&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-02.png?mtime=1335130596&quot; width=&quot;282&quot; height=&quot;387&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Create a soapUI project to test the proxy service as testing it via SBCONSOLE won&amp;#8217;t release the thread as it will remain active for the web console, so soapUI is a better option in this case.&lt;/p&gt;

&lt;p&gt;Before making the first request, go to the &lt;code&gt;WebLogic console&lt;/code&gt;. We will take a thread dump  to see the state of the thread which served the request. So, after logging in, expand the &lt;code&gt;Environment&lt;/code&gt; link and click &lt;code&gt;Servers&lt;/code&gt;. Select the &lt;code&gt;AdminServer&lt;/code&gt; (supposing you&amp;#8217;re running the developer install of OSB), &lt;code&gt;Monitoring&lt;/code&gt;, &lt;code&gt;Threads&lt;/code&gt;. This is where you&amp;#8217;ll be able to take the dump when needed. &lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-03.png?mtime=1335131670&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-03.png?mtime=1335131670&quot; width=&quot;737&quot; height=&quot;182&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Before going back to soapUI to make the request, notice that it will log the thread handling the request, so you will be able to look back at the dump. You should do the following:
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Invoke the request via soapUI;&lt;/li&gt;
&lt;li&gt;Go back to the console and take the name of the thread OSB logged to the system output;&lt;/li&gt;
&lt;li&gt;Take a thread dump and see that this thread is doing;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let&amp;#8217;s try it!&lt;/p&gt;

&lt;p&gt;After starting the request via soapUI, I see the following message in the standard output:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;***************************** Thread[[ACTIVE] ExecuteThread: &#039;5&#039; for queue: &#039;weblogic.kernel.Default (self-tuning)&#039;,5,Pooled Threads]&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;This means that the ExecuteThread 5 from weblogic.kernel.Default is handling my request. After that, I took a thread dump and searched for this thread and could see the following stack for it:
&lt;/p&gt;
&lt;pre&gt;[ACTIVE] ExecuteThread: &#039;5&#039; for queue: &#039;weblogic.kernel.Default (self-tuning)&#039;&quot; waiting for lock weblogic.work.ExecuteThread@42f9ef WAITING
    java.lang.Object.wait(Native Method)
    java.lang.Object.wait(Object.java:485)
    weblogic.work.ExecuteThread.waitForRequest(ExecuteThread.java:205)
    weblogic.work.ExecuteThread.run(ExecuteThread.java:226)&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;This stack means this thread is waiting for a request inside WebLogic&amp;#8217;s Thread pool or, to be more specific, it was returned to the pool. At this time, the response was not yet to our service and the client is also waiting OSB to deliver the response but, the important thing to notice is that we are not holding a thread until such response available, proving the non blocking capability. &lt;/p&gt;

&lt;p&gt;After 1 minute, the response is delivered and we see in the log the thread elected to hold the response (even if the request was blocking, response would have been delivered in a different thread).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The other side&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This behavior may seem a little strange at the first seen, so we can see how the behavior would be if this request was blocking. To make it blocking, add a Routing Options node inside the Request Action pipeline and configure its &lt;code&gt;QoS&lt;/code&gt; as &lt;code&gt;&amp;#8220;Exactly Once&amp;#8221;&lt;/code&gt; as can be seen in the figure bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-04.png?mtime=1335133021&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-04.png?mtime=1335133021&quot; width=&quot;521&quot; height=&quot;586&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;The Routing will be blocking if the QoS is exactly once (Service Callout calls are also always blocking).&lt;/p&gt;

&lt;p&gt;Deploy the new version and do the same steps done before. This time, &lt;code&gt;Execute Thread 14&lt;/code&gt; was elected to serve my request, and looking at its stack, we see it&amp;#8217;s blocked trying to read the response.&lt;/p&gt;

&lt;pre&gt;
[ACTIVE] ExecuteThread: &#039;14&#039; for queue: &#039;weblogic.kernel.Default (self-tuning)&#039;&quot; RUNNABLE native
   java.net.SocketInputStream.socketRead0(Native Method)
   java.net.SocketInputStream.read(SocketInputStream.java:129)
   java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
   java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
   java.io.BufferedInputStream.read(BufferedInputStream.java:317)
   weblogic.net.http.MessageHeader.isHTTP(MessageHeader.java:224)
   weblogic.net.http.MessageHeader.parseHeader(MessageHeader.java:148)
   weblogic.net.http.HttpClient.parseHTTP(HttpClient.java:468)
   weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:401)
   weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37)
   weblogic.net.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1005)
   com.bea.wli.sb.transports.http.HttpOutboundMessageContext.getResponse(HttpOutboundMessageContext.java:679)
   com.bea.wli.sb.transports.http.HttpOutboundMessageContext.send(HttpOutboundMessageContext.java:347)
   com.bea.wli.sb.transports.http.wls.HttpTransportProvider.sendMessageAsync(HttpTransportProvider.java:211)
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Here, we saw how the thread could get blocked until a response is received. As we saw during the post, this is worst than the former scenario, so try to leverage non blocking whenever possible - sometimes, however, you have only the option of working with blocking requests.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Oracle Service Bus offers a extremely helpful way for you to scale your system and what&amp;#8217;s better is that it requires no coding. Take this into consideration when architecting your services in OSB!&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/osb-non-blocking-http-extreme&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Oracle Service Bus provides a non blocking HTTP invoking mechanism, releasing the thread used to make the request until a response is received, so that a new thread is allocated and takes care of the response handling, providing extreme scalability. I aim to show you in this post how it works and provide some theoretical info on why it&#8217;s important.</p>

<p><strong>Why do we scale more if we use less threads?</strong></p>

<p>Threads are costly resources and are limited in the sense of how many you can create and still have a responsive system. More threads represent more context switching at the CPU and more work executed by the system, leaving fewer resources to execute what matters: application code. Threads are also expensive to be created, that&#8217;s why we generally see pools of them, so releasing a thread quicker will enable you to be able to handle more requests and will, certainly, improve the scalability of your entire system.</p>

<p><strong>What does OSB provide?</strong></p>

<p>When routing to a service which has a non blocking capable transport, depending on the scenario (protocol, type of routing, etc), OSB has the capability to send the request and release the thread until a response is received. This is extremely useful in cases where the response may be too slow so that the released thread may be used to serve other request instead of keeping blocked for a long time. When the callee service has a response to deliver, OSB borrows a thread from WebLogic&#8217;s thread pool and uses it to handle the response. OSB has support for a lot of protocols as non blocking and we will explore how this works with HTTP.</p>

<p><strong>Seeing it in practice</strong></p>
<p><strong>Creating a slow service</strong></p>

<p>To see what happens, we first need a slow service, which will allow us to route to it and see the request thread being released back to the pool. This is achieved by the following JAX-WS service which sleeps for 60 Seconds before delivering the response:</p>

<pre class="prettyprint">
package br.com.amadei.osb.nbhttp;
import java.util.Date;
import javax.jws.WebService;

@WebService
public class SlowWS {

	public String verySlowOperation() {
		try {
			Thread.sleep(60000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return new Date().toString();
	}
}
</pre>
<p><br /></p>
<p>Deploy the WAR containing this Web Service to the same WLS running OSB.</p>

<p>We will have another class, which will be called via a Java Callout from OSB to show the current thread. The class is as follows:</p>


<pre class="prettyprint">
package br.com.amadei.osb.nbhttp;

public class LogThread {

	public static void logCurrThread() {
		System.out.println("***************************** "
				+ Thread.currentThread());
	}
}
</pre>
<p><br /></p>
<p>Compile this class and create a JAR. In OSB, create an OSB project and copy this JAR into it. It will be used later.</p>

<p>Now, import the WSDL and XSD of the SlowWS to OSB and create a business service for it as can be seen bellow. This OSB business service should point to the created "SlowWS" JAX-WS web service, deployed as a WAR.</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-01.png?mtime=1335130341"><img alt="" src="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-01.png?mtime=1335130341" width="898" height="336" /></a></div>

<p>Create a proxy service based on the same WSDL and add a Routing to the created business service. Inside the request and response actions of the Routing, add a java callout to the <code>LogThread.logCurrThread</code> method call. This can be seen bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-02.png?mtime=1335130596"><img alt="" src="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-02.png?mtime=1335130596" width="282" height="387" /></a></div>
<p><br /></p>
<p>Create a soapUI project to test the proxy service as testing it via SBCONSOLE won&#8217;t release the thread as it will remain active for the web console, so soapUI is a better option in this case.</p>

<p>Before making the first request, go to the <code>WebLogic console</code>. We will take a thread dump  to see the state of the thread which served the request. So, after logging in, expand the <code>Environment</code> link and click <code>Servers</code>. Select the <code>AdminServer</code> (supposing you&#8217;re running the developer install of OSB), <code>Monitoring</code>, <code>Threads</code>. This is where you&#8217;ll be able to take the dump when needed. </p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-03.png?mtime=1335131670"><img alt="" src="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-03.png?mtime=1335131670" width="737" height="182" /></a></div>
<p><br /></p>

<p>Before going back to soapUI to make the request, notice that it will log the thread handling the request, so you will be able to look back at the dump. You should do the following:
</p>

<ol>
<li>Invoke the request via soapUI;</li>
<li>Go back to the console and take the name of the thread OSB logged to the system output;</li>
<li>Take a thread dump and see that this thread is doing;</li>
</ol>

<p>Let&#8217;s try it!</p>

<p>After starting the request via soapUI, I see the following message in the standard output:</p>
<p><code>***************************** Thread[[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads]</code><br />
<br /></p>
<p>This means that the ExecuteThread 5 from weblogic.kernel.Default is handling my request. After that, I took a thread dump and searched for this thread and could see the following stack for it:
</p>
<pre>[ACTIVE] ExecuteThread: '5' for queue: 'weblogic.kernel.Default (self-tuning)'" waiting for lock weblogic.work.ExecuteThread@42f9ef WAITING
    java.lang.Object.wait(Native Method)
    java.lang.Object.wait(Object.java:485)
    weblogic.work.ExecuteThread.waitForRequest(ExecuteThread.java:205)
    weblogic.work.ExecuteThread.run(ExecuteThread.java:226)</pre>
<p><br /></p>
<p>This stack means this thread is waiting for a request inside WebLogic&#8217;s Thread pool or, to be more specific, it was returned to the pool. At this time, the response was not yet to our service and the client is also waiting OSB to deliver the response but, the important thing to notice is that we are not holding a thread until such response available, proving the non blocking capability. </p>

<p>After 1 minute, the response is delivered and we see in the log the thread elected to hold the response (even if the request was blocking, response would have been delivered in a different thread).</p>

<p><strong>The other side</strong></p>

<p>This behavior may seem a little strange at the first seen, so we can see how the behavior would be if this request was blocking. To make it blocking, add a Routing Options node inside the Request Action pipeline and configure its <code>QoS</code> as <code>&#8220;Exactly Once&#8221;</code> as can be seen in the figure bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-04.png?mtime=1335133021"><img alt="" src="http://amadei.com.br/blog/media/blogs/blog/osb-nb/osb-nb-04.png?mtime=1335133021" width="521" height="586" /></a></div>
<p><br /></p>
<p>The Routing will be blocking if the QoS is exactly once (Service Callout calls are also always blocking).</p>

<p>Deploy the new version and do the same steps done before. This time, <code>Execute Thread 14</code> was elected to serve my request, and looking at its stack, we see it&#8217;s blocked trying to read the response.</p>

<pre>
[ACTIVE] ExecuteThread: '14' for queue: 'weblogic.kernel.Default (self-tuning)'" RUNNABLE native
   java.net.SocketInputStream.socketRead0(Native Method)
   java.net.SocketInputStream.read(SocketInputStream.java:129)
   java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
   java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
   java.io.BufferedInputStream.read(BufferedInputStream.java:317)
   weblogic.net.http.MessageHeader.isHTTP(MessageHeader.java:224)
   weblogic.net.http.MessageHeader.parseHeader(MessageHeader.java:148)
   weblogic.net.http.HttpClient.parseHTTP(HttpClient.java:468)
   weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:401)
   weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37)
   weblogic.net.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1005)
   com.bea.wli.sb.transports.http.HttpOutboundMessageContext.getResponse(HttpOutboundMessageContext.java:679)
   com.bea.wli.sb.transports.http.HttpOutboundMessageContext.send(HttpOutboundMessageContext.java:347)
   com.bea.wli.sb.transports.http.wls.HttpTransportProvider.sendMessageAsync(HttpTransportProvider.java:211)
</pre>
<p><br /></p>
<p>Here, we saw how the thread could get blocked until a response is received. As we saw during the post, this is worst than the former scenario, so try to leverage non blocking whenever possible - sometimes, however, you have only the option of working with blocking requests.</p>
<p><br /></p>
<p><strong>Conclusion</strong></p>
<p>Oracle Service Bus offers a extremely helpful way for you to scale your system and what&#8217;s better is that it requires no coding. Take this into consideration when architecting your services in OSB!</p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/osb-non-blocking-http-extreme">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/osb-non-blocking-http-extreme#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=49</wfw:commentRss>
		</item>
				<item>
			<title>My new article on OTN: The Joy of Overriding</title>
			<link>http://amadei.com.br/blog/index.php/my-new-article-on-otn-1</link>
			<pubDate>Sat, 21 Apr 2012 20:55:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="alt">Java</category>
<category domain="main">Coherence</category>			<guid isPermaLink="false">48@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;I&#039;ve come to write a new article for Oracle OTN. This new one it&#039;s about Oracle Coherence and its ability to override system property names used by the product. The article is available &lt;a href=&quot;http://www.oracle.com/technetwork/articles/oem/coherence-overriding-1592484.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/my-new-article-on-otn-1&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>I've come to write a new article for Oracle OTN. This new one it's about Oracle Coherence and its ability to override system property names used by the product. The article is available <a href="http://www.oracle.com/technetwork/articles/oem/coherence-overriding-1592484.html">here</a>.</p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/my-new-article-on-otn-1">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/my-new-article-on-otn-1#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=48</wfw:commentRss>
		</item>
				<item>
			<title>Propagating errors in Oracle Service Bus flows</title>
			<link>http://amadei.com.br/blog/index.php/propagating-errors-in-osb</link>
			<pubDate>Tue, 10 Apr 2012 02:23:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="main">SOA &amp; Integration</category>			<guid isPermaLink="false">47@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;I&amp;#8217;m going to provide some info here on how you can easily propagate errors in OSB flows what can be specially handy when dealing with multiple web service calls, when you want to propagate an error as returned by a called web service.&lt;/p&gt;

&lt;p&gt;Suppose you have a service in OSB with the following pipeline:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/01.png?mtime=1334021282&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/01.png?mtime=1334021282&quot; width=&quot;341&quot; height=&quot;425&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;This service will always raise an error and, as this is an WSDL based HTTP web service, this will translate into a SOAP Fault being returned to the called. The contents of the SOAP Fault will be dictated by OSB with some customization made in the Raise Error stage action as can be seen bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/02.png?mtime=1334021282&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/02.png?mtime=1334021282&quot; width=&quot;704&quot; height=&quot;213&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt; &lt;/p&gt;
&lt;p&gt;When calling this service (let&amp;#8217;s name it CalleeService), we get the following response (as expected):&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/03.png?mtime=1334021282&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/03.png?mtime=1334021282&quot; width=&quot;475&quot; height=&quot;394&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, let&amp;#8217;s try to route to the Callee Service from this new one. Let&amp;#8217;s call it Caller Service and its routing configuration should be like the one bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/04.png?mtime=1334021567&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/04.png?mtime=1334021567&quot; width=&quot;294&quot; height=&quot;305&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/05.png?mtime=1334021567&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/05.png?mtime=1334021567&quot; width=&quot;1197&quot; height=&quot;260&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;With the routing configured, let&amp;#8217;s try to call our new created Caller Service. You must be expecting to receive the same error thrown by Callee Service in respone, however, that&amp;#8217;s not what happens as can be seen bellow:
&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/06.png?mtime=1334021567&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/06.png?mtime=1334021567&quot; width=&quot;474&quot; height=&quot;339&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;OSB handles the error and returns a generic protocol error.&lt;/p&gt; 

&lt;p&gt;To make OSB propagate the error received, add an Error Handler to the flow. We will add it at the service level as can be seen bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/07.png?mtime=1334021567&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/07.png?mtime=1334021567&quot; width=&quot;402&quot; height=&quot;313&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Add a Reply inside the Error Handler and configure it as &amp;#8220;With Failure&amp;#8221; as can be seen bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/08.png?mtime=1334021567&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/08.png?mtime=1334021567&quot; width=&quot;1197&quot; height=&quot;263&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, executing the service, we see the error is propagated up to the caller as thrown by the callee service, what is the main objective of this post. Bellow, we see it:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog/media/blogs/blog/09.png?mtime=1334021567&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog/media/blogs/blog/09.png?mtime=1334021567&quot; width=&quot;477&quot; height=&quot;419&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;I aim to have successfully shown you how to propagate errors in OSB in a very simple manner. Obviously you can write code to get more advanced behavior but if you need to simply propagate the error and avoid the generic error handling made by OSB, this post will certainly help you!&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/propagating-errors-in-osb&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>I&#8217;m going to provide some info here on how you can easily propagate errors in OSB flows what can be specially handy when dealing with multiple web service calls, when you want to propagate an error as returned by a called web service.</p>

<p>Suppose you have a service in OSB with the following pipeline:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/01.png?mtime=1334021282"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/01.png?mtime=1334021282" width="341" height="425" /></a></div>
<p><br /></p>
<p>This service will always raise an error and, as this is an WSDL based HTTP web service, this will translate into a SOAP Fault being returned to the called. The contents of the SOAP Fault will be dictated by OSB with some customization made in the Raise Error stage action as can be seen bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/02.png?mtime=1334021282"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/02.png?mtime=1334021282" width="704" height="213" /></a></div>
<p><br /> </p>
<p>When calling this service (let&#8217;s name it CalleeService), we get the following response (as expected):</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/03.png?mtime=1334021282"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/03.png?mtime=1334021282" width="475" height="394" /></a></div>
<p><br /></p>

<p>Now, let&#8217;s try to route to the Callee Service from this new one. Let&#8217;s call it Caller Service and its routing configuration should be like the one bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/04.png?mtime=1334021567"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/04.png?mtime=1334021567" width="294" height="305" /></a></div>
<p><br /></p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/05.png?mtime=1334021567"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/05.png?mtime=1334021567" width="1197" height="260" /></a></div>
<p><br /></p>

<p>With the routing configured, let&#8217;s try to call our new created Caller Service. You must be expecting to receive the same error thrown by Callee Service in respone, however, that&#8217;s not what happens as can be seen bellow:
</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/06.png?mtime=1334021567"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/06.png?mtime=1334021567" width="474" height="339" /></a></div>
<p><br /></p>

<p>OSB handles the error and returns a generic protocol error.</p> 

<p>To make OSB propagate the error received, add an Error Handler to the flow. We will add it at the service level as can be seen bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/07.png?mtime=1334021567"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/07.png?mtime=1334021567" width="402" height="313" /></a></div>
<p><br /></p>

<p>Add a Reply inside the Error Handler and configure it as &#8220;With Failure&#8221; as can be seen bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/08.png?mtime=1334021567"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/08.png?mtime=1334021567" width="1197" height="263" /></a></div>
<p><br /></p>

<p>Now, executing the service, we see the error is propagated up to the caller as thrown by the callee service, what is the main objective of this post. Bellow, we see it:</p>

<div class="image_block"><a href="http://amadei.com.br/blog/media/blogs/blog/09.png?mtime=1334021567"><img title="" alt="" src="http://amadei.com.br/blog/media/blogs/blog/09.png?mtime=1334021567" width="477" height="419" /></a></div>

<p>I aim to have successfully shown you how to propagate errors in OSB in a very simple manner. Obviously you can write code to get more advanced behavior but if you need to simply propagate the error and avoid the generic error handling made by OSB, this post will certainly help you!</p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/propagating-errors-in-osb">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/propagating-errors-in-osb#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=47</wfw:commentRss>
		</item>
				<item>
			<title>Programmatically Registering Servlets!</title>
			<link>http://amadei.com.br/blog/index.php/programmatically-registering-servlets</link>
			<pubDate>Sun, 26 Feb 2012 23:29:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="main">Java</category>			<guid isPermaLink="false">46@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;The Servlets 3.0 specification adds a couple of new features. One nice new feature specially for framework designers is the capability of programmatically registering servlets or filters. &lt;/p&gt;
&lt;p&gt;You can do such registration via the servlet context and there&amp;#8217;s also a way to directly detect servlets from a JAR file.&lt;/p&gt;
&lt;p&gt;Bellow we have a sample servlet that we will register dynamically with the web container:&lt;/p&gt;
&lt;pre class=&quot;prettyprint&quot;&gt;
package br.com.amadei.blog.dyn.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletA extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.getWriter().println(&quot;&amp;lt;h1&amp;gt;&quot;);
		response.getWriter().println(&quot;Hi, it&#039;s &quot; + new Date());
		response.getWriter().println(&quot;&amp;lt;/h1&amp;gt;&quot;);
	}

}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;We can register such servlet via a listener like the one bellow:&lt;/p&gt;


&lt;pre class=&quot;prettyprint&quot;&gt;
package br.com.amadei.blog.dyn.servlets;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event) {
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext context = event.getServletContext();

		Dynamic dynamic = context.addServlet(&quot;ServletA&quot;, ServletA.class);
		dynamic.addMapping(&quot;/ServletA&quot;);
	}

}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;There are some interesting things to notice:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We use the new annotation made available by the Servlet 3.0 specification, @WebListener so that our listener is discovered without the necessity of being declared in the deployment descriptor (we can also add listeners dynamically via method calls too!).&lt;/li&gt;

  &lt;li&gt;The method responsible for the servlet registration is ServletContext.addServlet(). It returns an instance of javax.servlet.ServletRegistration.Dynamic that is used to configure the servlet registration later (in this case, we use it to define the servlet&amp;#8217;s URL mapping).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By running the application and pointing the browser to &amp;lt;server address&amp;gt;:&amp;lt;server port&amp;gt;/&amp;lt;Context Root&amp;gt;/ServletA, we see the programatically registered servlet responding, showing our sample just worked as shown bellow:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/Untitled.png?mtime=1330283954&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/Untitled.png?mtime=1330283954&quot; width=&quot;972&quot; height=&quot;338&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;There are many other programatic changes you can make new to servlet 3.0:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Register and configure servlets programatically (as we saw);
&lt;/li&gt;
  &lt;li&gt;Register and configure listeners and filter programatically, in the same way we did with servlets;
&lt;/li&gt;
  &lt;li&gt;Configure the session tracking cookie;
&lt;/li&gt;
  &lt;li&gt;Configure the session tracking mode.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This sample may seem useless and in fact it really is. My main idea was just to show this capability and I believe you can imagine how powerful it can be for framework and product builders that rely on the Servlet API. &lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/programmatically-registering-servlets&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>The Servlets 3.0 specification adds a couple of new features. One nice new feature specially for framework designers is the capability of programmatically registering servlets or filters. </p>
<p>You can do such registration via the servlet context and there&#8217;s also a way to directly detect servlets from a JAR file.</p>
<p>Bellow we have a sample servlet that we will register dynamically with the web container:</p>
<pre class="prettyprint">
package br.com.amadei.blog.dyn.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletA extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.getWriter().println("&lt;h1&gt;");
		response.getWriter().println("Hi, it's " + new Date());
		response.getWriter().println("&lt;/h1&gt;");
	}

}
</pre>
<p><br /></p>
<p>We can register such servlet via a listener like the one bellow:</p>


<pre class="prettyprint">
package br.com.amadei.blog.dyn.servlets;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event) {
	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext context = event.getServletContext();

		Dynamic dynamic = context.addServlet("ServletA", ServletA.class);
		dynamic.addMapping("/ServletA");
	}

}
</pre>
<p><br /></p>
<p>There are some interesting things to notice:</p>
<ul>
  <li>We use the new annotation made available by the Servlet 3.0 specification, @WebListener so that our listener is discovered without the necessity of being declared in the deployment descriptor (we can also add listeners dynamically via method calls too!).</li>

  <li>The method responsible for the servlet registration is ServletContext.addServlet(). It returns an instance of javax.servlet.ServletRegistration.Dynamic that is used to configure the servlet registration later (in this case, we use it to define the servlet&#8217;s URL mapping).</li>
</ul>

<p>By running the application and pointing the browser to &lt;server address&gt;:&lt;server port&gt;/&lt;Context Root&gt;/ServletA, we see the programatically registered servlet responding, showing our sample just worked as shown bellow:</p>
<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/Untitled.png?mtime=1330283954"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/Untitled.png?mtime=1330283954" width="972" height="338" /></a></div>
<p><br /></p>
<p>There are many other programatic changes you can make new to servlet 3.0:</p>

<ul>
  <li>Register and configure servlets programatically (as we saw);
</li>
  <li>Register and configure listeners and filter programatically, in the same way we did with servlets;
</li>
  <li>Configure the session tracking cookie;
</li>
  <li>Configure the session tracking mode.
</li>
</ul>

<p>This sample may seem useless and in fact it really is. My main idea was just to show this capability and I believe you can imagine how powerful it can be for framework and product builders that rely on the Servlet API. </p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/programmatically-registering-servlets">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/programmatically-registering-servlets#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=46</wfw:commentRss>
		</item>
				<item>
			<title>My new article on OTN: Unit Testing Asynchronous BPEL Processes Using soapUI</title>
			<link>http://amadei.com.br/blog/index.php/my-new-article-on-otn</link>
			<pubDate>Wed, 15 Feb 2012 22:40:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="alt">Unit Testing</category>
<category domain="alt">soapUI</category>
<category domain="alt">SOA</category>
<category domain="main">BPEL</category>			<guid isPermaLink="false">45@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;My new article was published on OTN. It talks about how to automate unit testing of asynchronous projects using soapUI using mechanisms like WS-Adressing for request and callback correlation. 
&lt;/p&gt;
&lt;p&gt;Access it clicking &lt;a href=&quot;http://www.oracle.com/technetwork/articles/soa/bpel-amadei-1518001.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/my-new-article-on-otn&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>My new article was published on OTN. It talks about how to automate unit testing of asynchronous projects using soapUI using mechanisms like WS-Adressing for request and callback correlation. 
</p>
<p>Access it clicking <a href="http://www.oracle.com/technetwork/articles/soa/bpel-amadei-1518001.html">here</a>.</p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/my-new-article-on-otn">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/my-new-article-on-otn#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=45</wfw:commentRss>
		</item>
				<item>
			<title>Getting rid of the &#8220;The assign activity of the to node query is returning zero node.&#8221; error in Oracle BPEL 11g</title>
			<link>http://amadei.com.br/blog/index.php/getting-rid-of-the-the</link>
			<pubDate>Thu, 19 Jan 2012 22:40:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="main">SOA &amp; Integration</category>			<guid isPermaLink="false">44@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;
The BPELWS 1.1 specification mandates that when an assign is made, the &amp;#8220;to&amp;#8221; node must evaluate to exactly one node. If it evaluates to zero nodes, a bpws:selectionFailure must be thrown. This can become quite inconvenient as you have to pre-populate your XML fields to empty elements before assigning to them.
&lt;/p&gt;
&lt;p&gt;
Let&amp;#8217;s see it in practice.
&lt;/p&gt;
&lt;p&gt;
Imagine you have the following XSD defining your input (process element) and output (processResponse).
&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/01-xsd.png?mtime=1326760101&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/01-xsd.png?mtime=1326760101&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;And you have a BPEL service capable of returning the rest of the person&amp;#8217;s info based on the informed ID. Your BPEL process should look like the following:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/02-bpel.png?mtime=1326760101&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/02-bpel.png?mtime=1326760101&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;In the enrich assign we assign hard coded values to the name and age elements (it&amp;#8217;s just a test...). We enrich the received person and copy it to the output as shown bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/02.1-soap.png?mtime=1326761042&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/02.1-soap.png?mtime=1326761042&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;The following SOAP request is made to the BPEL process:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/02.2-soap.png?mtime=1326761049&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/02.2-soap.png?mtime=1326761049&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;You should be willing to receive a person as response containing name and age, however, you should receive an error similar to the one bellow:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;faultName: {{http://schemas.xmlsoap.org/ws/2003/03/business-process/}selectionFailure} messageType: {{http://schemas.oracle.com/bpel/extension}RuntimeFaultMessage} parts: {{ summary=&amp;lt;summary&amp;gt;XPath query string returns zero node. The assign activity of the to node query is returning zero node. Either the to node data or the xpath query in the to node was invalid. According to BPEL4WS spec 1.1 section 14.3, verify the to node value at line number 76 in the BPEL source. &amp;lt;/summary&amp;gt;}&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The reason for such error is that the xpath /client:process/client:person/client:name did not evaluate to a single node but to zero.&lt;/p&gt;

&lt;p&gt;One option would be to create the node empty with a &amp;#8220;XML fragment&amp;#8221; before assigning to it but it can become quite tedious. A better option is to use the Oracle extension property bpelx:insertMissingToData. To use such property, right click the copy line and select the insertMissingToData from the menu as shown bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/03-menu.png?mtime=1326760101&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/03-menu.png?mtime=1326760101&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
Do the same for the age assign and click Apply. Your assign should look like the one bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/03.1-assign.png?mtime=1326761198&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/03.1-assign.png?mtime=1326761198&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, the BPEL process run should be successful, showing the bpelx:insertMissingToData helped a long way!&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/04-bpelcompleted.png?mtime=1326760101&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/04-bpelcompleted.png?mtime=1326760101&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/05-bpelreply.png?mtime=1326760101&quot;&gt;&lt;img title=&quot;&quot; alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/05-bpelreply.png?mtime=1326760101&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/getting-rid-of-the-the&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>
The BPELWS 1.1 specification mandates that when an assign is made, the &#8220;to&#8221; node must evaluate to exactly one node. If it evaluates to zero nodes, a bpws:selectionFailure must be thrown. This can become quite inconvenient as you have to pre-populate your XML fields to empty elements before assigning to them.
</p>
<p>
Let&#8217;s see it in practice.
</p>
<p>
Imagine you have the following XSD defining your input (process element) and output (processResponse).
</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/01-xsd.png?mtime=1326760101"><img title="" alt="" src="http://amadei.com.br/blog//media/blogs/blog/01-xsd.png?mtime=1326760101" /></a></div>
<p><br /></p>

<p>And you have a BPEL service capable of returning the rest of the person&#8217;s info based on the informed ID. Your BPEL process should look like the following:</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/02-bpel.png?mtime=1326760101"><img title="" alt="" src="http://amadei.com.br/blog//media/blogs/blog/02-bpel.png?mtime=1326760101" /></a></div>
<p><br /></p>
<p>In the enrich assign we assign hard coded values to the name and age elements (it&#8217;s just a test...). We enrich the received person and copy it to the output as shown bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/02.1-soap.png?mtime=1326761042"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/02.1-soap.png?mtime=1326761042" /></a></div>

<p><br /></p>
<p>The following SOAP request is made to the BPEL process:</p>
<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/02.2-soap.png?mtime=1326761049"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/02.2-soap.png?mtime=1326761049" /></a></div>

<p><br /></p>
<p>You should be willing to receive a person as response containing name and age, however, you should receive an error similar to the one bellow:</p>

<p><code>faultName: {{http://schemas.xmlsoap.org/ws/2003/03/business-process/}selectionFailure} messageType: {{http://schemas.oracle.com/bpel/extension}RuntimeFaultMessage} parts: {{ summary=&lt;summary&gt;XPath query string returns zero node. The assign activity of the to node query is returning zero node. Either the to node data or the xpath query in the to node was invalid. According to BPEL4WS spec 1.1 section 14.3, verify the to node value at line number 76 in the BPEL source. &lt;/summary&gt;}<br />
</code></p>

<p>The reason for such error is that the xpath /client:process/client:person/client:name did not evaluate to a single node but to zero.</p>

<p>One option would be to create the node empty with a &#8220;XML fragment&#8221; before assigning to it but it can become quite tedious. A better option is to use the Oracle extension property bpelx:insertMissingToData. To use such property, right click the copy line and select the insertMissingToData from the menu as shown bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/03-menu.png?mtime=1326760101"><img title="" alt="" src="http://amadei.com.br/blog//media/blogs/blog/03-menu.png?mtime=1326760101" /></a></div>
<p><br /><br />
Do the same for the age assign and click Apply. Your assign should look like the one bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/03.1-assign.png?mtime=1326761198"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/03.1-assign.png?mtime=1326761198" /></a></div>

<p><br /></p>

<p>Now, the BPEL process run should be successful, showing the bpelx:insertMissingToData helped a long way!</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/04-bpelcompleted.png?mtime=1326760101"><img title="" alt="" src="http://amadei.com.br/blog//media/blogs/blog/04-bpelcompleted.png?mtime=1326760101" /></a></div>
<p><br /></p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/05-bpelreply.png?mtime=1326760101"><img title="" alt="" src="http://amadei.com.br/blog//media/blogs/blog/05-bpelreply.png?mtime=1326760101" /></a></div><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/getting-rid-of-the-the">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/getting-rid-of-the-the#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=44</wfw:commentRss>
		</item>
				<item>
			<title>Writing to files from soapUI</title>
			<link>http://amadei.com.br/blog/index.php/writing-to-files-from-soapui</link>
			<pubDate>Wed, 14 Dec 2011 22:06:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="main">SOA &amp; Integration</category>
<category domain="alt">Unit Testing</category>
<category domain="alt">soapUI</category>			<guid isPermaLink="false">43@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;In this post I aim to show you how to write to files from a soapUI test case. This is really interesting when you have to change properties so that you can automate your tests and get free from manual file changes.
&lt;/p&gt;

&lt;p&gt;This was made easy by the possibility of running Groovy scripts as a test step and can help you to configure a parameter file or a properties file before running some piece of test.&lt;/p&gt;

&lt;p&gt;So, to see this in practice, create a new soapUI Workspace and a new empty Project inside it. Add a new Test Suite to the project:
&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/01-add-test-suite.png?mtime=1323607176&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/01-add-test-suite.png?mtime=1323607176&quot; width=&quot;328&quot; height=&quot;268&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;
And add a new Test Case to the Test Suite:
&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/02-add-test-case.png?mtime=1323607176&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/02-add-test-case.png?mtime=1323607176&quot; width=&quot;407&quot; height=&quot;134&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;
Add a new Groovy Script step to the test case:
&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/03-add-test-step-groovy.png?mtime=1323607176&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/03-add-test-step-groovy.png?mtime=1323607176&quot; width=&quot;486&quot; height=&quot;164&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Inside the Groovy Script Test Step, enter the following source code:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;br /&gt;
File file = new File(&quot;/tmp/myConfig.properties&quot;)&lt;br /&gt;
file.delete()&lt;br /&gt;
file &amp;lt;&amp;lt; (&quot;PROPERTY1=123&quot;)&lt;br /&gt;
file &amp;lt;&amp;lt; (&quot;\r\n&quot;)&lt;br /&gt;
file &amp;lt;&amp;lt; (&quot;PROPERTY2=456&quot;)&lt;br /&gt;
&lt;/code&gt;&lt;br /&gt;
This piece of code will:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;First open file at &amp;#8220;/tmp&amp;#8221; called myConfig.properties&lt;/li&gt;
  &lt;li&gt;Delete it (if it exists)&lt;/li&gt;
  &lt;li&gt;Then we write 2 key/value properties to the file, breaking the line between both of them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The source code should look like the following image:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/04-groovy-source.png?mtime=1323607988&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/04-groovy-source.png?mtime=1323607988&quot; width=&quot;624&quot; height=&quot;460&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, run the script and you should see an information alert showing it was successful. Go to the file system and open the /tmp/myConfig.properties file. You should see it was generated as expected like shown in the image bellow:&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://amadei.com.br/blog//media/blogs/blog/05-file-contents.png?mtime=1323608302&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://amadei.com.br/blog//media/blogs/blog/05-file-contents.png?mtime=1323608302&quot; width=&quot;690&quot; height=&quot;304&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;As you may have noticed, this was pretty easy and this possibility enables you to change properties so that your tests can avoid human intervention to change property and other kinds of files.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/writing-to-files-from-soapui&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>In this post I aim to show you how to write to files from a soapUI test case. This is really interesting when you have to change properties so that you can automate your tests and get free from manual file changes.
</p>

<p>This was made easy by the possibility of running Groovy scripts as a test step and can help you to configure a parameter file or a properties file before running some piece of test.</p>

<p>So, to see this in practice, create a new soapUI Workspace and a new empty Project inside it. Add a new Test Suite to the project:
</p>
<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/01-add-test-suite.png?mtime=1323607176"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/01-add-test-suite.png?mtime=1323607176" width="328" height="268" /></a></div>
<p><br /></p><p>
And add a new Test Case to the Test Suite:
</p>
<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/02-add-test-case.png?mtime=1323607176"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/02-add-test-case.png?mtime=1323607176" width="407" height="134" /></a></div>
<p><br /></p><p>
Add a new Groovy Script step to the test case:
</p>
<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/03-add-test-step-groovy.png?mtime=1323607176"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/03-add-test-step-groovy.png?mtime=1323607176" width="486" height="164" /></a></div>
<p><br /></p>
<p>Inside the Groovy Script Test Step, enter the following source code:</p>
<p><code><br />
File file = new File("/tmp/myConfig.properties")<br />
file.delete()<br />
file &lt;&lt; ("PROPERTY1=123")<br />
file &lt;&lt; ("\r\n")<br />
file &lt;&lt; ("PROPERTY2=456")<br />
</code><br />
This piece of code will:</p>
<ul>
  <li>First open file at &#8220;/tmp&#8221; called myConfig.properties</li>
  <li>Delete it (if it exists)</li>
  <li>Then we write 2 key/value properties to the file, breaking the line between both of them.</li>
</ul>

<p>The source code should look like the following image:</p>
<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/04-groovy-source.png?mtime=1323607988"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/04-groovy-source.png?mtime=1323607988" width="624" height="460" /></a></div>
<p><br /></p>

<p>Now, run the script and you should see an information alert showing it was successful. Go to the file system and open the /tmp/myConfig.properties file. You should see it was generated as expected like shown in the image bellow:</p>

<div class="image_block"><a href="http://amadei.com.br/blog//media/blogs/blog/05-file-contents.png?mtime=1323608302"><img alt="" src="http://amadei.com.br/blog//media/blogs/blog/05-file-contents.png?mtime=1323608302" width="690" height="304" /></a></div>
<p><br /></p>

<p>As you may have noticed, this was pretty easy and this possibility enables you to change properties so that your tests can avoid human intervention to change property and other kinds of files.</p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/writing-to-files-from-soapui">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/writing-to-files-from-soapui#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=43</wfw:commentRss>
		</item>
				<item>
			<title>Understanding Java Semaphores</title>
			<link>http://amadei.com.br/blog/index.php/understanding-java-semaphores</link>
			<pubDate>Fri, 21 Oct 2011 01:29:00 +0000</pubDate>			<dc:creator>damadei</dc:creator>
			<category domain="main">Java</category>			<guid isPermaLink="false">42@http://amadei.com.br/blog/</guid>
						<description>&lt;p&gt;The &lt;code&gt;java.util.concurrent.Semaphore&lt;/code&gt; class is a counting semaphore useful to implement throttling over a shared resource.&lt;/p&gt;

&lt;p&gt;The code bellow shows how to force only 5 threads to run concurrently even when we have 10 threads running.&lt;/p&gt;

&lt;pre class=&quot;prettyprint&quot;&gt;
package semaphore.tests;

import java.util.Date;
import java.util.concurrent.Semaphore;

public class SemaphoreTests {

  public static void main(String[] args) {
    Semaphore semaphore = new Semaphore(5);

    for (int i = 0; i &amp;lt; 10; i++) {
      new Worker(semaphore).start();
    }
  }
}

class Worker extends Thread {
  private Semaphore semaphore;

  public Worker(Semaphore semaphore) {
    this.semaphore = semaphore;
  }

  @Override
  public void run() {

    while (true) {
      try {
        semaphore.acquire();

        String msg = Thread.currentThread().getName() + &quot; - running at: &quot;
            + new Date();

        System.out.println(msg);

        Thread.sleep(10000);

      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        break;
      } finally {
        semaphore.release();
      }
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;
When running such code we see the following:
&lt;/p&gt;

&lt;pre&gt;
Thread-3 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-1 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-5 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011

Thread-1 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-5 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-6 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011

Thread-6 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-2 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-9 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;
Notice that five threads run concurrently, sleep for five seconds and then another five threads run concurrently again and sleep for five seconds more and so on...
&lt;/p&gt;
&lt;p&gt;
If you change the number of the Semaphore&#039;s permits to, for example, two permits, you should see only two threads running concurrently, sleeping for five seconds, then two more running and so on, proving that the &lt;code&gt;Semaphore&lt;/code&gt; is controlling the throttling.
&lt;/p&gt;
&lt;p&gt;
To change the number of permits, simply change the number that the Semaphore receives in its constructor: &lt;code&gt;Semaphore semaphore = new Semaphore(2);&lt;/code&gt; and you should see:
&lt;/p&gt;
&lt;pre&gt;
Thread-2 - running at: Thu Oct 20 23:54:17 GMT-03:00 2011
Thread-3 - running at: Thu Oct 20 23:54:17 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:27 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:54:27 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:37 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:54:37 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:47 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:54:47 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:57 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:54:57 GMT-03:00 2011
&lt;/pre&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;
We&amp;#8217;ve seen how to implement a throttling mechanism using the &lt;code&gt;Semaphore&lt;/code&gt; Java class. On the next post I&amp;#8217;ll show how the Semaphore class can be used to control access to a pool of resources.
&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://amadei.com.br/blog/index.php/understanding-java-semaphores&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://b2evolution.net/&quot;&gt;b2evolution&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>The <code>java.util.concurrent.Semaphore</code> class is a counting semaphore useful to implement throttling over a shared resource.</p>

<p>The code bellow shows how to force only 5 threads to run concurrently even when we have 10 threads running.</p>

<pre class="prettyprint">
package semaphore.tests;

import java.util.Date;
import java.util.concurrent.Semaphore;

public class SemaphoreTests {

  public static void main(String[] args) {
    Semaphore semaphore = new Semaphore(5);

    for (int i = 0; i &lt; 10; i++) {
      new Worker(semaphore).start();
    }
  }
}

class Worker extends Thread {
  private Semaphore semaphore;

  public Worker(Semaphore semaphore) {
    this.semaphore = semaphore;
  }

  @Override
  public void run() {

    while (true) {
      try {
        semaphore.acquire();

        String msg = Thread.currentThread().getName() + " - running at: "
            + new Date();

        System.out.println(msg);

        Thread.sleep(10000);

      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        break;
      } finally {
        semaphore.release();
      }
    }
  }
}
</pre>
<p><br /></p>
<p>
When running such code we see the following:
</p>

<pre>
Thread-3 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-1 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-5 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:38:48 GMT-03:00 2011

Thread-1 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-5 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:38:58 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-6 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:39:08 GMT-03:00 2011

Thread-6 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-2 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-9 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:39:18 GMT-03:00 2011
</pre>
<p><br /></p>
<p>
Notice that five threads run concurrently, sleep for five seconds and then another five threads run concurrently again and sleep for five seconds more and so on...
</p>
<p>
If you change the number of the Semaphore's permits to, for example, two permits, you should see only two threads running concurrently, sleeping for five seconds, then two more running and so on, proving that the <code>Semaphore</code> is controlling the throttling.
</p>
<p>
To change the number of permits, simply change the number that the Semaphore receives in its constructor: <code>Semaphore semaphore = new Semaphore(2);</code> and you should see:
</p>
<pre>
Thread-2 - running at: Thu Oct 20 23:54:17 GMT-03:00 2011
Thread-3 - running at: Thu Oct 20 23:54:17 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:27 GMT-03:00 2011
Thread-0 - running at: Thu Oct 20 23:54:27 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:37 GMT-03:00 2011
Thread-4 - running at: Thu Oct 20 23:54:37 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:47 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:54:47 GMT-03:00 2011

Thread-2 - running at: Thu Oct 20 23:54:57 GMT-03:00 2011
Thread-7 - running at: Thu Oct 20 23:54:57 GMT-03:00 2011
</pre>
<p><br /></p>
<p>
We&#8217;ve seen how to implement a throttling mechanism using the <code>Semaphore</code> Java class. On the next post I&#8217;ll show how the Semaphore class can be used to control access to a pool of resources.
</p><div class="item_footer"><p><small><a href="http://amadei.com.br/blog/index.php/understanding-java-semaphores">Original post</a> blogged on <a href="http://b2evolution.net/">b2evolution</a>.</small></p></div>]]></content:encoded>
								<comments>http://amadei.com.br/blog/index.php/understanding-java-semaphores#comments</comments>
			<wfw:commentRss>http://amadei.com.br/blog/index.php?tempskin=_rss2&#38;disp=comments&#38;p=42</wfw:commentRss>
		</item>
			</channel>
</rss>

