<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Java Quickly</title>
	<atom:link href="http://javaquickly.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://javaquickly.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 09 Jul 2009 12:09:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='javaquickly.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Java Quickly</title>
		<link>http://javaquickly.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://javaquickly.wordpress.com/osd.xml" title="Java Quickly" />
	<atom:link rel='hub' href='http://javaquickly.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Partial rule Matching in Drools</title>
		<link>http://javaquickly.wordpress.com/2009/07/09/partial-rule-matching-in-drools/</link>
		<comments>http://javaquickly.wordpress.com/2009/07/09/partial-rule-matching-in-drools/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:08:23 +0000</pubDate>
		<dc:creator>wzedan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Drools]]></category>
		<category><![CDATA[partial matching]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://javaquickly.wordpress.com/?p=25</guid>
		<description><![CDATA[This article discuesses two approaches for implementing partial rule matching in Drools<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=25&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Most RETE based rule engines don&#8217;t support partial rule matching, because the tree branches are bruned as soon as a condition is violated (either alpha or beta nodes)</p>
<p>We have tried two solutions for implementing partial matching</p>
<p>Single rule per risk      profile approach:</p>
<ul>
<li>Anded conditions are       defined in the object creation LHS part</li>
<li>Ored conditions are       defined using eval keyword as java objects</li>
<li>Helper classes were       used to perform comparison operations on BigInteger  , BigDecimal ,Date and String fields to       prevent null pointer exceptions for not supplied fact data</li>
<li>Scoring:
<ul>
<li>
<ul>
<li>Implemented by        accumulating the conditions score in a globally defined ArrayList within        the working memory for example ( c1 &amp;&amp; sums.add(new        condition(“cond1” , 15))</li>
<li>All the risk        profile is anded with another eval that invokes proceed method that        takes the sums and a threshold, that determines if a risk should be        inserted into the working memory or not</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Pros: Number of rules =       no. of risk profiles + 1</p>
<p>Cons:Extensive use of       eval keyword in scoring which removes any optimization done by Drools       engine (Alpha nodes sharing , indexing and early tree pruning)</p>
<ul>
<li><strong>Multiple rules for      each Ored or Scored conditions approach:</strong>
<ul>
<li>Anded conditions are       defined in the object creation LHS part</li>
<li>Ored conditions are       defined in different rules in the same activation group with salience       governing the conditions order</li>
<li>Scoring:
<ul>
<li>Each condition is        defined in a separate rule with salience = -1 so that it never fire        (accumulate rule is the only one allowed to fire)</li>
<li>An accumulate rule        is defined for each risk profile that invokes the proceed with the        accumulated conditions sums</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>Pros:Usage of Drools       optimization techniques</p>
<p>Cons: Number of rules =       no. of risk profiles * no.of ored conditions Or scored conditions + no.of       risk profiles + 1</p>
<p>for example if we have 2500 risk profiles, where 500 of them has on average 4 ored and 500 scored conditions (3 conditions each)= 2000 + 4 * 500 + 500 * 3 + 500 + 1 = 6001</p>
<p>For POC case most of the risk profiles contains ored conditions and scoring so for the 2500 risk profiles defined, about 7000 rules were generated.</p>
<p><strong>Performance comparison for both approaches</strong></p>
<p>Test was performed on local standalone OC4J with local Oracle XE database.</p>
<p>Binary packages for single rule per risk profile approach was used as it was not possible to increase the Java heap space more than 1GB on windows XP, thus multiple rules approach failed locally because of Java heap space exceptions.</p>
<p>Note:</p>
<p>This data are manually collected as different criteria are monitored (on first request response time, OC4J memory increases, after first request response time)</p>
<p>Memory Consumption:</p>
<ul>
<li>Single      Rule per risk profiles approach:
<ul>
<li>Binary       package size ~75MB</li>
<li>OC4J       needed 430-450MB to build a binary package of 2800 rules</li>
<li>Load       time ~32s to ~1 minute</li>
</ul>
</li>
<li>Multiple      rules approach:
<ul>
<li>Binary       package size &gt; 212MB (sum of single packages)</li>
<li>OC4J       needed &gt;1GB to build a DRL of 7000 rules</li>
<li>OC4J       needed &gt;1GB to build binary packages of sizes ranging from 20 to 90MB       (most of the times the OC4J fails to load)</li>
<li>Load       time from 7 to 10 minutes</li>
</ul>
</li>
</ul>
<p>Single Request Performance:</p>
<ul>
<li>First      time invocation average: 750-1000ms</li>
<li>Further      Requests average: 500-650ms</li>
<li>Memory      increased by about 20-30MB</li>
</ul>
<p>Six Concurrent Requests Performance</p>
<ul>
<li>First      time invocation average: &gt;5s</li>
<li>Further      Requests average: 1.5 – 2s</li>
</ul>
<ul>
<li>Memory      increased by about 50 – 60 MB</li>
</ul>
<p>Fifty concurrent requests Performance:</p>
<ul>
<li>Memory      increased by about 300MB</li>
<li>First      response arrival: ~4-6s</li>
<li>Last      response arrival: ~12-16s</li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaquickly.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaquickly.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaquickly.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaquickly.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaquickly.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaquickly.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaquickly.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaquickly.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=25&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaquickly.wordpress.com/2009/07/09/partial-rule-matching-in-drools/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a34549b23d0534e7ac60c0354843e5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">wzedan</media:title>
		</media:content>
	</item>
		<item>
		<title>Validation in GWT</title>
		<link>http://javaquickly.wordpress.com/2008/10/23/validation-in-gwt/</link>
		<comments>http://javaquickly.wordpress.com/2008/10/23/validation-in-gwt/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 09:58:47 +0000</pubDate>
		<dc:creator>wzedan</dc:creator>
				<category><![CDATA[GWT]]></category>

		<guid isPermaLink="false">http://javaquickly.wordpress.com/?p=15</guid>
		<description><![CDATA[Client Side Validation: DTO Class UserDto: public class UserDto implements Serializable { private int userId; private String userName; } Remote Service interface: public interface MyService extends RemoteService { public String saveUser(UserDto userDto); } Remote Service Asynchronous interface: public interface MyServiceAsync { public void saveUser(UserDto userDto, AsyncCallback&#60;String&#62; callback); } The client side call will be as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=15&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><!-- 	 	 --></p>
<p>Client 		Side Validation:</p>
<p>DTO Class UserDto:</p>
<p><strong>public</strong> <strong>class</strong> UserDto <strong>implements</strong> Serializable {</p>
<p><strong>private</strong> <strong>int</strong> userId;</p>
<p><strong>private</strong> String userName;</p>
<p><strong>}</strong></p>
<p>Remote Service interface:</p>
<p><strong>public</strong> <strong>interface</strong> MyService <strong>extends</strong> RemoteService {</p>
<p><strong>public</strong> String saveUser(UserDto userDto);</p>
<p>}</p>
<p>Remote Service Asynchronous interface:</p>
<p><strong>public</strong> <strong>interface</strong> MyServiceAsync {</p>
<p><strong>public</strong> <strong>void</strong> saveUser(UserDto userDto, AsyncCallback&lt;String&gt; callback);</p>
<p>}</p>
<p>The client side call will be as follows:</p>
<p>MyServiceAsync myService = MyService.Util.getInstance();</p>
<p>UserDto user = new UserDto();</p>
<p>user.setUserId(Integer.parseInt(userId.getText()));</p>
<p>user.setUserName(userName.getText());</p>
<p>myService.saveUser(user, new AsyncCallback&lt;String&gt;() {</p>
<p>&#8230;</p>
<p>So when user for example enters string in user id, a number format exception will be raised which can be caught and appropriate error messages can be displayed. For debugging purposes the exception details are displayed in FireBug and the Hosted mode server console. (<em>see GWT documentation for the emulated java exceptions</em>).</p>
<p>Server 		side Validation:</p>
<p>Invalid 			input types:</p>
<p>The serialized request sent to the RPC service is mentioned as follows:</p>
<p><span style="text-decoration:underline;"><a href="http://host:8888/TestValidation/?485975FDB961BEAB5B5D6F881FE">http://host:8888/TestValidation/?485975FDB961BEAB5B5D6F881FE</a></span>9205A?demo.client.MyService?saveUser?demo.client.UserDto?demo.client.UserDto/1050776168?<strong>text</strong>?1?2?3?4?1?5?6?<strong>2342</strong>?7?</p>
<p>The dispatcher Servlet uses the interface name and method requested to get the bean implementing the requested interface, by using reflection, it gets the method parameters</p>
<p>By using ServerSerializationStreamReader utility class, it de-serializes the request parameter to the actual method parameter:</p>
<p>ServerSerializationStreamReader.deserializeValue(parameterTypes[i]);</p>
<p>The ServerSerializationStreamReader class, validates the parameters for its compatibility with method parameter types, and create the method parameters. If invalid parameters are sent, a serialization exception is sent back to the client (internal server error (code: 500)</p>
<p>Business 			constraint violations:</p>
<p>Since GWT handles the incorrect types automatically (String instead of integer for example), the business constraints on data transfer objects should be handled by the service invocation layer (RPC Handler)</p>
<ul>
<li>Approach 	1:
<ul>
<li>Using 		third party validation party (for example Oval 		http://oval.sourceforge.net/) which allows defining annotations on 		the client DTO</li>
</ul>
</li>
<li>Approach 	2:
<ul>
<li>Validating 		the input DTO manually by the service invocation layer</li>
</ul>
</li>
</ul>
<p>In order to allow third party annotations to be recognized by the GWT Compiler, the library should be included on the class path of the GWT compiler; this ant script illustrates the idea</p>
<p>&lt;path id=&#8221;classpath&#8221;&gt;<br />
&lt;pathelement location=&#8221;${gwtpath}/gwt-user.jar&#8221; /&gt;<br />
&lt;pathelement location=&#8221;${gwtpath}/gwt-dev-windows.jar&#8221; /&gt;<br />
&lt;pathelement location=&#8221;${src.dir}&#8221; /&gt;<br />
&lt;fileset dir=&#8221;${lib.dir}&#8221;&gt;<br />
&lt;include name=&#8221;**/*.jar&#8221; /&gt;<br />
&lt;/fileset&gt;<br />
&lt;/path&gt;</p>
<p>&lt;target name=&#8221;compile-gwt&#8221; depends=&#8221;init&#8221;&gt;<br />
&lt;java classname=&#8221;com.google.gwt.dev.GWTCompiler&#8221; fork=&#8221;true&#8221;&gt;<br />
&lt;classpath refid=&#8221;classpath&#8221; /&gt;<br />
&lt;jvmarg value=&#8221;-Xmx256M&#8221; /&gt;<br />
&lt;arg value=&#8221;-out&#8221; /&gt;<br />
&lt;arg value=&#8221;${www.dir}&#8221; /&gt;<br />
&lt;arg value=&#8221;${gwt.module}&#8221; /&gt;<br />
&lt;/java&gt;<br />
&lt;/target&gt;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaquickly.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaquickly.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaquickly.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaquickly.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaquickly.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaquickly.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaquickly.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaquickly.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=15&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaquickly.wordpress.com/2008/10/23/validation-in-gwt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a34549b23d0534e7ac60c0354843e5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">wzedan</media:title>
		</media:content>
	</item>
		<item>
		<title>JBoss and JMS</title>
		<link>http://javaquickly.wordpress.com/2008/10/17/jboss-and-jms/</link>
		<comments>http://javaquickly.wordpress.com/2008/10/17/jboss-and-jms/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 08:02:06 +0000</pubDate>
		<dc:creator>wzedan</dc:creator>
				<category><![CDATA[JBoss]]></category>

		<guid isPermaLink="false">http://javaquickly.wordpress.com/?p=21</guid>
		<description><![CDATA[Introduction The JMS specification describes a set of programming interfaces that support distributed, enterprise messaging. An enterprise messaging systems enables independent distributed components or applications to interact through messages. In the messaging world, messages are not sent directly to other applications. Instead, messages are sent to destinations, known as queues or topics. JMS specification defines [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=21&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><!-- 	 	 --></p>
<p>Introduction</p>
<p>The JMS specification describes a set of programming interfaces that support distributed, enterprise messaging. An enterprise messaging systems enables independent distributed components or applications to interact through messages.</p>
<p>In the messaging world, messages are not sent directly to other applications. Instead, messages are sent to destinations, known as queues or topics.</p>
<p>JMS specification defines two Messaging paradigms:</p>
<ul>
<li>Point To Point (PTP)
<ul>
<li>It uses the message Queue 		concept.</li>
<li>Messages are stored in a Queue 		destination.</li>
<li>The message has only one 		consumer.</li>
</ul>
</li>
<li>Publish Subscribe:
<ul>
<li>It uses a Topic to send and 		receive messages.</li>
<li>Each message can have multiple 		consumers.</li>
</ul>
</li>
</ul>
<p>Administrator is responsible for defining the destination Queue/Topic with their JNDI names.</p>
<h1>JMS Application Components</h1>
<h2>JMS Clients</h2>
<p>Java program that send / receive Messages. Clients can be standalone java applications or Enterprise Java Beans capable of receiving messages asynchronously (Message Driven beans)</p>
<h2>Messages</h2>
<h2>Administered Objects</h2>
<p>These are objects created on the Application server. They include the Connection Factory, the topic or queue destinations.</p>
<h2>JMS Provider</h2>
<p>Known as a Message Oriented Middleware (MOM) system.</p>
<p>JBoss comes with a JMS 1.1 compliant JMS provider called JBoss Messaging or JBossMQ</p>
<h1>Setup JMS Connections in JBoss</h1>
<p>A connection is needed to the destination Queue/Topic, this can be done using JNDI lookup, but we need first to get the initial context which is the entry point in any JNDI lookup operation.</p>
<p>Hashtable environment = new Hashtable();</p>
<p>environment.put(Context.INITIAL_CONTEXT_FACTORY,</p>
<p>&#8220;org.jnp.interfaces.NamingContextFactory&#8221;);</p>
<p>environment.put(Context.URL_PKG_PREFIXES,</p>
<p>&#8220;org.jboss.naming:org.jnp.interfaces&#8221;);</p>
<p>environment.put(Context.PROVIDER_URL, &#8220;jnp://localhost:1099&#8243;);</p>
<p>InitialContext iniCtx = new InitialContext(environment);</p>
<p>The next step is to create a connection to the destination Queue/Topic</p>
<p>Object tmp = iniCtx.lookup(&#8220;ConnectionFactory&#8221;);</p>
<p>Then depending on whether we want a Queue or Topic destination we type cast the connection factory object was lookup to either QueueConnectionFactory or TopicConnectionFactory object.</p>
<p>QueueConnectionFactory qcf = (QueueConnectionFactory) tmp;</p>
<p>javax.jms.QueueConnection = qcf.createQueueConnection();</p>
<p>javax.jms.Queue = (Queue) iniCtx.lookup(&#8220;queue/testQueue&#8221;);</p>
<p>javax.jms.QueueSession = conn.createQueueSession(false,                                          QueueSession.AUTO_ACKNOWLEDGE);</p>
<p>Note that Queues/Topics should be prefixed with queue or topic words.</p>
<h1>Create a JMS sender</h1>
<p>The Session interface defines different types of messages that can be sent.</p>
<h2>PTP Sender</h2>
<p>Javax.jms.QueueSender  = session.createSender(javax.jms.Queue);</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaquickly.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaquickly.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaquickly.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaquickly.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaquickly.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaquickly.wordpress.com/21/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaquickly.wordpress.com/21/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaquickly.wordpress.com/21/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=21&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaquickly.wordpress.com/2008/10/17/jboss-and-jms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a34549b23d0534e7ac60c0354843e5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">wzedan</media:title>
		</media:content>
	</item>
		<item>
		<title>Springfying GWT</title>
		<link>http://javaquickly.wordpress.com/2008/10/17/springfying-gwt/</link>
		<comments>http://javaquickly.wordpress.com/2008/10/17/springfying-gwt/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 06:52:16 +0000</pubDate>
		<dc:creator>wzedan</dc:creator>
				<category><![CDATA[GWT]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[GWT Spring]]></category>

		<guid isPermaLink="false">http://javaquickly.wordpress.com/?p=3</guid>
		<description><![CDATA[Google toolkit allows usage of Spring framework in the server side. The goal of this approach is to decouple the business interface (defined in the client side) from the web interface. Step 1: Create business interface created in the client package that extends RemoteService interface package demo.client; public interface Test extends RemoteService { public static [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=3&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Google toolkit allows usage of Spring framework in the server side. The<br />
goal of this approach is to decouple the business interface (defined in the<br />
client side) from the web interface.</p>
<p>Step 1:</p>
<p>Create business interface created in the client package that extends<br />
RemoteService interface</p>
<p><code>package demo.client;</code></p>
<p><code>public interface Test extends RemoteService {</code></p>
<p style="padding-left:30px;"><code>public static final String SERVICE_URI = "RPCHandler";</code></p>
<p style="padding-left:30px;"><code>public static class Util {</code></p>
<p style="padding-left:30px;">
<p style="padding-left:60px;"><code>public static TestAsync getInstance() {</code></p>
<p style="padding-left:60px;">
<p style="padding-left:60px;"><code>TestAsync instance = (TestAsync) GWT.create(Test.class);</code></p>
<p style="padding-left:60px;"><code>ServiceDefTarget target = (ServiceDefTarget) instance;</code></p>
<p style="padding-left:60px;"><code>target.setServiceEntryPoint(GWT.getModuleBaseURL() +<br />
SERVICE_URI);</code></p>
<p style="padding-left:60px;"><code>return instance;</code></p>
<p style="padding-left:60px;"><code>}</code></p>
<p style="padding-left:30px;"><code>}</code></p>
<p style="padding-left:30px;"><code>public String sayHello(String name);</code></p>
<p><code>}</code></p>
<p>Step 2:</p>
<p>Create the asynchronous business interface (e.g. MyServiceAsycn.java).</p>
<p>(Note: The &lt;interfaceName&gt;Async naming convention is important for the GWT compiler)</p>
<p><code>package demo.client;</code></p>
<p><code>public interface TestAsync {</code></p>
<p style="padding-left:30px;"><code>public void sayHello(String s, AsyncCallback&lt;String&gt; callback);</code></p>
<p><code>}</code></p>
<p>Step 3:</p>
<p style="text-align:justify;">The dispatcher Servlet acts as the controller to invoke the services implementations that are defined as spring beans. The dispatcher Servlet uses</p>
<p style="text-align:justify;">WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean(interfaceName)</p>
<p style="text-align:justify;">to get the requested service implementation bean. The dispatcher Servlet design follows these steps:</p>
<p style="text-align:justify;">Create an ordinary Servlet to handle all GWT RPC calls, and then Servlet<br />
implementation is responsible for invoking the requested service<br />
method.</p>
<p>Define the service implementation as a spring bean to implement the<br />
business interface defined at the client.</p>
<p><code>package demo.server;</code></p>
<p><code>@Transactional</code></p>
<p><code>public class HandlerService implements Test {</code></p>
<p style="padding-left:30px;"><code>public String sayHello(String name) {</code></p>
<p style="padding-left:30px;"><code>return "Hello " + name;</code></p>
<p style="padding-left:30px;"><code>}</code></p>
<p>Use RPC.decodeRequest utility class to get the RPC request, which contains<br />
the business interface name , method to be invoked and parameters, the<br />
encoded request looks similar to this pattern:</p>
<p>http://host:8888/TestValidation/?485975FDB961BEAB5B5D6F881FE9205A?<span style="color:#ff0000;">demo.client.MyService</span>?<span style="color:#0000ff;">saveUser</span></p>
<p>?demo.client.UserDto?<span style="color:#008000;">demo.client.UserDto</span>/1050776168?text?1?2?3?4?1?5?6?2342?7?</p>
<p>where,</p>
<p><span style="color:#ff0000;">demo.client.MyService</span>: Interface name</p>
<p><span style="color:#0000ff;">saveUser</span>: Method to be invoked</p>
<p><span style="color:#008000;">demo.client.UserDto</span>: method parameters object type(s)</p>
<p>Service implementation invocation design approaches:</p>
<ul>
<li>The service implementation is defined as a Spring bean , with an ID<br />
equals to the name of the interface</li>
<li>The service implementation is defined as a Spring bean with id equals<br />
to the name of the interface and method (&lt;interface<br />
FQN&gt;.methodname;</li>
</ul>
<p>The RPC Handler Servlet ,invokes the method requested in the payload on<br />
the retrieved spring bean</p>
<p>The RPC Handler uses the utility classes that comes with the GWT library<br />
(RPC , RPCServletUtil …)</p>
<p style="text-align:left;"><code>protected void doPost(HttpServletRequest httpRequest,HttpServletResponse httpResponse) throws ServletException,IOException {</code></p>
<p style="padding-left:30px;text-align:left;"><code>String payload =<br />
RPCServletUtils.readContentAsUtf8(httpRequest);</code></p>
<p style="padding-left:30px;text-align:left;"><code>Object service = null;</code></p>
<p style="padding-left:30px;text-align:left;"><code> </code></p>
<p style="padding-left:30px;text-align:left;"><code>String responsePayload = null;</code></p>
<p style="padding-left:30px;text-align:left;"><code>RPCRequest rpcRequest = RPC.decodeRequest(payload);</code></p>
<p style="padding-left:30px;text-align:left;"><code>Class clazz = rpcRequest.getMethod().getDeclaringClass();</code></p>
<p style="padding-left:30px;text-align:left;"><code>service = getService(clazz.getName());</code></p>
<p style="padding-left:30px;text-align:left;"><code>responsePayload = RPC.invokeAndEncodeResponse(service,</code><code> rpcRequest.getMethod(), rpcRequest.getParameters(), rpcRequest.getSerializationPolicy());</code></p>
<p style="padding-left:30px;text-align:left;"><code>boolean gzipEncode = false;</code></p>
<p style="padding-left:30px;text-align:left;"><code>RPCServletUtils.writeResponse(getServletContext(),httpResponse,responsePayload, gzipEncode);</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/javaquickly.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/javaquickly.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/javaquickly.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/javaquickly.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/javaquickly.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/javaquickly.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/javaquickly.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/javaquickly.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=javaquickly.wordpress.com&amp;blog=5194893&amp;post=3&amp;subd=javaquickly&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://javaquickly.wordpress.com/2008/10/17/springfying-gwt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6a34549b23d0534e7ac60c0354843e5c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">wzedan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
