Example usage for io.netty.buffer UnpooledByteBufAllocator UnpooledByteBufAllocator

List of usage examples for io.netty.buffer UnpooledByteBufAllocator UnpooledByteBufAllocator

Introduction

In this page you can find the example usage for io.netty.buffer UnpooledByteBufAllocator UnpooledByteBufAllocator.

Prototype

public UnpooledByteBufAllocator(boolean preferDirect) 

Source Link

Document

Create a new instance which uses leak-detection for direct buffers.

Usage

From source file:org.apache.bookkeeper.client.BookKeeperTestClient.java

License:Apache License

public BookKeeperTestClient(ClientConfiguration conf, TestStatsProvider statsProvider)
        throws IOException, InterruptedException, BKException {
    super(conf, null, null, new UnpooledByteBufAllocator(false),
            statsProvider == null ? NullStatsLogger.INSTANCE : statsProvider.getStatsLogger(""), null, null,
            null);/* ww  w .  j  a v  a  2s .co m*/
    this.statsProvider = statsProvider;
}

From source file:org.apache.giraph.conf.GiraphConfiguration.java

License:Apache License

/**
 * Used by netty client and server to create ByteBufAllocator
 *
 * @return ByteBufAllocator/*from  w  w w. j a va 2 s. c o  m*/
 */
public ByteBufAllocator getNettyAllocator() {
    if (nettyBufferAllocator == null) {
        if (NETTY_USE_POOLED_ALLOCATOR.get(this)) { // Use pooled allocator
            nettyBufferAllocator = new PooledByteBufAllocator(NETTY_USE_DIRECT_MEMORY.get(this));
        } else { // Use un-pooled allocator
            // Note: Current default settings create un-pooled heap allocator
            nettyBufferAllocator = new UnpooledByteBufAllocator(NETTY_USE_DIRECT_MEMORY.get(this));
        }
    }
    return nettyBufferAllocator;
}

From source file:org.hornetq.core.remoting.impl.netty.NettyConnector.java

License:Apache License

