Search in sources :

Example 76 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project storm by apache.

the class Client method flushMessages.

/**
     * Asynchronously writes the message batch to the channel.
     *
     * If the write operation fails, then we will close the channel and trigger a reconnect.
     */
private void flushMessages(Channel channel, final MessageBatch batch) {
    if (null == batch || batch.isEmpty()) {
        return;
    }
    final int numMessages = batch.size();
    LOG.debug("writing {} messages to channel {}", batch.size(), channel.toString());
    pendingMessages.addAndGet(numMessages);
    ChannelFuture future = channel.write(batch);
    future.addListener(new ChannelFutureListener() {

        public void operationComplete(ChannelFuture future) throws Exception {
            pendingMessages.addAndGet(0 - numMessages);
            if (future.isSuccess()) {
                LOG.debug("sent {} messages to {}", numMessages, dstAddressPrefixedName);
                messagesSent.getAndAdd(batch.size());
            } else {
                LOG.error("failed to send {} messages to {}: {}", numMessages, dstAddressPrefixedName, future.getCause());
                closeChannelAndReconnect(future.getChannel());
                messagesLost.getAndAdd(numMessages);
            }
        }
    });
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) InterruptedException(java.lang.InterruptedException)

Example 77 with ChannelFuture

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

the class SimpleKafkaClient method getOffset.

