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

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

Introduction

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

Prototype

public ObjectDecoder(int maxObjectSize, ClassResolver classResolver) 

Source Link

Document

Creates a new decoder with the specified maximum object size.

Usage

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 w  w .  ja v a 2  s.  c  o 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   w w w. ja v a  2s .  com
    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:cn.savor.small.netty.NettyClient.java

License:Open Source License

public void start() {
    try {//w w w .  j a  v a  2  s  . co 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());
}

From source file:com.baidu.rigel.biplatform.ma.file.client.monitor.FileServerMonitor.java

License:Open Source License

/**
 * ?/*ww w . j  a v a 2  s . c  o  m*/
 */
public void start() throws Exception {
    EventLoopGroup work = new NioEventLoopGroup();
    Bootstrap strap = new Bootstrap();
    try {
        strap.group(work).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel chl) throws Exception {
                        // ??
                        chl.pipeline().addLast(new ObjectDecoder(1024,
                                ClassResolvers.cacheDisabled(FileServerMonitor.class.getClassLoader())));
                        chl.pipeline().addLast(new ObjectEncoder());
                        chl.pipeline().addLast(new FileServerMonitor());
                    }

                });

        ChannelFuture future = strap.connect(new InetSocketAddress(host, port));
        future.channel().closeFuture().sync();
    } finally {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(3);
                    ChannelFuture future = strap.connect("localhost", 9090).sync();
                    future.channel().closeFuture().sync();
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(), e);
                }
            }

        });
    }
}

From source file:com.changxx.phei.netty.codec.serializable.netty.SubReqClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO//from   w w  w.  ja  v a2  s .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) throws Exception {
                        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:com.github.mrstampy.gameboot.otp.netty.client.ClearClientInitializer.java

License:Open Source License

@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new LengthFieldPrepender(4));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast(clientHandler);/*from  w  w  w  .  j  a va2 s .  c om*/
}

From source file:com.github.mrstampy.gameboot.otp.netty.client.EncryptedClientInitializer.java

License:Open Source License

@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();

    pipeline.addLast(new SslHandler(createSslEngine()));
    pipeline.addLast(new LengthFieldPrepender(2));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2));
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null)));
    pipeline.addLast(clientHandler);/*from w  w w. j  a v a 2s . co m*/
}