Search in sources :

Example 1 with SucceededChannelFuture

use of org.jboss.netty.channel.SucceededChannelFuture in project opentsdb by OpenTSDB.

the class TestRpcHandler method emptyPathIsBadRequest.

@Test
public void emptyPathIsBadRequest() throws Exception {
    final HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "");
    final Channel mockChan = handleHttpRpc(req, new Answer<ChannelFuture>() {

        public ChannelFuture answer(final InvocationOnMock args) throws Throwable {
            DefaultHttpResponse response = (DefaultHttpResponse) args.getArguments()[0];
            assertEquals(HttpResponseStatus.BAD_REQUEST, response.getStatus());
            return new SucceededChannelFuture((Channel) args.getMock());
        }
    });
    final RpcHandler rpc = new RpcHandler(tsdb, rpc_manager);
    Whitebox.invokeMethod(rpc, "handleHttpQuery", tsdb, mockChan, req);
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Channel(org.jboss.netty.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with SucceededChannelFuture

use of org.jboss.netty.channel.SucceededChannelFuture 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)

Aggregations

Channel (org.jboss.netty.channel.Channel)2 ChannelFuture (org.jboss.netty.channel.ChannelFuture)2 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)2 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)1 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)1 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)1 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1