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

<channel>
	<title>Ashish's Tech Blog &#187; Spring</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Integrating Apache MINA with Spring</title>
		<link>http://www.ashishpaliwal.com/blog/2008/11/integrating-apache-mina-with-spring/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/11/integrating-apache-mina-with-spring/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 12:10:15 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=198</guid>
		<description><![CDATA[So far we have seen Apache MINA code samples in standalone form. Apache MINA can be nicely integrated into DI frameworks like Spring. Lets see how to integrate a simple MINA application with Spring This is how our application is structure. To see details of this application, please refer to the post Implementing Trap Receiver [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">So far we have seen Apache MINA code samples in standalone form. Apache MINA can be nicely integrated into DI frameworks like Spring. Lets see how to integrate a simple MINA application with Spring</p>
<p class="MsoNormal">This is how our application is structure. To see details of this application, please refer to the post <a href="http://www.ashishpaliwal.com/blog/2008/10/implementing-trap-receiver-in-30-minutes-using-apache-mina/">Implementing Trap Receiver in 30 minutes using Apache MINA</a></p>
<p>To integrate with Spring, we need to do following</p>
<ul>
<li>One Handler</li>
<li>Two Filter – Logging Filter and a ProtocolCodec Filter</li>
<li>NioDatagram Socket</li>
</ul>
<p class="MsoNormal">This is how our code looks like for the standalone application</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> initialize<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/io/IOException.html"><span style="color: #aaaadd; font-weight: bold;">IOException</span></a> <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">// Create an Acceptor</span>
	NioDatagramAcceptor acceptor = <span style="color: #000000; font-weight: bold;">new</span> NioDatagramAcceptor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Add Handler</span>
	acceptor.<span style="color: #006600;">setHandler</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ServerHandler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	acceptor.<span style="color: #006600;">getFilterChain</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addLast</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;logging&quot;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> LoggingFilter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	acceptor.<span style="color: #006600;">getFilterChain</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">addLast</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;codec&quot;</span>,
				<span style="color: #000000; font-weight: bold;">new</span> ProtocolCodecFilter<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SNMPCodecFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// Create Session Configuration</span>
	DatagramSessionConfig dcfg = acceptor.<span style="color: #006600;">getSessionConfig</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        dcfg.<span style="color: #006600;">setReuseAddress</span><span style="color: #66cc66;">&#40;</span><span style="color: #b13366;">true</span><span style="color: #66cc66;">&#41;</span>;
        logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Starting Server......&quot;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #808080; font-style: italic;">// Bind and be ready to listen</span>
        acceptor.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/InetSocketAddress.html"><span style="color: #aaaadd; font-weight: bold;">InetSocketAddress</span></a><span style="color: #66cc66;">&#40;</span>DEFAULT_PORT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Server listening on &quot;</span>+DEFAULT_PORT<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>To integrate with Spring, we need to do following</p>
<ol>
<li>Set the IO handler</li>
<li>Create the Filters and add to the chain</li>
<li>Create the Socket and set Socket Parameters</li>
</ol>
<div>
<p class="MsoNormal">NOTE: The latest MINA releases doesn’t have the package specific to Spring, like its earlier versions. The package is now named Integration Beans, to make the implementation work for all DI frameworks.</p>
<p class="MsoNormal">Lets see the Spring xml file. Please see that I have removed generic part from xml and have put only the specific things needed to pull up the implementation. This example has been derived from Chat example shipped with MINA release.</p>
<p class="MsoNormal">Now lets pull things together</p>
<ol type="1">
<li class="MsoNormal">Lets set the IO Handler</li>
<div><span style="text-decoration: underline;"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/filter.png"></a><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/handler.png"><img class="size-full wp-image-201 aligncenter" title="handler" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/handler.png" alt="" width="500" height="25" /></a><br />
</span></div>
<li class="MsoNormal">Lets  create the Filter chain</li>
<p style="text-align: center;"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/filter1.png"><img class="size-full wp-image-202 aligncenter" title="filter1" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/filter1.png" alt="" width="500" height="155" /></a></p>
<p>Here, we create instance of our IoFilter. See that for the ProtocolCodec factory, we have used Constructor injection. Logging Filter creation is straight forward. Once we have defined the beans for the filters to be used, we now create the Filter Chain to be used for the implementation. We define a bean with id “FilterChainBuidler” and add the defined filters to it. We are almost ready, and we just need to create the Socket and call bind</p>
<li class="MsoNormal"><span>Lets complete the last part of creating the Socket and completing the chain</span><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/acceptor.png"><img class="size-full wp-image-203 aligncenter" title="acceptor" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/11/acceptor.png" alt="" width="500" height="182" /></a></li>
</ol>
<p>Now we create our ioAcceptor, set IO handler and Filter Chain. Now we have to write a function to read this file using Spring and start our application. Here’s the code</p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> initializeViaSpring<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">new</span> ClassPathXmlApplicationContext<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;trapReceiverContext.xml&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>We just call this method from main, and this shall initialize our MINA application. Will try to write a  post using some other DI framework like Google Guice.</p>
<p><strong><em>Reference:</em></strong></p>
<ul>
<li><a href="http://svn.apache.org/viewvc/mina/branches/1.0/example/src/main/java/org/apache/mina/example/chat/">Chat Server</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/11/integrating-apache-mina-with-spring/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
