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

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

Introduction

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

Prototype

public static ClassResolver weakCachingConcurrentResolver(ClassLoader classLoader) 

Source Link

Document

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

Usage

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

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO// w ww. j av  a 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:com.baidu.rigel.biplatform.ma.file.serv.FileServer.java

License:Open Source License

private static void startServer(String location, int port) {
    fileLocation = new FileLocation(location);
    service = new LocalFileOperationServiceImpl(fileLocation);
    EventLoopGroup bossGroup = new NioEventLoopGroup(10);
    EventLoopGroup workGroup = new NioEventLoopGroup(50);
    try {//  w  w w  . j av  a 2s . co  m
        ServerBootstrap strap = new ServerBootstrap();
        strap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel channel) throws Exception {
                        // ??
                        channel.pipeline().addLast(new ObjectDecoder(ClassResolvers
                                .weakCachingConcurrentResolver(FileServer.class.getClassLoader())));
                        channel.pipeline().addLast(new ObjectEncoder());
                        //                        channel.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
                        channel.pipeline().addLast(new FileServer());
                    }
                });
        ChannelFuture future = strap.bind(port).sync();
        LOGGER.info("start file server successfully");
        LOGGER.info("port : " + port);
        LOGGER.info("location : " + location);
        future.channel().closeFuture().sync();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        LOGGER.error("can not start file server with [port : {}] and fileLocation {{}}", port, location);
        printUsage();
        System.exit(-1);
    } finally {
        bossGroup.shutdownGracefully();
        workGroup.shutdownGracefully();
    }
}

From source file:com.baidu.rigel.biplatform.tesseract.node.service.IndexAndSearchClient.java

License:Open Source License

public IndexAndSearchClient() {
    // channelMaps = new ConcurrentHashMap<NodeAddress, Channel>();
    actionHandlerMaps = new ConcurrentHashMap<String, ChannelHandler>();
    b = new Bootstrap();
    group = new NioEventLoopGroup();
    b.group(group);/*from   w w  w  . j a va2s  . c o m*/
    b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("encode", new ObjectEncoder());
            pipeline.addLast("decode", new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(null)));
            //                pipeline.addLast("frameencoder",new LengthFieldPrepender(4,false));
            //                pipeline.addLast("framedecoder",new LengthFieldBasedFrameDecoder(1024*1024*1024, 0, 4,0,4));
        }
    });

    logger.info("IndexAndSearchClient init finished");
}

From source file:com.baidu.rigel.biplatform.tesseract.node.service.IndexAndSearchServer.java

License:Open Source License

/**
 * startServer/*from w  w  w.  ja va 2 s  .com*/
 * 
 * @throws Exception
 */
protected void startServer() throws Exception {
    LOGGER.info("Index and Search server ready to start...");
    long curr = System.currentTimeMillis();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.option(ChannelOption.SO_BACKLOG, 1000000);
        b.childHandler(new ChannelInitializer<SocketChannel>() {

            /*
             * (non-Javadoc)
             * 
             * @see
             * io.netty.channel.ChannelInitializer#initChannel(io.netty.
             * channel.Channel)
             */
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("encode", new ObjectEncoder());
                pipeline.addLast("decode",
                        new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(null)));
                pipeline.addLast(IndexServerHandler.getChannelHandler());
                pipeline.addLast(SearchServerHandler.getChannelHandler());
                pipeline.addLast(FileServerHandler.getChannelHandler());
            }

        });

        // ChannelFuture f = b.bind(IP, PORT).sync();
        // f.channel().closeFuture().sync();

        int currPort = NetworkUtils.getAvailablePort(this.node.getPort());

        ChannelFuture f = b.bind(IP, currPort).sync();

        if (currPort != this.node.getPort()) {
            this.node.setPort(currPort);
        }

        serverChannelFuture = f;
        LOGGER.info("Index and Search server started at Port:" + this.node.getPort());
        LOGGER.info("Index and Search server started in " + (System.currentTimeMillis() - curr) + "ms");
        this.isRunning = true;

        serverChannelFuture.channel().closeFuture().sync().channel();

    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.ebay.jetstream.messaging.transport.netty.eventconsumer.Acceptor.java

License:MIT License

/**
 * @param rxSessionHandler/*w  w  w. jav a  2s.c  o m*/
 * @throws Exception
 */