@Override
public ListenableFuture<long[]> getOffset(final String topic, final int partition, long time, int maxOffsets) {
    final SettableFuture<long[]> resultFuture = SettableFuture.create();
    final ChannelBuffer body = ChannelBuffers.buffer(Longs.BYTES + Ints.BYTES);
    body.writeLong(time);
    body.writeInt(maxOffsets);
    connectionPool.connect(getTopicBroker(topic, partition).getAddress()).getChannelFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (checkFailure(future)) {
                return;
            }
            future.getChannel().write(KafkaRequest.createOffsets(topic, partition, body, new ResponseHandler() {

                @Override
                public void received(KafkaResponse response) {
                    if (response.getErrorCode() != FetchException.ErrorCode.OK) {
                        resultFuture.setException(new FetchException("Failed to fetch offset.", response.getErrorCode()));
                    } else {
                        // Decode the offset response, which contains 4 bytes number of offsets, followed by number of offsets,
                        // each 8 bytes in size.
                        ChannelBuffer resultBuffer = response.getBody();
                        int size = resultBuffer.readInt();
                        long[] result = new long[size];
                        for (int i = 0; i < size; i++) {
                            result[i] = resultBuffer.readLong();
                        }
                        resultFuture.set(result);
                    }
                }
            })).addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    checkFailure(future);
                }
            });
        }

        private boolean checkFailure(ChannelFuture future) {
            if (!future.isSuccess()) {
                if (future.isCancelled()) {
                    resultFuture.cancel(true);
                } else {
                    resultFuture.setException(future.getCause());
                }
                return true;
            }
            return false;
        }
    });
    return resultFuture;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) FetchException(com.continuuity.weave.kafka.client.FetchException) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) FetchException(com.continuuity.weave.kafka.client.FetchException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 78 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class ChannelCloseFaultInjectionRequestProcessor method process.

@Override
public DatabusRequest process(DatabusRequest request) throws IOException, RequestProcessingException {
    // Close the channel
    LOG.debug("Waiting for raw channel to close");
    ChannelFuture future = request.getResponseContent().getRawChannel().close().awaitUninterruptibly();
    try {
        future.await();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    LOG.debug("Done waiting for raw channel to close");
    return request;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture)

Example 79 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class DummyHttpRequestHandler method testWriteTwoChunks.

@Test
public void testWriteTwoChunks() {
    LOG.info("Start: Testing headers with one chunk");
    setupClient();
    String chunk1 = "hello";
    String chunk2 = "bye";
    ArrayList<byte[]> chunks = new ArrayList<byte[]>();
    chunks.add(chunk1.getBytes(Charset.defaultCharset()));
    chunks.add(chunk2.getBytes(Charset.defaultCharset()));
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("header1", "value1");
    headers.put("header2", "value2");
    HashMap<String, String> footers = new HashMap<String, String>();
    footers.put("footer1", "1value");
    footers.put("footer2", "2value");
    setupServer(HttpResponseStatus.OK, chunks, headers, footers);
    ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
    connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", connectFuture.isSuccess());
    HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
    Channel requestChannel = connectFuture.getChannel();
    ChannelFuture writeFuture = requestChannel.write(request);
    writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", writeFuture.isSuccess());
    HttpResponse response = _responseHandler.getResponse();
    assertEquals("response code", Integer.toString(HttpResponseStatus.OK.getCode()), response.getHeader(ChunkedBodyWritableByteChannel.RESPONSE_CODE_FOOTER_NAME));
    assertEquals("Checking header1 value", "value1", response.getHeader("header1"));
    assertEquals("Checking header2 value", "value2", response.getHeader("header2"));
    assertEquals("Checking footer1 value", "1value", response.getHeader("footer1"));
    assertEquals("Checking footer2 value", "2value", response.getHeader("footer2"));
    byte[] responseBody = _responseHandler.getReceivedBytes();
    assertEquals("response length", chunk1.getBytes(Charset.defaultCharset()).length + chunk2.getBytes(Charset.defaultCharset()).length, responseBody.length);
    byte[] fullBody = new byte[chunk1.getBytes(Charset.defaultCharset()).length + chunk2.getBytes(Charset.defaultCharset()).length];
    System.arraycopy(chunk1.getBytes(Charset.defaultCharset()), 0, fullBody, 0, chunk1.getBytes(Charset.defaultCharset()).length);
    System.arraycopy(chunk2.getBytes(Charset.defaultCharset()), 0, fullBody, chunk1.getBytes(Charset.defaultCharset()).length, chunk2.getBytes(Charset.defaultCharset()).length);
    assertTrue("response content", Arrays.equals(fullBody, responseBody));
    LOG.info("Done: Testing headers with one chunk");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Example 80 with ChannelFuture

use of org.jboss.netty.channel.ChannelFuture in project databus by linkedin.

the class DummyHttpRequestHandler method testWriteEmptyResponse.

@Test
public void testWriteEmptyResponse() {
    LOG.info("Start: Testing empty response");
    setupClient();
    ArrayList<byte[]> chunks = new ArrayList<byte[]>();
    HashMap<String, String> headers = new HashMap<String, String>();
    HashMap<String, String> footers = new HashMap<String, String>();
    setupServer(HttpResponseStatus.OK, chunks, headers, footers);
    ChannelFuture connectFuture = _clientBootstrap.connect(_serverAddress);
    connectFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", connectFuture.isSuccess());
    HttpRequest request = new DefaultHttpRequest(HTTP_1_1, HttpMethod.GET, "/test");
    Channel requestChannel = connectFuture.getChannel();
    ChannelFuture writeFuture = requestChannel.write(request);
    writeFuture.awaitUninterruptibly(1, TimeUnit.SECONDS);
    assertTrue("connect succeeded", writeFuture.isSuccess());
    HttpResponse response = _responseHandler.getResponse();
    assertEquals("response code", HttpResponseStatus.OK, response.getStatus());
    byte[] responseBody = _responseHandler.getReceivedBytes();
    assertTrue("empty response", null == responseBody || responseBody.length == 0);
    LOG.info("Done: Testing empty response");
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) HashMap(java.util.HashMap) DefaultHttpRequest(org.jboss.netty.handler.codec.http.DefaultHttpRequest) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) Channel(org.jboss.netty.channel.Channel) ArrayList(java.util.ArrayList) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) Test(org.testng.annotations.Test)

Aggregations

ChannelFuture (org.jboss.netty.channel.ChannelFuture)122 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)36 Channel (org.jboss.netty.channel.Channel)33 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)29 ChannelFutureListener (org.jboss.netty.channel.ChannelFutureListener)26 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)25 InetSocketAddress (java.net.InetSocketAddress)22 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)22 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)19 SucceededChannelFuture (org.jboss.netty.channel.SucceededChannelFuture)13 Test (org.junit.Test)13 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)8 Test (org.testng.annotations.Test)8 ConnectException (java.net.ConnectException)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6