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);
}
}
});
}
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;
}
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;
}
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");
}
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");
}
Aggregations