Search in sources :

Example 21 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project camel by apache.

the class NettyProducer method openConnection.

protected ChannelFuture openConnection() throws Exception {
    ChannelFuture answer;
    if (isTcp()) {
        // its okay to create a new bootstrap for each new channel
        ClientBootstrap clientBootstrap = new ClientBootstrap(channelFactory);
        clientBootstrap.setOption("keepAlive", configuration.isKeepAlive());
        clientBootstrap.setOption("tcpNoDelay", configuration.isTcpNoDelay());
        clientBootstrap.setOption("reuseAddress", configuration.isReuseAddress());
        clientBootstrap.setOption("connectTimeoutMillis", configuration.getConnectTimeout());
        // set any additional netty options
        if (configuration.getOptions() != null) {
            for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) {
                clientBootstrap.setOption(entry.getKey(), entry.getValue());
            }
        }
        // set the pipeline factory, which creates the pipeline for each newly created channels
        clientBootstrap.setPipelineFactory(pipelineFactory);
        answer = clientBootstrap.connect(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created new TCP client bootstrap connecting to {}:{} with options: {}", new Object[] { configuration.getHost(), configuration.getPort(), clientBootstrap.getOptions() });
        }
        return answer;
    } else {
        // its okay to create a new bootstrap for each new channel
        ConnectionlessBootstrap connectionlessClientBootstrap = new ConnectionlessBootstrap(datagramChannelFactory);
        connectionlessClientBootstrap.setOption("child.keepAlive", configuration.isKeepAlive());
        connectionlessClientBootstrap.setOption("child.tcpNoDelay", configuration.isTcpNoDelay());
        connectionlessClientBootstrap.setOption("child.reuseAddress", configuration.isReuseAddress());
        connectionlessClientBootstrap.setOption("child.connectTimeoutMillis", configuration.getConnectTimeout());
        connectionlessClientBootstrap.setOption("child.broadcast", configuration.isBroadcast());
        connectionlessClientBootstrap.setOption("sendBufferSize", configuration.getSendBufferSize());
        connectionlessClientBootstrap.setOption("receiveBufferSize", configuration.getReceiveBufferSize());
        // set any additional netty options
        if (configuration.getOptions() != null) {
            for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) {
                connectionlessClientBootstrap.setOption(entry.getKey(), entry.getValue());
            }
        }
        // set the pipeline factory, which creates the pipeline for each newly created channels
        connectionlessClientBootstrap.setPipelineFactory(pipelineFactory);
        // if no one is listen on the port
        if (!configuration.isUdpConnectionlessSending()) {
            answer = connectionlessClientBootstrap.connect(new InetSocketAddress(configuration.getHost(), configuration.getPort()));
        } else {
            // bind and store channel so we can close it when stopping
            Channel channel = connectionlessClientBootstrap.bind(new InetSocketAddress(0));
            allChannels.add(channel);
            answer = new SucceededChannelFuture(channel);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created new UDP client bootstrap connecting to {}:{} with options: {}", new Object[] { configuration.getHost(), configuration.getPort(), connectionlessClientBootstrap.getOptions() });
        }
        return answer;
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) Map(java.util.Map) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Example 22 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project jstorm by alibaba.

the class NettyClient method start.

public void start() {
    bootstrap = new ClientBootstrap(clientChannelFactory);
    bootstrap.setOption("tcpNoDelay", true);
    bootstrap.setOption("reuseAddress", true);
    bootstrap.setOption("sendBufferSize", bufferSize);
    bootstrap.setOption("keepAlive", true);
    // Set up the pipeline factory.
    bootstrap.setPipelineFactory(new StormClientPipelineFactory(this, stormConf));
    reconnect();
}
Also used : ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap)

Example 23 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project opennms by OpenNMS.

the class AsyncBasicDetectorNettyImpl method isServiceDetected.

