We are ready to go now. What's left is to bind to the port and to start the server. Here, we bind to the port 8888 of all NICs (network interface cards) in the machine. You can now call the bind method as many times as you want (with different bind addresses.)

Before deploying your EJB on the server, JBoss AS 7 users still miss one thing: the Netty libraries are not classified as automatic dependencies, therefore we need telling our application to trigger org.jboss.netty module. Just add to your MANIFEST.MF file the following:

 

 

Manifest-Version: 1.0
Dependencies: org.jboss.netty

 

You can connect with any client: the simplest one is telnet 8888

Or if you prefer use any Socket client, like this NIO client:


package com.sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;

public class NIOClient {

    SelectionKey selkey=null;
    Selector sckt_manager=null;
    public static void coreClient()
    {

        BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
        SocketChannel sc = null;
        try
        { sc = SocketChannel.open();
        sc.configureBlocking(false);       
        sc.connect(new InetSocketAddress(8888));  
        while (!sc.finishConnect())
        {   
        } 

        System.out.println("Enter the text");
        String HELLO_REQUEST =stdin.readLine().toString();

        System.out.println("Sending a request to HelloServer");    
        ByteBuffer buffer = ByteBuffer.wrap(HELLO_REQUEST.getBytes());    
        sc.write(buffer); 


        } 
        catch (IOException e) 
        {          
            e.printStackTrace();    
        }
        finally
        {       
            if (sc != null)
            {            
                try 
                {             
                    sc.close();            
                }
                catch (Exception e)
                {           
                    e.printStackTrace();       
                }       
            } 
        }   }

    public static void main(String args[]) {
        coreClient();

    }
}

That's just a first taste of this awesome library, which allows advanced Server features like Asynchronous I/O API, TimeBased servers, an extensible event model, built-in security and much more!

For futher reading: http://docs.jboss.org/netty/3.2/guide/html/index.html

0
0
0
s2smodern

W

i

l

d

F

l

y

 

c

h

e

a

t

s

h

e

e

t