Example usage for io.netty.handler.codec.serialization ClassResolvers softCachingResolver

List of usage examples for io.netty.handler.codec.serialization ClassResolvers softCachingResolver

Introduction

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

Prototype

public static ClassResolver softCachingResolver(ClassLoader classLoader) 

Source Link

Document

aggressive non-concurrent cache good for non-shared cache, when we're not worried about class unloading

Usage

From source file:com.doctor.netty5.example.echo_object.EchoClient.java

License:Apache License

public void start() throws InterruptedException {
    NioEventLoopGroup workersGroup = new NioEventLoopGroup(1);

    try {/*w  w w  . j ava2 s . c  o  m*/
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new ObjectDecoder(
                                ClassResolvers.softCachingResolver(this.getClass().getClassLoader())));
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        workersGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.echo_object.EchoServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from   w w w  . java  2s.  c o  m
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new ObjectDecoder(
                                ClassResolvers.softCachingResolver(this.getClass().getClassLoader())));
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(EchoServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

        channelFuture.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}