public synchronized void start() {
    if (channelClazz != null) {
        return;//from  w w w .  j a v a  2s .  co  m
    }

    int threadsToUse;

    if (nioRemotingThreads == -1) {
        // Default to number of cores * 3

        threadsToUse = Runtime.getRuntime().availableProcessors() * 3;
    } else {
        threadsToUse = this.nioRemotingThreads;
    }

    if (useNioGlobalWorkerPool) {
        channelClazz = NioSocketChannel.class;
        group = SharedNioEventLoopGroup.getInstance(threadsToUse);
    } else {
        channelClazz = NioSocketChannel.class;
        group = new NioEventLoopGroup(threadsToUse);
    }
    // if we are a servlet wrap the socketChannelFactory
    if (useServlet) {
        // TODO: This will be replaced by allow upgrade HTTP connection from Undertow.;
    }
    bootstrap = new Bootstrap();
    bootstrap.channel(channelClazz);
    bootstrap.group(group);

    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);

    if (connectTimeoutMillis != -1) {
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis);
    }
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
    }
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.ALLOCATOR, new UnpooledByteBufAllocator(false));
    channelGroup = new DefaultChannelGroup("hornetq-connector", GlobalEventExecutor.INSTANCE);

    final SSLContext context;
    if (sslEnabled) {
        try {
            // HORNETQ-680 - override the server-side config if client-side system properties are set
            String realKeyStorePath = keyStorePath;
            String realKeyStoreProvider = keyStoreProvider;
            String realKeyStorePassword = keyStorePassword;
            if (System.getProperty(JAVAX_KEYSTORE_PATH_PROP_NAME) != null) {
                realKeyStorePath = System.getProperty(JAVAX_KEYSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(JAVAX_KEYSTORE_PASSWORD_PROP_NAME) != null) {
                realKeyStorePassword = System.getProperty(JAVAX_KEYSTORE_PASSWORD_PROP_NAME);
            }

            if (System.getProperty(HORNETQ_KEYSTORE_PROVIDER_PROP_NAME) != null) {
                realKeyStoreProvider = System.getProperty(HORNETQ_KEYSTORE_PROVIDER_PROP_NAME);
            }
            if (System.getProperty(HORNETQ_KEYSTORE_PATH_PROP_NAME) != null) {
                realKeyStorePath = System.getProperty(HORNETQ_KEYSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(HORNETQ_KEYSTORE_PASSWORD_PROP_NAME) != null) {
                realKeyStorePassword = System.getProperty(HORNETQ_KEYSTORE_PASSWORD_PROP_NAME);
            }

            String realTrustStorePath = trustStorePath;
            String realTrustStoreProvider = trustStoreProvider;
            String realTrustStorePassword = trustStorePassword;
            if (System.getProperty(JAVAX_TRUSTSTORE_PATH_PROP_NAME) != null) {
                realTrustStorePath = System.getProperty(JAVAX_TRUSTSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(JAVAX_TRUSTSTORE_PASSWORD_PROP_NAME) != null) {
                realTrustStorePassword = System.getProperty(JAVAX_TRUSTSTORE_PASSWORD_PROP_NAME);
            }

            if (System.getProperty(HORNETQ_TRUSTSTORE_PROVIDER_PROP_NAME) != null) {
                realTrustStoreProvider = System.getProperty(HORNETQ_TRUSTSTORE_PROVIDER_PROP_NAME);
            }
            if (System.getProperty(HORNETQ_TRUSTSTORE_PATH_PROP_NAME) != null) {
                realTrustStorePath = System.getProperty(HORNETQ_TRUSTSTORE_PATH_PROP_NAME);
            }
            if (System.getProperty(HORNETQ_TRUSTSTORE_PASSWORD_PROP_NAME) != null) {
                realTrustStorePassword = System.getProperty(HORNETQ_TRUSTSTORE_PASSWORD_PROP_NAME);
            }
            context = SSLSupport.createContext(realKeyStoreProvider, realKeyStorePath, realKeyStorePassword,
                    realTrustStoreProvider, realTrustStorePath, realTrustStorePassword);
        } catch (Exception e) {
            close();
            IllegalStateException ise = new IllegalStateException(
                    "Unable to create NettyConnector for " + host + ":" + port);
            ise.initCause(e);
            throw ise;
        }
    } else {
        context = null; // Unused
    }

    if (context != null && useServlet) {
        // TODO: Fix me
        //bootstrap.setOption("sslContext", context);
    }

    bootstrap.handler(new ChannelInitializer<Channel>() {
        public void initChannel(Channel channel) throws Exception {
            final ChannelPipeline pipeline = channel.pipeline();
            if (sslEnabled && !useServlet) {
                SSLEngine engine = context.createSSLEngine();

                engine.setUseClientMode(true);

                engine.setWantClientAuth(true);

                // setting the enabled cipher suites resets the enabled protocols so we need
                // to save the enabled protocols so that after the customer cipher suite is enabled
                // we can reset the enabled protocols if a customer protocol isn't specified
                String[] originalProtocols = engine.getEnabledProtocols();

                if (enabledCipherSuites != null) {
                    try {
                        engine.setEnabledCipherSuites(
                                SSLSupport.parseCommaSeparatedListIntoArray(enabledCipherSuites));
                    } catch (IllegalArgumentException e) {
                        HornetQClientLogger.LOGGER.invalidCipherSuite(SSLSupport
                                .parseArrayIntoCommandSeparatedList(engine.getSupportedCipherSuites()));
                        throw e;
                    }
                }

                if (enabledProtocols != null) {
                    try {
                        engine.setEnabledProtocols(
                                SSLSupport.parseCommaSeparatedListIntoArray(enabledProtocols));
                    } catch (IllegalArgumentException e) {
                        HornetQClientLogger.LOGGER.invalidProtocol(
                                SSLSupport.parseArrayIntoCommandSeparatedList(engine.getSupportedProtocols()));
                        throw e;
                    }
                } else {
                    engine.setEnabledProtocols(originalProtocols);
                }

                SslHandler handler = new SslHandler(engine);

                pipeline.addLast(handler);
            }

            if (httpEnabled) {
                pipeline.addLast(new HttpRequestEncoder());

                pipeline.addLast(new HttpResponseDecoder());

                pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));

                pipeline.addLast(new HttpHandler());
            }

            if (httpUpgradeEnabled) {
                // prepare to handle a HTTP 101 response to upgrade the protocol.
                final HttpClientCodec httpClientCodec = new HttpClientCodec();
                pipeline.addLast(httpClientCodec);
                pipeline.addLast("http-upgrade", new HttpUpgradeHandler(pipeline, httpClientCodec));
            }
            pipeline.addLast(new HornetQFrameDecoder2());

            pipeline.addLast(new HornetQClientChannelHandler(channelGroup, handler, new Listener()));
        }
    });

    if (batchDelay > 0) {
        flusher = new BatchFlusher();

        batchFlusherFuture = scheduledThreadPool.scheduleWithFixedDelay(flusher, batchDelay, batchDelay,
                TimeUnit.MILLISECONDS);
    }

    HornetQClientLogger.LOGGER.debug("Started Netty Connector version " + TransportConstants.NETTY_VERSION);
}

From source file:org.jupiter.transport.netty.NettyAcceptor.java

License:Apache License