public void bind(EventProducerSessionHandler rxSessionHandler) throws Exception {

    m_rxSessionHandler = rxSessionHandler;
    m_bossGroup = new NioEventLoopGroup(1,
            new NameableThreadFactory("NettyAcceptor-" + m_tc.getTransportName()));
    m_workerGroup = new NioEventLoopGroup(m_numIoProcessors,
            new NameableThreadFactory("NettyReceiver-" + m_tc.getTransportName()));

    try {
        m_serverbootstrap = new ServerBootstrap();
        m_serverbootstrap.group(m_bossGroup, m_workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {

                        ch.pipeline().addFirst("msghandler", m_rxSessionHandler);

                        StreamMessageDecoder decoder = new StreamMessageDecoder(
                                ClassResolvers.weakCachingConcurrentResolver(null));
                        ch.pipeline().addBefore("msghandler", "msgdecoder", decoder);
                        ch.pipeline().addBefore("msgdecoder", "kryoEcnoder", new KryoObjectEncoder());
                        ch.pipeline().addBefore("kryoEcnoder", "msgencoder", new NettyObjectEncoder());

                        if (m_enableCompression) {

                            ch.pipeline().addBefore("msgencoder", "decompressor",
                                    new MessageDecompressionHandler(false, 250000));

                            ch.pipeline().addBefore("decompressor", "idleTimeoutHandler",
                                    new IdleStateHandler((int) m_readIdleTimeout, 0, 0));
                        } else
                            ch.pipeline().addBefore("msgencoder", "idleTimeoutHandler",
                                    new IdleStateHandler((int) m_readIdleTimeout, 0, 0));

                    }
                }).option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                .childOption(ChannelOption.SO_KEEPALIVE, isTcpKeepAlive());

        if (m_tc.getReceivebuffersize() > 0) {
            m_serverbootstrap.childOption(ChannelOption.SO_RCVBUF, m_tc.getReceivebuffersize());
        }

        if (m_tc.getSendbuffersize() > 0) {
            m_serverbootstrap.childOption(ChannelOption.SO_SNDBUF, m_tc.getSendbuffersize());
        }

    } catch (Exception t) {
        throw t;
    }

    // we are given a port from DNS. We will check if it is taken. If it is
    // we will increment the port # by 10 and keep trying 10 times.
    // adding this feature so that we can operate the cluster on a single
    // node. Very useful for debugging and also for demos.

    int retryCount = 20;
    int port = m_tcpPort;

    while (retryCount-- > 0) {
        if (isPortAvailable(port))
            break;
        port += 1;

    }

    if (retryCount == 0)
        return; // we did not find a port to bind

    m_tcpPort = port;

    LOGGER.info("Acceptor bound to port" + m_tcpPort);

}

From source file:com.heren.turtle.entry.channel.MessageReceiveInitializer.java

License:Open Source License

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

    pipeline.addLast(new ObjectDecoder(MAX_OBJECT_SIZE,
            ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
    pipeline.addLast(new ObjectEncoder());
    if (needToFilter) {
        pipeline.addLast(new LoginAuthRespHandler());
    }//from w ww  .jav  a  2  s.  c  o  m
    pipeline.addLast(new MessageReceiverHandler());
}

From source file:com.newlandframework.rpc.netty.handler.JdkNativeRecvHandler.java

License:Apache License

public void handle(Map<String, Object> handlerMap, ChannelPipeline pipeline) {
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, MessageCodecUtil.MESSAGE_LENGTH, 0,
            MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new LengthFieldPrepender(MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE,
            ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
    pipeline.addLast(new MessageRecvHandler(handlerMap));
}

From source file:com.newlandframework.rpc.netty.handler.JdkNativeSendHandler.java

License:Apache License

public void handle(ChannelPipeline pipeline) {
    pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, MessageCodecUtil.MESSAGE_LENGTH, 0,
            MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new LengthFieldPrepender(MessageCodecUtil.MESSAGE_LENGTH));
    pipeline.addLast(new ObjectEncoder());
    pipeline.addLast(new ObjectDecoder(Integer.MAX_VALUE,
            ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
    pipeline.addLast(new MessageSendHandler());
}

From source file:darks.grid.network.GridMessageClient.java

License:Apache License

private ChannelInitializer<SocketChannel> newChannelHandler() {
    ChannelInitializer<SocketChannel> result = new ChannelInitializer<SocketChannel>() {

        @Override/*w  ww.  j av a 2 s  .co m*/
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("decoder", CodecFactory.createDecoder(
                    ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
            pipeline.addLast("encoder", CodecFactory.createEncoder());
            //            pipeline.addLast("alive", new IdleStateHandler(60, 60, 120));
            pipeline.addLast("message", new GridClientMessageHandler());
        }
    };
    return result;
}

From source file:darks.grid.network.GridMessageServer.java

License:Apache License

private ChannelInitializer<SocketChannel> newChannelHandler() {
    ChannelInitializer<SocketChannel> result = new ChannelInitializer<SocketChannel>() {

        @Override/*from   w ww. j av  a2  s  .c  o  m*/
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("decoder", CodecFactory.createDecoder(
                    ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())));
            pipeline.addLast("encoder", CodecFactory.createEncoder());
            pipeline.addLast("alive", new IdleStateHandler(60, 60, 120));
            pipeline.addLast("message", new GridServerMessageHandler());
        }
    };
    return result;
}