/** {@inheritDoc} */
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
    DetectFuture detectFuture = new DetectFutureFailedImpl(this, new IllegalStateException());
    try {
        ClientBootstrap bootstrap = new ClientBootstrap(m_factory);
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline retval = Channels.pipeline();
                // Upstream handlers
                //retval.addLast("retryHandler", new RetryChannelHandler());
                appendToPipeline(retval);
                // Downstream handlers
                retval.addLast("detectorHandler", getDetectorHandler(getConversation()));
                if (isUseSSLFilter()) {
                    // Use a relaxed SSL context
                    retval.addLast("sslHandler", new SslHandler(createClientSSLContext().createSSLEngine()));
                }
                return retval;
            }
        });
        bootstrap.setOption("tcpNoDelay", true);
        bootstrap.setOption("keepAlive", true);
        SocketAddress remoteAddress = new InetSocketAddress(address, getPort());
        ChannelFuture future = bootstrap.connect(remoteAddress);
        future.addListener(new RetryChannelFutureListener(remoteAddress, this.getRetries()));
        detectFuture = new DetectFutureNettyImpl(this, future);
    } catch (Throwable e) {
        detectFuture = new DetectFutureFailedImpl(this, e);
    }
    return detectFuture;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) ServiceDetectionFailedException(org.opennms.netmgt.provision.support.DetectFutureNettyImpl.ServiceDetectionFailedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SslHandler(org.jboss.netty.handler.ssl.SslHandler) DetectFuture(org.opennms.netmgt.provision.DetectFuture) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory)

Example 24 with ClientBootstrap

use of org.jboss.netty.bootstrap.ClientBootstrap in project motan by weibocom.

the class NettyClient method initClientBootstrap.

/**
	 * 初始化 netty clientBootstrap
	 */
private void initClientBootstrap() {
    bootstrap = new ClientBootstrap(channelFactory);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    // 实际上,极端情况下,connectTimeout会达到500ms,因为netty nio的实现中,是依赖BossThread来控制超时,
    // 如果为了严格意义的timeout,那么需要应用端进行控制。
    int timeout = getUrl().getIntParameter(URLParamType.connectTimeout.getName(), URLParamType.connectTimeout.getIntValue());
    if (timeout <= 0) {
        throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    bootstrap.setOption("connectTimeoutMillis", timeout);
    // 最大响应包限制
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyClient.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder(codec, NettyClient.this));
            pipeline.addLast("handler", new NettyChannelHandler(NettyClient.this, new MessageHandler() {

                @Override
                public Object handle(Channel channel, Object message) {
                    Response response = (Response) message;
                    NettyResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId());
                    if (responseFuture == null) {
                        LoggerUtil.warn("NettyClient has response from server, but resonseFuture not exist,  requestId={}", response.getRequestId());
                        return null;
                    }
                    if (response.getException() != null) {
                        responseFuture.onFailure(response);
                    } else {
                        responseFuture.onSuccess(response);
                    }
                    return null;
                }
            }));
            return pipeline;
        }
    });
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Channel(com.weibo.api.motan.transport.Channel) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory)

Aggregations

ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)24 InetSocketAddress (java.net.InetSocketAddress)10 ChannelFuture (org.jboss.netty.channel.ChannelFuture)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)10 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)7 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)7 Channel (org.jboss.netty.channel.Channel)4 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2 NioClientBossPool (org.jboss.netty.channel.socket.nio.NioClientBossPool)2 NioWorkerPool (org.jboss.netty.channel.socket.nio.NioWorkerPool)2 HttpClientCodec (org.jboss.netty.handler.codec.http.HttpClientCodec)2 HttpRequestEncoder (org.jboss.netty.handler.codec.http.HttpRequestEncoder)2 HttpResponseDecoder (org.jboss.netty.handler.codec.http.HttpResponseDecoder)2 LoggingHandler (org.jboss.netty.handler.logging.LoggingHandler)2 HashedWheelTimer (org.jboss.netty.util.HashedWheelTimer)2 Checkpoint (com.linkedin.databus.core.Checkpoint)1 FooterAwareHttpChunkAggregator (com.linkedin.databus.core.test.netty.FooterAwareHttpChunkAggregator)1 SimpleHttpResponseHandler (com.linkedin.databus.core.test.netty.SimpleHttpResponseHandler)1