Example usage for io.netty.handler.codec.serialization ObjectEncoder ObjectEncoder

List of usage examples for io.netty.handler.codec.serialization ObjectEncoder ObjectEncoder

Introduction

In this page you can find the example usage for io.netty.handler.codec.serialization ObjectEncoder ObjectEncoder.

Prototype

ObjectEncoder

Source Link

Usage

From source file:afred.javademo.proxy.rpc.ObjectEchoClient.java

License:Apache License

public void start(String host, int port) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//w ww  .j a v a 2s .co m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                        new ObjectEchoClientHandler());
            }
        });

        channel = b.connect(host, port).sync().channel();
    } finally {

    }
}

From source file:afred.javademo.proxy.rpc.ObjectEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  w w w  .  j  a  va 2  s . com
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                                new ObjectEchoServerHandler());
                    }
                });

        // Bind and start to accept incoming connections.
        b.bind(8080).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:books.netty.codec.serializable.netty.SubReqClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*from  w  ww  .  j  a  v a2s .  co m*/
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new ObjectDecoder(1024,
                                ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new SubReqClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();

        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:books.netty.codec.serializable.netty.SubReqServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO/*from   ww  w  .  j ava 2s  .c o m*/
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new ObjectDecoder(1024 * 1024, ClassResolvers
                                .weakCachingConcurrentResolver(this.getClass().getClassLoader())));
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new SubReqServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:ChatClient.ChatClientInitializer.java

@Override
protected void initChannel(SocketChannel c) throws Exception {
    ChannelPipeline pipeline = c.pipeline();

    pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
    pipeline.addLast("encoder", new ObjectEncoder());

    pipeline.addLast("handler", new ChatClientHandler());
}

From source file:ChatServer.ChatServerInitializer.java

@Override
protected void initChannel(SocketChannel c) throws Exception {
    ChannelPipeline pipeline = c.pipeline();

    pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.cacheDisabled(getClass().getClassLoader())));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new ChatServerHandler());
}

From source file:cn.savor.small.netty.NettyClient.java

License:Open Source License

public void start() {
    try {/* w w  w .  j a  v a2  s  .  c  o m*/
        bootstrap = new Bootstrap();
        bootstrap.group(workGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        System.out.println("client SocketChannel.....................................");
                        ch.pipeline().addLast("ping", new IdleStateHandler(60, 60, 80, TimeUnit.SECONDS));
                        //POJO? ?
                        ch.pipeline().addLast(new ObjectDecoder(1024 * 5,
                                ClassResolvers.cacheDisabled(this.getClass().getClassLoader())));
                        //????
                        ch.pipeline().addLast(new ObjectEncoder());

                        ch.pipeline().addLast(new NettyClientHandler(NettyClient.this, callback, mContext));
                    }
                });
        connect();

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.athena.meerkat.agent.netty.MeerkatClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    LOGGER.debug("**************************Tran - Init *****************************");
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", handler);
}

From source file:com.athena.peacock.agent.netty.PeacockClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", handler);
}

From source file:com.athena.peacock.controller.netty.PeacockServerInitializer.java

License:Open Source License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast("decoder", new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast("encoder", new ObjectEncoder());
    pipeline.addLast("handler", new PeacockServerHandler());
}