梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
1.server
package example.helloword.server;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;

import example.helloword.NetConstant;

public class Server
{
    
private static Server server = new Server();
    
    
private ServerBootstrap bootstrap;
    
    
private Server()
    
{}
    
    
public static Server getInstance()
    
{
        
return server;
    }

    
    
public void start()
    
{
        bootstrap 
= new ServerBootstrap(new NioServerSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors
                        .newCachedThreadPool()));
        bootstrap.setPipelineFactory(
new ServerPipelineFactory());
        bootstrap.bind(
new InetSocketAddress(NetConstant.server_port));
    }

    
    
public void stop()
    
{
        bootstrap.releaseExternalResources();
    }

    
    
public static void main(String[] args)
    
{
        Server server 
= Server.getInstance();
        server.start();
    }

}

2.ServerPipelineFactory
package example.helloword.server;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

public class ServerPipelineFactory implements ChannelPipelineFactory
{
    
public ChannelPipeline getPipeline() throws Exception
    
{
        ChannelPipeline pipleline 
= pipeline();
        pipleline.addLast(
"encode"new StringEncoder());
        pipleline.addLast(
"decode"new StringDecoder());
        pipleline.addLast(
"handler"new ServerHandler());
        
return pipleline;
    }

}

3.handle
package example.helloword.server;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;

public class ServerHandler extends SimpleChannelUpstreamHandler
{
    
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
            
throws Exception
    
{
        System.out.println(
"recive message,message content:" + e.getMessage());
        e.getChannel().write(
"byte");
        
    }

    
    
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
            
throws Exception
    
{
        e.getChannel().close();
    }

}
clinet:
package example.helloword.client22;

import static org.jboss.netty.channel.Channels.pipeline;

import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

public class ClientPipelineFactory implements ChannelPipelineFactory
{
    
public ChannelPipeline getPipeline() throws Exception
    
{
        ChannelPipeline pipleline 
= pipeline();  
        pipleline.addLast(
"frameDecoder"new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0404));  
        pipleline.addLast(
"frameEncode"new LengthFieldPrepender(4false));
        pipleline.addLast(
"encode"new StringEncoder());  
        pipleline.addLast(
"decode"new StringDecoder());  
        pipleline.addLast(
"handler"new ClinetHandler());  
        
return pipleline;  
    }

}
clientpool:
package example.helloword.client22;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

import example.helloword.NetConstant;
import example.helloword.client2.ClientPipelineFactory;

public class ClientPool
{
    
public static ClientPool clientPool = new ClientPool();
    
    
private ClientBootstrap bootstrap;
    
    
private ClientPool()
    
{
        bootstrap 
= new ClientBootstrap(new NioClientSocketChannelFactory(
                Executors.newCachedThreadPool(), Executors
                        .newCachedThreadPool()));
        
        bootstrap.setPipelineFactory(
new ClientPipelineFactory());
        bootstrap.setOption(
"tcpNoDelay"true);
        bootstrap.setOption(
"keepAlive"true);
    }

    
    
public static ClientPool getInstance()
    
{
        
return clientPool;
    }

    
    
public void getChannelFuture(String host, int port, String message)
    
{
        ChannelFuture future 
= bootstrap.connect(new InetSocketAddress(host,
                NetConstant.server_port));
        future.awaitUninterruptibly();
        
if (!future.isSuccess())
        
{
            future.getCause().printStackTrace();
            future.getChannel().getCloseFuture().awaitUninterruptibly(); 
            
return;
        }

        future.getChannel().write(message);
    }

    
    
public static void main(String[] args) throws InterruptedException
    
{
        
for (int i = 0; i < 1000; i++)
        
{
            ClientPool.getInstance().getChannelFuture(
"127.0.0.1"0,
                    
"test" + i);
            Thread.sleep(
1000 * 3);
        }

    }

}

posted on 2011-08-30 13:07 HUIKK 阅读(3387) 评论(2)  编辑  收藏 所属分类: Javajava Net

评论

# re: jboss netty 框架小试 2013-11-04 15:25 kunjie
LZ,这文章,一点文字说明都没,没有多大实用意义  回复  更多评论
  

# re: jboss netty 框架小试[未登录] 2014-01-02 14:47 123
tell us about what you think and code less, plz.  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: