Building scalable server in 20 lines

MINA is an Apache open source network application framework, it provides developers with all the necessary infrastructure required to build scalable and high performance server. The following chart shows the performance of MINA compared to Apache server, the chart was obtained from the following link

MINA architecture is very flexible and easy to use, it took me 3 hours to setup my environment, download the source code and start hacking away with the source code using the test cases provided.

Following is a sample from MINA source repository that have been modified a bit to make it more compact

package org.apache.asyncweb.examples.listener;

import org.apache.asyncweb.examples.helloworld.HelloWorldHttpService;
import org.apache.asyncweb.examples.session.SessionExample;
import org.apache.asyncweb.fileservice.FileHttpService;
import org.apache.asyncweb.server.BasicServiceContainer;
import org.apache.asyncweb.server.HttpServiceHandler;
import org.apache.asyncweb.server.transport.mina.MinaTransport;
import org.apache.asyncweb.server.transport.mina.DefaultHttpIoHandler;
import org.apache.asyncweb.server.resolver.ExactMatchURIServiceResolver;

public class Main
{
    public static void main( String[] args ) throws Exception
    {
        // Setup the default container with a service handler that contains the helloWorldService
        BasicServiceContainer container = new BasicServiceContainer();
        HttpServiceHandler handler = new HttpServiceHandler();
        handler.addHttpService( "clientListenerExample", new HelloWorldHttpService() );
        container.addServiceFilter( handler );

        // Set up a resolver for the HttpServiceHandler
        ExactMatchURIServiceResolver resolver = new ExactMatchURIServiceResolver();
        resolver.addURIMapping( "/listener", "clientListenerExample" );
        handler.setServiceResolver( resolver );

        // Create the mina transport and enable the container with it
        MinaTransport transport = new MinaTransport();
        container.addTransport( transport );
        DefaultHttpIoHandler ioHandler = new DefaultHttpIoHandler();
        ioHandler.setReadIdle( 10 );
        transport.setIoHandler( ioHandler );

        // Fire it up and go
        container.start();
    }
}

Point your browser to http://localhost:9012 and you will see a response displayed on your browser as shown below

Internally MINA takes care of the complexity in managing request and response letting you focus on developing the logic needed to takes care of the request of the user.

1 Comment so far

  1. [...] @me&java ยป Building scalable server in 20 lines [...]

Leave a reply