List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup()
From source file:com.github.liyp.netty.App.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try {/*from w ww. ja va2s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ReplayingDecoder() { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { short magicHeader = in.readShort(); logger.debug("Receive magic header: {}.", magicHeader); if (magicHeader != HEADER) { logger.error("Receive illegal magic header: {}, close channel: {}.", magicHeader, ctx.channel().remoteAddress()); ctx.close(); } short dataLen = in.readShort(); logger.debug("Receive message data length: {}.", dataLen); if (dataLen < 0) { logger.error("Data length is negative, close channel: {}.", ctx.channel().remoteAddress()); ctx.close(); } ByteBuf payload = in.readBytes(dataLen); String cloudMsg = payload.toString(CharsetUtil.UTF_8); logger.debug("Receive data: {}.", cloudMsg); out.add(cloudMsg); } }).addLast(new MessageToByteEncoder<String>() { @Override protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception { out.writeBytes(msg.getBytes()); } }).addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("start receive msg..."); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { logger.info("receive msg: {}", msg); logger.info("echo msg"); ctx.writeAndFlush(msg); } }); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(PORT).sync(); logger.info("9999"); f.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); boss.shutdownGracefully(); } }
From source file:com.github.liyp.netty.HandlerChainApp.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try {/* w w w. ja v a 2 s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(boss, worker).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("1"); ctx.fireChannelActive(); } }).addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("2"); ctx.fireChannelActive(); } }).addLast(new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("3"); ctx.fireChannelActive(); } }); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(PORT).sync(); logger.info("9999"); f.channel().closeFuture().sync(); } finally { worker.shutdownGracefully(); boss.shutdownGracefully(); } }
From source file:com.github.milenkovicm.kafka.ControlBrokerTest.java
License:Apache License
@Test public void test_fetchMetadata() throws Exception { createTopic(topic);/*ww w . ja v a 2 s .c o m*/ ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); ControlKafkaBroker controlKafkaChannel = new ControlKafkaBroker("localhost", START_PORT, topic, new NioEventLoopGroup(), properties); controlKafkaChannel.connect().sync(); final CountDownLatch latch = new CountDownLatch(1); final KafkaPromise future = controlKafkaChannel.fetchMetadata(topic); future.addListener(new FutureListener<MetadataResponse>() { @Override public void operationComplete(Future<MetadataResponse> future) throws Exception { Assert.assertNotNull(future.get()); latch.countDown(); } }); Assert.assertTrue("latch not triggered", latch.await(2, TimeUnit.SECONDS)); controlKafkaChannel.disconnect(); }
From source file:com.github.milenkovicm.kafka.ControlBrokerTest.java
License:Apache License
@Test public void test_fetchMetadata_notExistingTopic() throws Exception { String topicName = "topic_does_not_exist"; ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); ControlKafkaBroker controlKafkaChannel = new ControlKafkaBroker("localhost", START_PORT, topicName, new NioEventLoopGroup(), properties); controlKafkaChannel.connect().sync(); try {//from w w w .jav a 2s . c o m controlKafkaChannel.fetchMetadata(topicName).sync(); Assert.fail("shouldn't get here"); } catch (KafkaException e) { Assert.assertThat(e.error, is(Error.LEADER_NOT_AVAILABLE)); } controlKafkaChannel.disconnect(); }
From source file:com.github.milenkovicm.kafka.DataBrokerTest.java
License:Apache License
@Test public void test_sendMessage() throws Exception { createTopic(topic);/*from www. ja v a2 s . co m*/ CountDownLatch latch = new CountDownLatch(1); ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); DataKafkaBroker dataChannel = new DataKafkaBroker("localhost", START_PORT, 0, topic, new NioEventLoopGroup(), properties); dataChannel.connect().sync(); dataChannel.send(freeLaterBuffer("1".getBytes()), 0, freeLaterBuffer(TEST_MESSAGE.getBytes())); 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)); dataChannel.disconnect(); }
From source file:com.github.milenkovicm.kafka.DataBrokerTest.java
License:Apache License
@Test(expected = KafkaException.class) public void test_sendMessage_unknownTopic() throws Exception { ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); DataKafkaBroker dataChannel = new DataKafkaBroker("localhost", START_PORT, 0, "unknown_topic", new NioEventLoopGroup(), properties); dataChannel.connect().sync();// www. jav a2 s . c o m final ChannelFuture future = dataChannel.send(freeLaterBuffer("1".getBytes()), 0, freeLaterBuffer(TEST_MESSAGE.getBytes())); future.sync(); dataChannel.disconnect(); }
From source file:com.github.milenkovicm.kafka.DataBrokerTest.java
License:Apache License
@Test public void test_sendMessage_unknownTopicNoAck() throws Exception { ProducerProperties properties = new ProducerProperties(); properties.override(ProducerProperties.NETTY_DEBUG_PIPELINE, true); properties.override(ProducerProperties.DATA_ACK, Acknowledgment.WAIT_FOR_NO_ONE); DataKafkaBroker dataChannel = new DataKafkaBroker("localhost", START_PORT, 0, "unknown_topic", new NioEventLoopGroup(), properties); dataChannel.connect().sync();/* w w w . j a va 2 s . c o m*/ final ChannelFuture future = dataChannel.send(freeLaterBuffer("1".getBytes()), 0, freeLaterBuffer(TEST_MESSAGE.getBytes())); future.sync(); dataChannel.disconnect(); }
From source file:com.github.mrstampy.gameboot.otp.netty.OtpNettyTestConfiguration.java
License:Open Source License
/** * Encrypted client bootstrap.//from w ww.j a va 2 s .c o m * * @return the bootstrap * @throws Exception * the exception */ @Bean(name = CLIENT_ENCRYPTED_BOOTSTRAP) public Bootstrap encryptedClientBootstrap() throws Exception { //@formatter:off return new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup()) .handler(encryptedClientInitializer()); //@formatter:on }
From source file:com.github.mrstampy.gameboot.otp.netty.OtpNettyTestConfiguration.java
License:Open Source License
/** * Clear client bootstrap./*from w ww. j a va 2 s.c o m*/ * * @return the bootstrap * @throws Exception * the exception */ @Bean(name = CLIENT_CLEAR_BOOTSTRAP) public Bootstrap clearClientBootstrap() throws Exception { //@formatter:off return new Bootstrap().channel(NioSocketChannel.class).group(new NioEventLoopGroup()) .handler(clearClientInitializer()); //@formatter:on }
From source file:com.github.mrstampy.gameboot.otp.netty.OtpNettyTestConfiguration.java
License:Open Source License
/** * Server encrypted bootstrap./*w w w.j ava 2 s . c o m*/ * * @return the server bootstrap * @throws Exception * the exception */ @Bean(name = SERVER_ENCRYPTED_BOOTSTRAP) public ServerBootstrap serverEncryptedBootstrap() throws Exception { //@formatter:off return new ServerBootstrap().channel(NioServerSocketChannel.class) .group(new NioEventLoopGroup(), new NioEventLoopGroup()).childHandler(encryptedServerInitializer()); //@formatter:on }