List of usage examples for io.netty.util.concurrent Future isSuccess
boolean isSuccess();
From source file:com.github.milenkovicm.kafka.ListenerTest.java
License:Apache License
@Test public void test_success() throws Exception { String topic = "test_success"; createTopic(topic, 1);/*from ww w . j ava2 s . com*/ ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); KafkaProducer producer = new KafkaProducer("localhost", START_PORT, topic, properties); producer.connect().sync(); KafkaTopic kafkaTopic = producer.topic(); final CountDownLatch latch = new CountDownLatch(1); final Future send = kafkaTopic.send(null, freeLaterBuffer(TEST_MESSAGE.getBytes())); send.addListener(new FutureListener() { @Override public void operationComplete(Future future) throws Exception { if (future.isSuccess()) { latch.countDown(); } } }); final List<KafkaStream<byte[], byte[]>> consume = consume(topic); final KafkaStream<byte[], byte[]> stream = consume.get(0); final ConsumerIterator<byte[], byte[]> messages = stream.iterator(); Assert.assertThat(TEST_MESSAGE, is(new String(messages.next().message()))); Assert.assertTrue(latch.await(2, TimeUnit.SECONDS)); producer.disconnect().sync(); }
From source file:com.github.milenkovicm.kafka.ListenerTest.java
License:Apache License
@Test public void test_producer_multi_message() throws Exception { String topic = "test_producer_multi_message"; ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); createTopic(topic);/*from w w w.ja va2s . com*/ final CountDownLatch latch = new CountDownLatch(3); FutureListener listener = new FutureListener() { @Override public void operationComplete(Future future) throws Exception { if (future.isSuccess()) { latch.countDown(); } } }; KafkaProducer producer = new KafkaProducer("localhost", START_PORT, topic, properties); producer.connect().sync(); KafkaTopic kafkaTopic = producer.topic(); kafkaTopic.send(null, freeLaterBuffer((TEST_MESSAGE + "01").getBytes())).addListener(listener); kafkaTopic.send(null, freeLaterBuffer((TEST_MESSAGE + "02").getBytes())).addListener(listener); kafkaTopic.send(null, freeLaterBuffer((TEST_MESSAGE + "03").getBytes())).addListener(listener); final KafkaStream<byte[], byte[]> stream = consume(topic).get(0); final ConsumerIterator<byte[], byte[]> messages = stream.iterator(); Assert.assertThat(new String(messages.next().message()), is(TEST_MESSAGE + "01")); Assert.assertThat(new String(messages.next().message()), is(TEST_MESSAGE + "02")); Assert.assertThat(new String(messages.next().message()), is(TEST_MESSAGE + "03")); Assert.assertTrue("latch failed", latch.await(2, TimeUnit.SECONDS)); producer.disconnect().sync(); }
From source file:com.github.milenkovicm.kafka.ListenerTest.java
License:Apache License
@Test @Ignore("not sure how to test this one") public void test_fail() throws Exception { String topic = "doesnotexist"; ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); KafkaProducer producer = new KafkaProducer("localhost", START_PORT, topic, properties); producer.connect().sync();/*w w w .j a va 2 s . c om*/ KafkaTopic kafkaTopic = producer.topic(); final CountDownLatch latch = new CountDownLatch(1); final Future<Void> send = kafkaTopic.send(null, freeLaterBuffer(TEST_MESSAGE.getBytes())); send.addListener(new FutureListener<Void>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (!future.isSuccess()) { latch.countDown(); } } }); Assert.assertTrue(latch.await(2, TimeUnit.SECONDS)); producer.disconnect().sync(); }
From source file:com.github.milenkovicm.kafka.NoTopicTest.java
License:Apache License
@Test public void test_producer_no_topic_async() throws Exception { final CountDownLatch latch = new CountDownLatch(1); String topic = "test_producer_no_topic__async"; ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); //createTopic(topic); KafkaProducer producer = new KafkaProducer("localhost", START_PORT, topic, properties); final Future<Void> connect = producer.connect(); connect.addListener(new GenericFutureListener<Future<? super Void>>() { @Override//w w w . j av a 2 s. co m public void operationComplete(Future<? super Void> future) throws Exception { latch.countDown(); } }); latch.await(5, TimeUnit.SECONDS); Assert.assertThat(connect.isDone(), is(true)); Assert.assertThat(connect.isSuccess(), is(false)); Assert.assertThat(connect.cause(), notNullValue()); Assert.assertThat(((KafkaException) connect.cause()).error, is(Error.LEADER_NOT_AVAILABLE)); producer.disconnect(); }
From source file:com.github.mrstampy.gameboot.netty.AbstractNettyProcessor.java
License:Open Source License
private void log(Future<? super Void> f, Response response, ChannelHandlerContext ctx) { ResponseCode rc = response.getResponseCode(); Integer id = response.getId(); if (f.isSuccess()) { log.debug("Successfully sent response code {}, id {} to {}", rc, id, ctx.channel()); } else {/*from ww w. j a v a 2 s.co m*/ log.error("Could not send response code {}, id {} to {}", rc, id, ctx.channel(), f.cause()); } }
From source file:com.github.mrstampy.gameboot.otp.netty.client.ClientHandler.java
License:Open Source License
private void validate(Future<? super Channel> f, ChannelHandlerContext ctx) { if (f.isSuccess()) { log.debug("Handshake successful with {}", ctx.channel()); } else {/* ww w .j ava2s . c o m*/ log.error("Handshake unsuccessful, disconnecting {}", ctx.channel(), f.cause()); ctx.close(); } }
From source file:com.github.mrstampy.gameboot.otp.netty.OtpEncryptedNettyHandler.java
License:Open Source License
private void log(Future<? super Void> f, ChannelHandlerContext ctx, String type) { if (f.isSuccess()) { log.debug("Successful send of {} to {}, closing channel", type, ctx.channel().remoteAddress()); } else {// w w w .j ava2 s. c o m log.error("Unsuccessful send of {} to {}, closing channel", type, ctx.channel().remoteAddress(), f.cause()); } ctx.close(); }
From source file:com.github.netfreer.shadowducks.client.handler.SocksServerConnectHandler.java
License:Apache License
@Override public void channelRead0(final ChannelHandlerContext ctx, final SocksMessage message) throws Exception { if (message instanceof Socks4CommandRequest) { final Socks4CommandRequest request = (Socks4CommandRequest) message; Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new FutureListener<Channel>() { @Override/*from w w w.ja v a2s.co m*/ public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ChannelFuture responseFuture = ctx.channel() .writeAndFlush(new DefaultSocks4CommandResponse(Socks4CommandStatus.SUCCESS)); responseFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel().writeAndFlush( new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.dstAddr(), request.dstPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection established use handler provided results } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush( new DefaultSocks4CommandResponse(Socks4CommandStatus.REJECTED_OR_FAILED)); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); } else if (message instanceof Socks5CommandRequest) { final Socks5CommandRequest request = (Socks5CommandRequest) message; Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new FutureListener<Channel>() { @Override public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ChannelFuture responseFuture = ctx.channel() .writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, request.dstAddrType(), request.dstAddr(), request.dstPort())); responseFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(SocksServerConnectHandler.this); outboundChannel.pipeline().addLast(new RelayHandler(ctx.channel())); ctx.pipeline().addLast(new RelayHandler(outboundChannel)); } }); } else { ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); SocksServerUtils.closeOnFlush(ctx.channel()); } } }); final Channel inboundChannel = ctx.channel(); b.group(inboundChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).option(ChannelOption.SO_KEEPALIVE, true) .handler(new DirectClientHandler(promise)); b.connect(request.dstAddr(), request.dstPort()).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { Attribute<Long> beginTimeAttr = ctx.channel().attr(AttrKeys.CHANNEL_BEGIN_TIME); final long parseTime = beginTimeAttr.get(); long usedTime = System.currentTimeMillis() - parseTime; if (future.isSuccess()) { // Connection established use handler provided results logger.info("connect {}:{} success, use time {} millis.", request.dstAddr(), request.dstPort(), usedTime); } else { // Close the connection if the connection attempt has failed. ctx.channel().writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, request.dstAddrType())); SocksServerUtils.closeOnFlush(ctx.channel()); logger.info("connect {}:{} failure, use time {} millis.", request.dstAddr(), request.dstPort(), usedTime); } beginTimeAttr.set(null); } }); } else { ctx.close(); } }
From source file:com.github.sinsinpub.pero.backend.ConnectBackendHandler.java
License:Apache License
/** * Create new promised callback on outbound channel operation complete. * /* w w w. j a v a 2 s. co m*/ * @param ctx * @param request * @return Promise */ protected Promise<Channel> newOutboundPromise(final ChannelHandlerContext ctx, final SocksCmdRequest request) { final Promise<Channel> promise = ctx.executor().newPromise(); promise.addListener(new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(final Future<Channel> future) throws Exception { final Channel outboundChannel = future.getNow(); if (future.isSuccess()) { ctx.channel().writeAndFlush(new SocksCmdResponse(SocksCmdStatus.SUCCESS, request.addressType())) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { ctx.pipeline().remove(ConnectBackendHandler.this); outboundChannel.pipeline().addLast(new RelayTrafficHandler(ctx.channel())); ctx.pipeline().addLast(new RelayTrafficHandler(outboundChannel)); } }); } else { ctx.channel() .writeAndFlush(new SocksCmdResponse(SocksCmdStatus.FAILURE, request.addressType())); NettyChannelUtils.closeOnFlush(ctx.channel()); } } }); return promise; }
From source file:com.github.spapageo.jannel.client.ClientSession.java
License:Open Source License
/** * Asynchronously sends an sms/*from w w w. j av a 2s . c o m*/ * @param sms the sms to send * @param timeoutMillis the timeout for an open window slot to appear * @return the future on the operation * @throws InterruptedException when the operation was interrupted */ @SuppressWarnings("unchecked") @Nonnull public WindowFuture<Sms, Ack> sendSms(final Sms sms, final long timeoutMillis) throws InterruptedException { // Generate UUID if null if (sms.getId() == null) { sms.setId(UUID.randomUUID()); } // Apply the current client id if null if (sms.getBoxId() == null) sms.setBoxId(configuration.getClientId()); WindowFuture future = sendWindow.offer(sms.getId(), sms, timeoutMillis, configuration.getRequestExpiryTimeout()); sendMessage(sms).addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(Future<? super Void> channelFuture) throws Exception { if (!channelFuture.isSuccess() && !channelFuture.isCancelled()) { sendWindow.fail(sms.getId(), channelFuture.cause()); } else if (channelFuture.isCancelled()) { sendWindow.cancel(sms.getId(), true); } } }); return future; }