You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by bu...@apache.org on 2012/11/15 16:28:09 UTC

svn commit: r838443 - in /websites/staging/mina/trunk/content: ./ mina-project/quick-start-guide.html

Author: buildbot
Date: Thu Nov 15 15:28:09 2012
New Revision: 838443

Log:
Staging update by buildbot for mina

Added:
    websites/staging/mina/trunk/content/mina-project/quick-start-guide.html
Modified:
    websites/staging/mina/trunk/content/   (props changed)

Propchange: websites/staging/mina/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Thu Nov 15 15:28:09 2012
@@ -1 +1 @@
-1409816
+1409833

Added: websites/staging/mina/trunk/content/mina-project/quick-start-guide.html
==============================================================================
--- websites/staging/mina/trunk/content/mina-project/quick-start-guide.html (added)
+++ websites/staging/mina/trunk/content/mina-project/quick-start-guide.html Thu Nov 15 15:28:09 2012
@@ -0,0 +1,364 @@
+<!DOCTYPE html>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<html>
+<head>
+	<title>Quick Start Guide &mdash; Apache MINA</title>
+	
+		<link href="./../css/common.css" rel="stylesheet" type="text/css">
+		<link href="./../css/mina.css" rel="stylesheet" type="text/css">
+	
+</head>
+  <body>
+    <div id="container">
+      <div id="header">
+        <div id="subProjectsNavBar">
+          <a href="./../">
+            
+              Apache MINA Project
+            
+          </a>
+          &nbsp;|&nbsp;
+          <a href="./../mina-project/index.html">
+            
+              <strong>MINA</strong>
+            
+          </a>
+          &nbsp;|&nbsp;
+          <a href="./../asyncweb-project/index.html">
+            
+              AsyncWeb
+            
+          </a>
+          &nbsp;|&nbsp;
+          <a href="./../ftpserver-project/index.html">
+            
+              FtpServer
+            
+          </a>
+          &nbsp;|&nbsp;
+          <a href="./../sshd-project/index.html">
+            
+              SSHD
+            
+          </a>
+          &nbsp;|&nbsp;
+          <a href="./../vysper-project/index.html">
+            
+              Vysper
+            
+          </a>
+        </div><!-- subProjectsNavBar -->
+      </div><!-- header -->
+      <div id="content">
+        <div id="leftColumn">
+                              
+          
+          
+          
+          
+            <div id="navigation">
+
+    <h5>Latest Downloads</h5>
+    <ul>
+        <li><a href="./../mina-project/downloads.html">Mina 2.0.7</a></li>
+    </ul>
+    <h5>Documentation</h5>
+    <ul>
+      <li><a href="./../mina-project/userguide/user-guide-toc.html" class="external-link" rel="nofollow">User guide</a></li>
+      <li><a href="./../mina-project/features.html" class="external-link" rel="nofollow">Features</a></li>
+      <li><a href="./../mina-project/road-map.html" class="external-link" rel="nofollow">Road Map</a></li>
+      <li><a href="./../mina-project/faq.html" class="external-link" rel="nofollow">FAQ</a></li>
+    </ul>
+    <h5>Resources</h5>
+    <ul>
+      <li><a href="./../mina-project/downloads.html" class="external-link" rel="nofollow">Downloads</a></li>
+      <li><a href="./../mina-project/mailing-lists.html" class="external-link" rel="nofollow">Mailing lists &amp; IRC</a></li>
+      <li><a href="./../mina-project/issue-tracking.html" class="external-link" rel="nofollow">Issue tracking</a></li>
+      <li><a href="./../mina-project/sources.html" class="external-link" rel="nofollow">Sources</a></li>
+      <li><a href="./../mina-project/javadocs/index.html" class="external-link" rel="nofollow">API Javadoc</a></li>
+      <li><a href="./../mina-project/performances.html" class="external-link" rel="nofollow">Performances</a></li>
+      <li><a href="./../mina-project/testimonials.html" class="external-link" rel="nofollow">Testimonials</a></li>
+      <li><a href="./../mina-project/conferences.html" class="external-link" rel="nofollow">Conferences</a></li>
+      <li><a href="./../mina-project/developper-guide.html" class="external-link" rel="nofollow">Developers Guide</a></li>
+      <li><a href="./../mina-project/related-projects.html" class="external-link" rel="nofollow">Related Projects</a></li>
+      <li><a href="http://people.apache.org/~vgritsenko/stats/projects/mina.html" class="external-link" rel="nofollow">Statistics</a></li>
+    </ul>
+
+    <h5>Community</h5>
+    <ul>
+      <li><a href="http://www.apache.org/foundation/contributing.html" class="external-link" rel="nofollow">Contributing</a></li>
+      <li><a href="./../contributors.html" class="external-link" rel="nofollow">Team</a></li>
+      <li><a href="./../special-thanks.html" class="external-link" rel="nofollow">Special Thanks</a></li>
+      <li><a href="http://www.apache.org/security/" class="external-link" rel="nofollow">Security</a></li>
+    </ul>
+
+    <h5>About Apache</h5>
+    <ul>
+      <li><a href="http://www.apache.org" class="external-link" rel="nofollow">Apache main site</a></li>
+      <li><a href="http://www.apache.org/licenses/" class="external-link" rel="nofollow">License</a></li>
+      <li><a href="http://www.apache.org/foundation/sponsorship.html" title="The ASF sponsorship program" class="external-link" rel="nofollow">Sponsorship program</a></li>
+      <li><a href="http://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">Thanks</a></li>
+    </ul>
+
+    <h3><a name="Navigation-Upcoming"></a>Upcoming</h3>
+    <ul>
+      <li>No event</li>
+    </ul>
+
+</div>
+
+<!-- navigation -->
+          
+          
+        </div><!-- leftColumn -->
+      <div id="rightColumn">
+
+
+
+
+<h1 id="quick-start-guide">Quick Start Guide</h1>
+<p>This tutorial will walk you through the process of building a MINA based program. This tutorial will walk through building a time server. The following prerequisites are required for this tutorial:
+<em> MINA 2.0.7 Core
+</em> JDK 1.5 or greater
+<em> [SLF4J|http://www.slf4j.org/] 1.3.0 or greater
+<strong> <em>Log4J 1.2</em> users: <em>slf4j-api.jar</em>, <em>slf4j-log4j12.jar</em>, and [Log4J|http://logging.apache.org/log4j/docs/] 1.2.x</strong> </em>Log4J 1.3<em> users: <em>slf4j-api.jar</em>, <em>slf4j-log4j13.jar</em>, and [Log4J|http://logging.apache.org/log4j/docs/] 1.3.x
+<strong> <em>{<em>}java.util.logging{</em></em>} users: <em>slf4j-api.jar</em> and <em>slf4j-jdk14.jar</em></strong> </em>IMPORTANT<em>: Please make sure you are using the right _slf4j-</em>.jar_ that matches to your logging framework.\
+ For instance, <em>slf4j-log4j12.jar</em> and <em>log4j-1.3.x.jar</em> can not be used together, and will malfunction.</p>
+<p>I have tested this program on both Windows© 2000 professional and linux. If you have any problems getting this program to work, please do not hesitate to [contact us|Contact] in order to talk to the MINA developers. Also, this tutorial has tried to remain independent of development environments (IDE, editors..etc). This tutorial will work with any environment that you are comfortable with. Compilation commands and steps to execute the program have been removed for brevity. If you need help learning how to either compile of execute java programs, please consult the [Java tutorial|http://java.sun.com/docs/books/tutorial/].</p>
+<h2 id="writing-the-mina-time-server">Writing the MINA time server</h2>
+<p>We will begin by creating a file called MinaTimeServer.java. The initial code can be found below:
+    :::Java
+    public class MinaTimeServer {</p>
+<div class="codehilite"><pre>    <span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
+        <span class="sr">//</span> <span class="n">code</span> <span class="n">will</span> <span class="n">go</span> <span class="n">here</span> <span class="k">next</span>
+    <span class="p">}</span>
+<span class="p">}</span>
+</pre></div>
+
+
+<p>This code should be straightforward to all. We are simply defining a main method that will be used to kick off the program. At this point, we will begin to add the code that will make up our server. First off, we need an object that will be used to listen for incoming connections. Since this program will be TCP/IP based, we will add a <em>SocketAcceptor</em> to our program.</p>
+<div class="codehilite"><pre><span class="kn">import</span> <span class="nn">org.apache.mina.common.ByteBuffer</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.IoAcceptor</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.SimpleByteBufferAllocator</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.transport.socket.nio.SocketAcceptor</span><span class="o">;</span>
+
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MinaTimeServer</span> <span class="o">{</span>
+
+    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
+        <span class="c1">// The following two lines change the default buffer type to &#39;heap&#39;,</span>
+        <span class="c1">// which yields better performance.c</span>
+        <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">setUseDirectBuffers</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
+        <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">setAllocator</span><span class="o">(</span><span class="k">new</span> <span class="n">SimpleByteBufferAllocator</span><span class="o">());</span>
+
+        <span class="n">IoAcceptor</span> <span class="n">acceptor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SocketAcceptor</span><span class="o">();</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>With the <strong>SocketAcceptor</strong> class in place, we can go ahead and define the handler class and bind the <em>SocketAcceptor</em> to a port. If you are interested in adding a thread model to the <em>SocketAcceptor</em>, please read the [Configuring Thread Model] tutorial.
+We will now add in the <em>SocketAcceptor</em> configuration.  This will allow us to make socket-specific settings for the socket that will be used to accept connections from clients.</p>
+<div class="codehilite"><pre><span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">java.nio.charset.Charset</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.ByteBuffer</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.IoAcceptor</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.SimpleByteBufferAllocator</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.filter.LoggingFilter</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.filter.codec.ProtocolCodecFilter</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.filter.codec.textline.TextLineCodecFactory</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.transport.socket.nio.SocketAcceptor</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.transport.socket.nio.SocketAcceptorConfig</span><span class="o">;</span>
+
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MinaTimeServer</span> <span class="o">{</span>
+
+    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">PORT</span> <span class="o">=</span> <span class="mi">9123</span><span class="o">;</span>
+
+    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
+        <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">setUseDirectBuffers</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
+        <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">setAllocator</span><span class="o">(</span><span class="k">new</span> <span class="n">SimpleByteBufferAllocator</span><span class="o">());</span>
+
+        <span class="n">IoAcceptor</span> <span class="n">acceptor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SocketAcceptor</span><span class="o">();</span>
+
+        <span class="n">SocketAcceptorConfig</span> <span class="n">cfg</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SocketAcceptorConfig</span><span class="o">();</span>
+        <span class="n">cfg</span><span class="o">.</span><span class="na">getFilterChain</span><span class="o">().</span><span class="na">addLast</span><span class="o">(</span> <span class="s">&quot;logger&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">LoggingFilter</span><span class="o">()</span> <span class="o">);</span>
+        <span class="n">cfg</span><span class="o">.</span><span class="na">getFilterChain</span><span class="o">().</span><span class="na">addLast</span><span class="o">(</span> <span class="s">&quot;codec&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">ProtocolCodecFilter</span><span class="o">(</span> <span class="k">new</span> <span class="n">TextLineCodecFactory</span><span class="o">(</span> <span class="n">Charset</span><span class="o">.</span><span class="na">forName</span><span class="o">(</span> <span class="s">&quot;UTF-8&quot;</span> <span class="o">))));</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Here was have created a new instance of the <em>SocketAcceptorConfig</em> class that will be used to pass in to the acceptor once we are ready to start up the acceptor.  First, we have set the reuse address flag.  See more information about this in the [JDK Documentation|http://java.sun.com/javase/6/docs/api/java/net/Socket.html#setReuseAddress(boolean)].  Next we add a filter to the configuration.  This filter will log all information such as newly created sessions, messages received, messages sent, session closed.  The next filter is a <em>ProtocolCodecFilter</em>.  This filter will translate binary or protocol specific data into message object and vice versa.</p>
+<p>This last addition will bind the acceptor to the port.  This method will signal the startup of the server process.  Without this method call, the server will not service client connections.</p>
+<div class="codehilite"><pre><span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">java.net.InetSocketAddress</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">java.nio.charset.Charset</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.ByteBuffer</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.IoAcceptor</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.SimpleByteBufferAllocator</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.filter.LoggingFilter</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.filter.codec.ProtocolCodecFilter</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.filter.codec.textline.TextLineCodecFactory</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.transport.socket.nio.SocketAcceptor</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.transport.socket.nio.SocketAcceptorConfig</span><span class="o">;</span>
+
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">MinaTimeServer</span> <span class="o">{</span>
+
+    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">PORT</span> <span class="o">=</span> <span class="mi">9123</span><span class="o">;</span>
+
+    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">IOException</span> <span class="o">{</span>
+        <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">setUseDirectBuffers</span><span class="o">(</span><span class="kc">false</span><span class="o">);</span>
+        <span class="n">ByteBuffer</span><span class="o">.</span><span class="na">setAllocator</span><span class="o">(</span><span class="k">new</span> <span class="n">SimpleByteBufferAllocator</span><span class="o">());</span>
+
+        <span class="n">IoAcceptor</span> <span class="n">acceptor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SocketAcceptor</span><span class="o">();</span>
+
+        <span class="n">SocketAcceptorConfig</span> <span class="n">cfg</span> <span class="o">=</span> <span class="k">new</span> <span class="n">SocketAcceptorConfig</span><span class="o">();</span>
+        <span class="n">cfg</span><span class="o">.</span><span class="na">getFilterChain</span><span class="o">().</span><span class="na">addLast</span><span class="o">(</span> <span class="s">&quot;logger&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">LoggingFilter</span><span class="o">()</span> <span class="o">);</span>
+        <span class="n">cfg</span><span class="o">.</span><span class="na">getFilterChain</span><span class="o">().</span><span class="na">addLast</span><span class="o">(</span> <span class="s">&quot;codec&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">ProtocolCodecFilter</span><span class="o">(</span> <span class="k">new</span> <span class="n">TextLineCodecFactory</span><span class="o">(</span> <span class="n">Charset</span><span class="o">.</span><span class="na">forName</span><span class="o">(</span> <span class="s">&quot;UTF-8&quot;</span> <span class="o">))));</span>
+
+        <span class="n">acceptor</span><span class="o">.</span><span class="na">bind</span><span class="o">(</span> <span class="k">new</span> <span class="n">InetSocketAddress</span><span class="o">(</span><span class="n">PORT</span><span class="o">),</span> <span class="k">new</span> <span class="n">TimeServerHandler</span><span class="o">(),</span> <span class="n">cfg</span><span class="o">);</span>
+        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;MINA Time server started.&quot;</span><span class="o">);</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>What you see here is that we have defined a variable port of type integer and made a call to SocketAcceptor.bind(SocketAddress,IoHandler). The first parameter is the <em>SocketAddress</em> that describes the network address that will be listening on, in this case port 9123, and the local address.</p>
+<p>The second parameter passed to the bind method is a class that must implement the interface <em>IoHandler</em>. For almost all programs that use MINA, this becomes the workhorse of the program, as it services all incoming requests from the clients. For this tutorial, we will extend the class <em>IoHandlerAdapter</em>. This is a class that follows the [adapter design pattern|http://en.wikipedia.org/wiki/Adapter_pattern] which simplifies the amount of code that needs to be written in order to satisfy the requirement of passing in a class that implements the <em>IoHandler</em> interface.</p>
+<p>The third parameter is the configuration object, cfg, which has been configured with a logger filter and a codec filter. MINA is set up such that each message that is received will be passed through any and all filters in the filter chain defined for the <em>IoAcceptor</em>. In this case, we will pass all messages through a logging filter and then a codec filter. The logging filter will simply log the message using the SL4J library, and the codec filter will decode each message received and encode each message sent using the supplied <em>TextLineCodecFactory</em> class. </p>
+<p>Below is the class TimeServerHandler:</p>
+<div class="codehilite"><pre><span class="kn">import</span> <span class="nn">java.util.Date</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.IdleStatus</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.IoHandlerAdapter</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.IoSession</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.common.TransportType</span><span class="o">;</span>
+<span class="kn">import</span> <span class="nn">org.apache.mina.transport.socket.nio.SocketSessionConfig</span><span class="o">;</span>
+
+<span class="kd">public</span> <span class="kd">class</span> <span class="nc">TimeServerHandler</span> <span class="kd">extends</span> <span class="n">IoHandlerAdapter</span> <span class="o">{</span>
+    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">exceptionCaught</span><span class="o">(</span><span class="n">IoSession</span> <span class="n">session</span><span class="o">,</span> <span class="n">Throwable</span> <span class="n">t</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
+        <span class="n">t</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span>
+        <span class="n">session</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">messageReceived</span><span class="o">(</span><span class="n">IoSession</span> <span class="n">session</span><span class="o">,</span> <span class="n">Object</span> <span class="n">msg</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
+        <span class="n">String</span> <span class="n">str</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="na">toString</span><span class="o">();</span>
+        <span class="k">if</span><span class="o">(</span> <span class="n">str</span><span class="o">.</span><span class="na">trim</span><span class="o">().</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="s">&quot;quit&quot;</span><span class="o">)</span> <span class="o">)</span> <span class="o">{</span>
+            <span class="n">session</span><span class="o">.</span><span class="na">close</span><span class="o">();</span>
+            <span class="k">return</span><span class="o">;</span>
+        <span class="o">}</span>
+
+        <span class="n">Date</span> <span class="n">date</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Date</span><span class="o">();</span>
+        <span class="n">session</span><span class="o">.</span><span class="na">write</span><span class="o">(</span> <span class="n">date</span><span class="o">.</span><span class="na">toString</span><span class="o">()</span> <span class="o">);</span>
+        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Message written...&quot;</span><span class="o">);</span>
+    <span class="o">}</span>
+
+    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">sessionCreated</span><span class="o">(</span><span class="n">IoSession</span> <span class="n">session</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
+        <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Session created...&quot;</span><span class="o">);</span>
+
+        <span class="k">if</span><span class="o">(</span> <span class="n">session</span><span class="o">.</span><span class="na">getTransportType</span><span class="o">()</span> <span class="o">==</span> <span class="n">TransportType</span><span class="o">.</span><span class="na">SOCKET</span> <span class="o">)</span>
+            <span class="o">((</span><span class="n">SocketSessionConfig</span><span class="o">)</span> <span class="n">session</span><span class="o">.</span><span class="na">getConfig</span><span class="o">()</span> <span class="o">).</span><span class="na">setReceiveBufferSize</span><span class="o">(</span> <span class="mi">2048</span> <span class="o">);</span>
+
+        <span class="n">session</span><span class="o">.</span><span class="na">setIdleTime</span><span class="o">(</span> <span class="n">IdleStatus</span><span class="o">.</span><span class="na">BOTH_IDLE</span><span class="o">,</span> <span class="mi">10</span> <span class="o">);</span>
+    <span class="o">}</span>
+<span class="o">}</span>
+</pre></div>
+
+
+<p>Here we have the code for the handler portion of the tutorial program. We have overridden the methods exceptionCaught, messageReceived and sessionCreated. As stated previously, this class extends the <em>IoHandlerAdapter</em>, which is a class that follows the Adapter design pattern.</p>
+<p>The exceptionCaught method will simply print the stack trace of the error and close the session. For most programs, this will be standard practice unless the handler can recover from the exception condition.</p>
+<p>The messageReceived method will receive the data from the client and write back to the client the current time. If the message received from the client is the word "quit", then the session will be closed. This method will also print out the current time to the client. Depending on the protocol codec that you use, the object (second parameter) that gets passed in to this method will be different, as well as the object that you pass in to the session.write(Object) method. If you do not specify a protocol codec, you will most likely receive a <em>ByteBuffer</em> object, and be required to write out a <em>ByteBuffer</em> object.</p>
+<p>The sessionCreated method is typically where your session initialization occurs. In this case, we print out that the method has been entered, and then test if the transport type of the sesion is socket based (versus UDP), and then set the receive buffer size. In the case above, the incoming buffer size is set to 2048 bytes. The idle time is also set to 10 seconds. If we were to override the sessionIdle method, the sessionIdle method would get called every 10 seconds.</p>
+<h2 id="try-out-the-time-server">Try out the Time server</h2>
+<p>At this point, we can go ahead and compile the program. Once you have compiled the program you can run the program in order to test out what happens. The easiest way to test the program is to start the program, and then telnet in to the program:</p>
+<table>
+<thead>
+<tr>
+<th>Client Output</th>
+<th>Server Output</th>
+</tr>
+</thead>
+<tbody>
+<tr>
+<td>user@myhost:~&gt; telnet 127.0.0.1 9123 \</td>
+<td></td>
+</tr>
+<tr>
+<td>Trying 127.0.0.1... \</td>
+<td></td>
+</tr>
+<tr>
+<td>Connected to 127.0.0.1. \</td>
+<td></td>
+</tr>
+<tr>
+<td>Escape character is '^]'. \</td>
+<td></td>
+</tr>
+<tr>
+<td>hello \</td>
+<td></td>
+</tr>
+<tr>
+<td>Mon Apr 09 23:42:55 EDT 2007 \</td>
+<td></td>
+</tr>
+<tr>
+<td>quit \</td>
+<td></td>
+</tr>
+<tr>
+<td>Connection closed by foreign host. \</td>
+<td></td>
+</tr>
+<tr>
+<td>user@myhost:~&gt;</td>
+<td>MINA Time server started. \</td>
+</tr>
+<tr>
+<td>Session created... \</td>
+<td></td>
+</tr>
+<tr>
+<td>Message written...</td>
+<td></td>
+</tr>
+</tbody>
+</table>
+<h2 id="using-mina-in-restricted-java-environnements">Using MINA in restricted Java environnements</h2>
+<p><a href="http://issues.apache.org/jira/browse/DIRMINA-659">DIRMINA-659</a> issue rised the necessity of adding a special permission when running MINA framework in a restricted environnement like applets or Java Webstart technology. This happens when shutting down the thread pools (<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#shutdown()">ExecutorService</a>).</p>
+<p>In order to get it to work properly, you will need to set the following permission :</p>
+<div class="codehilite"><pre><span class="n">permission</span> <span class="n">java</span><span class="o">.</span><span class="na">lang</span><span class="o">.</span><span class="na">RuntimePermission</span> <span class="s">&quot;modifyThread&quot;</span><span class="o">;</span>
+</pre></div>
+
+
+<p>Or you can also choose to sign your code.</p>
+<h2 id="whats-next">What's Next?</h2>
+<p>Please visit our [Documentation] page to find out more resources.  You can also keep reading <a href="Documentation#Tutorials">other tutorials</a>.</p>
+
+
+
+                </div><!-- rightColumn -->
+                <div id="endContent"></div>
+            </div><!-- content -->
+            <div id="footer">&copy; 2003-2012, <a href="http://www.apache.org">The Apache Software Foundation</a> - <a href="./../privacy-policy.html">Privacy Policy</a><br />
+                Apache MINA, MINA, Apache Vysper, Vysper, Apache SSHd, SSHd, Apache FtpServer, FtpServer, Apache AsyncWeb, AsyncWeb,
+                Apache, the Apache feather logo, and the Apache Mina project logos are trademarks of The Apache Software Foundation.
+            </div>
+        </div><!-- container -->
+    </body>
+</html>
+