Search in sources :

Example 6 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project neo4j by neo4j.

the class BufferReusingChunkingChannelBufferTest method triggerOperationCompleteCallback.

private static ChannelBuffer triggerOperationCompleteCallback(BufferReusingChunkingChannelBuffer buffer) throws Exception {
    ChannelBuffer reusedBuffer = spy(ChannelBuffers.dynamicBuffer());
    ChannelFuture channelFuture = mock(ChannelFuture.class);
    when(channelFuture.isDone()).thenReturn(true);
    when(channelFuture.isSuccess()).thenReturn(true);
    buffer.newChannelFutureListener(reusedBuffer).operationComplete(channelFuture);
    return reusedBuffer;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 7 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project weave by continuuity.

the class SimpleKafkaClient method preparePublish.

@Override
public PreparePublish preparePublish(final String topic, final Compression compression) {
    final Map<Integer, MessageSetEncoder> encoders = Maps.newHashMap();
    return new PreparePublish() {

        @Override
        public PreparePublish add(byte[] payload, Object partitionKey) {
            return add(ByteBuffer.wrap(payload), partitionKey);
        }

        @Override
        public PreparePublish add(ByteBuffer payload, Object partitionKey) {
            // TODO: Partition
            int partition = 0;
            MessageSetEncoder encoder = encoders.get(partition);
            if (encoder == null) {
                encoder = getEncoder(compression);
                encoders.put(partition, encoder);
            }
            encoder.add(ChannelBuffers.wrappedBuffer(payload));
            return this;
        }

        @Override
        public ListenableFuture<?> publish() {
            List<ListenableFuture<?>> futures = Lists.newArrayListWithCapacity(encoders.size());
            for (Map.Entry<Integer, MessageSetEncoder> entry : encoders.entrySet()) {
                futures.add(doPublish(topic, entry.getKey(), entry.getValue().finish()));
            }
            encoders.clear();
            return Futures.allAsList(futures);
        }

        private ListenableFuture<?> doPublish(String topic, int partition, ChannelBuffer messageSet) {
            final KafkaRequest request = KafkaRequest.createProduce(topic, partition, messageSet);
            final SettableFuture<?> result = SettableFuture.create();
            final ConnectionPool.ConnectResult connection = connectionPool.connect(getTopicBroker(topic, partition).getAddress());
            connection.getChannelFuture().addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    try {
                        future.getChannel().write(request).addListener(getPublishChannelFutureListener(result, null, connection));
                    } catch (Exception e) {
                        result.setException(e);
                    }
                }
            });
            return result;
        }
    };
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) PreparePublish(com.continuuity.weave.kafka.client.PreparePublish) ByteBuffer(java.nio.ByteBuffer) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) FetchException(com.continuuity.weave.kafka.client.FetchException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Map(java.util.Map)

Example 8 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project weave by continuuity.

the class ConnectionPool method connect.

ConnectResult connect(InetSocketAddress address) {
    Queue<ChannelFuture> channelFutures = connections.get(address);
    if (channelFutures == null) {
        channelFutures = new ConcurrentLinkedQueue<ChannelFuture>();
        Queue<ChannelFuture> result = connections.putIfAbsent(address, channelFutures);
        channelFutures = result == null ? channelFutures : result;
    }
    ChannelFuture channelFuture = channelFutures.poll();
    while (channelFuture != null) {
        if (channelFuture.isSuccess() && channelFuture.getChannel().isConnected()) {
            return new SimpleConnectResult(address, channelFuture);
        }
        channelFuture = channelFutures.poll();
    }
    channelFuture = bootstrap.connect(address);
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                channelGroup.add(future.getChannel());
            }
        }
    });
    return new SimpleConnectResult(address, channelFuture);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener)

Example 9 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project neo4j by neo4j.

the class ChunkingChannelBuffer method writeCurrentChunk.

private void writeCurrentChunk() {
    if (!channel.isOpen() || !channel.isConnected() || !channel.isBound()) {
        throw new ComException("Channel has been closed, so no need to try to write to it anymore. Client closed it?");
    }
    waitForClientToCatchUpOnReadingChunks();
    ChannelFuture future = channel.write(buffer);
    future.addListener(newChannelFutureListener(buffer));
    writeAheadCounter.incrementAndGet();
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 10 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project graylog2-server by Graylog2.

the class GELFHttpHandlerTest method setUp.

@Before
public void setUp() throws Exception {
    ChannelBuffer channelBuffer = ChannelBuffers.copiedBuffer("{}", StandardCharsets.UTF_8);
    when(headers.get(HttpHeaders.Names.CONNECTION)).thenReturn(HttpHeaders.Values.CLOSE);
    when(request.getMethod()).thenReturn(HttpMethod.POST);
    when(request.headers()).thenReturn(headers);
    when(request.getProtocolVersion()).thenReturn(HttpVersion.HTTP_1_1);
    when(request.getContent()).thenReturn(channelBuffer);
    when(request.getUri()).thenReturn("/gelf");
    when(ctx.getChannel()).thenReturn(mock(Channel.class));
    ChannelFuture channelFuture = mock(ChannelFuture.class);
    when(channel.write(any())).thenReturn(channelFuture);
    when(evt.getMessage()).thenReturn(request);
    when(evt.getChannel()).thenReturn(channel);
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) Channel(org.jboss.netty.channel.Channel) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Before(org.junit.Before)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)138 Channel (org.jboss.netty.channel.Channel)40 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)38 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)28 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)27 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)26 InetSocketAddress (java.net.InetSocketAddress)25 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)23 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)23 Test (org.junit.Test)15 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)13 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 ConnectException (java.net.ConnectException)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)6