protected void setOptions() {
    JConfig parent = configGroup().parent(); // parent options
    JConfig child = configGroup().child(); // child options

    setIoRatio(parent.getOption(JOption.IO_RATIO), child.getOption(JOption.IO_RATIO));

    boolean direct = child.getOption(JOption.PREFER_DIRECT);
    if (child.getOption(JOption.USE_POOLED_ALLOCATOR)) {
        if (direct) {
            allocator = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {/*from www .  j a v a2s .  co  m*/
            allocator = new PooledByteBufAllocator(false);
        }
    } else {
        if (direct) {
            allocator = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {
            allocator = new UnpooledByteBufAllocator(false);
        }
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, allocator).childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR,
            JMessageSizeEstimator.DEFAULT);
}

From source file:org.jupiter.transport.netty.NettyConnector.java

License:Apache License

protected void setOptions() {
    JConfig child = config();/* w w w  .ja va 2 s  .co  m*/

    setIoRatio(child.getOption(JOption.IO_RATIO));

    boolean direct = child.getOption(JOption.PREFER_DIRECT);
    if (child.getOption(JOption.USE_POOLED_ALLOCATOR)) {
        if (direct) {
            allocator = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {
            allocator = new PooledByteBufAllocator(false);
        }
    } else {
        if (direct) {
            allocator = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {
            allocator = new UnpooledByteBufAllocator(false);
        }
    }
    bootstrap.option(ChannelOption.ALLOCATOR, allocator).option(ChannelOption.MESSAGE_SIZE_ESTIMATOR,
            JMessageSizeEstimator.DEFAULT);
}

From source file:org.springframework.core.codec.support.AbstractAllocatingTestCase.java

License:Apache License

@Parameterized.Parameters(name = "{0}")
public static Object[][] allocators() {
    return new Object[][] { { new NettyDataBufferAllocator(new UnpooledByteBufAllocator(true)) },
            { new NettyDataBufferAllocator(new UnpooledByteBufAllocator(false)) },
            { new NettyDataBufferAllocator(new PooledByteBufAllocator(true)) },
            { new NettyDataBufferAllocator(new PooledByteBufAllocator(false)) },
            { new DefaultDataBufferAllocator(true) }, { new DefaultDataBufferAllocator(false) }

    };/*from www .  j a va2  s  . com*/
}

From source file:org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase.java

License:Apache License

@Parameterized.Parameters(name = "{0}")
public static Object[][] dataBufferFactories() {
    return new Object[][] { { new NettyDataBufferFactory(new UnpooledByteBufAllocator(true)) },
            { new NettyDataBufferFactory(new UnpooledByteBufAllocator(false)) },
            // disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
            { new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0, true)) },
            { new NettyDataBufferFactory(new PooledByteBufAllocator(false, 1, 1, 8192, 11, 0, 0, 0, true)) },
            { new DefaultDataBufferFactory(true) }, { new DefaultDataBufferFactory(false) }

    };/*from  w  w w  .ja v a 2s  .  c o  m*/
}

From source file:org.springframework.core.io.buffer.AbstractDataBufferAllocatingTests.java

License:Apache License

public static Stream<Arguments> dataBufferFactories() {
    return Stream.of(
            arguments("NettyDataBufferFactory - UnpooledByteBufAllocator - preferDirect = true",
                    new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))),
            arguments("NettyDataBufferFactory - UnpooledByteBufAllocator - preferDirect = false",
                    new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))),
            // disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
            arguments("NettyDataBufferFactory - PooledByteBufAllocator - preferDirect = true",
                    new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 4096, 2, 0, 0, 0, true))),
            arguments("NettyDataBufferFactory - PooledByteBufAllocator - preferDirect = false",
                    new NettyDataBufferFactory(
                            new PooledByteBufAllocator(false, 1, 1, 4096, 2, 0, 0, 0, true))),
            arguments("DefaultDataBufferFactory - preferDirect = true", new DefaultDataBufferFactory(true)),
            arguments("DefaultDataBufferFactory - preferDirect = false", new DefaultDataBufferFactory(false)));
}

From source file:org.springframework.core.io.buffer.DataBufferTests.java

License:Apache License

@Parameterized.Parameters(name = "{0}")
public static Object[][] buffers() {

    return new Object[][] { { new NettyDataBufferAllocator(new UnpooledByteBufAllocator(true)) },
            { new NettyDataBufferAllocator(new UnpooledByteBufAllocator(false)) },
            { new NettyDataBufferAllocator(new PooledByteBufAllocator(true)) },
            { new NettyDataBufferAllocator(new PooledByteBufAllocator(false)) },
            { new DefaultDataBufferAllocator(true) }, { new DefaultDataBufferAllocator(false) } };
}

From source file:org.springframework.core.io.buffer.PooledDataBufferTests.java

License:Apache License

@Parameterized.Parameters(name = "{0}")
public static Object[][] buffers() {

    return new Object[][] { { new NettyDataBufferFactory(new UnpooledByteBufAllocator(true)) },
            { new NettyDataBufferFactory(new UnpooledByteBufAllocator(false)) },
            { new NettyDataBufferFactory(new PooledByteBufAllocator(true)) },
            { new NettyDataBufferFactory(new PooledByteBufAllocator(false)) } };
}