List of usage examples for io.netty.buffer PooledByteBufAllocator DEFAULT
PooledByteBufAllocator DEFAULT
To view the source code for io.netty.buffer PooledByteBufAllocator DEFAULT.
Click Source Link
From source file:com.uber.tchannel.handlers.InitDefaultRequestHandlerTest.java
License:Open Source License
@Test public void testInitHandlerRemovesItself() throws Exception { // Given/*from w w w .jav a 2 s . co m*/ EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler(new PeerManager(new Bootstrap()))); assertEquals(3, channel.pipeline().names().size()); InitRequestFrame initRequestFrame = new InitRequestFrame(42, InitFrame.DEFAULT_VERSION, new HashMap<String, String>() { { put(InitFrame.HOST_PORT_KEY, "0.0.0.0:0"); put(InitFrame.PROCESS_NAME_KEY, "test-process"); } }); channel.writeInbound( MessageCodec.encode(MessageCodec.encode(PooledByteBufAllocator.DEFAULT, initRequestFrame))); channel.writeOutbound(channel.readInbound()); // Then TFrame tFrame = MessageCodec.decode((ByteBuf) channel.readOutbound()); InitResponseFrame initResponseFrame = (InitResponseFrame) MessageCodec.decode(tFrame); tFrame.release(); // Assert assertNotNull(initResponseFrame); assertEquals(initRequestFrame.getId(), initResponseFrame.getId()); assertEquals(initRequestFrame.getVersion(), initResponseFrame.getVersion()); assertEquals(initRequestFrame.getHostPort(), initResponseFrame.getHostPort()); // Assert Pipeline is empty assertEquals(2, channel.pipeline().names().size()); // Make sure Messages are still passed through channel.writeInbound(initRequestFrame); channel.writeOutbound(channel.readInbound()); InitRequestFrame sameInitRequestFrame = channel.readOutbound(); assertEquals(initRequestFrame.getId(), sameInitRequestFrame.getId()); assertEquals(initRequestFrame.getVersion(), sameInitRequestFrame.getVersion()); assertEquals(initRequestFrame.getHostPort(), sameInitRequestFrame.getHostPort()); }
From source file:com.uber.tchannel.handlers.InitDefaultRequestHandlerTest.java
License:Open Source License
@Test public void testValidInitRequest() throws Exception { // Given//from w w w . ja v a 2 s . c o m EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler(new PeerManager(new Bootstrap()))); InitRequestFrame initRequestFrame = new InitRequestFrame(42, InitFrame.DEFAULT_VERSION, new HashMap<String, String>() { { put(InitFrame.HOST_PORT_KEY, "0.0.0.0:0"); put(InitFrame.PROCESS_NAME_KEY, "test-process"); } }); channel.writeInbound( MessageCodec.encode(MessageCodec.encode(PooledByteBufAllocator.DEFAULT, initRequestFrame))); channel.writeOutbound(channel.readInbound()); // Then TFrame tFrame = MessageCodec.decode((ByteBuf) channel.readOutbound()); InitResponseFrame initResponseFrame = (InitResponseFrame) MessageCodec.decode(tFrame); tFrame.release(); // Assert assertNotNull(initResponseFrame); assertEquals(initRequestFrame.getId(), initResponseFrame.getId()); assertEquals(initRequestFrame.getVersion(), initResponseFrame.getVersion()); assertEquals(initRequestFrame.getHostPort(), initResponseFrame.getHostPort()); }
From source file:com.uber.tchannel.handlers.InitDefaultRequestHandlerTest.java
License:Open Source License
@Test public void testInvalidCallBeforeInitRequest() throws Exception { // Given//w w w .j a v a2 s . c o m EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler(new PeerManager(new Bootstrap()))); CallRequestFrame callRequestFrame = Fixtures.callRequest(0, false, Unpooled.EMPTY_BUFFER); channel.writeInbound( MessageCodec.encode(MessageCodec.encode(PooledByteBufAllocator.DEFAULT, callRequestFrame))); TFrame tFrame = MessageCodec.decode((ByteBuf) channel.readOutbound()); ErrorFrame errorFrame = (ErrorFrame) MessageCodec.decode(tFrame); tFrame.release(); assertNotNull(errorFrame); assertThat(errorFrame.getErrorType(), is(ErrorType.FatalProtocolError)); assertThat(errorFrame.getMessage(), containsString("The first frame should be an Init Request")); channel.writeOutbound(); }
From source file:com.uber.tchannel.handlers.InitDefaultRequestHandlerTest.java
License:Open Source License
@Test public void testIncorrectProtocolVersion() throws Exception { // Given/*from ww w .j av a2 s.c om*/ EmbeddedChannel channel = new EmbeddedChannel(new InitRequestHandler(new PeerManager(new Bootstrap()))); InitRequestFrame initRequestFrame = new InitRequestFrame(42, 1, new HashMap<String, String>() { { put(InitFrame.HOST_PORT_KEY, "0.0.0.0:0"); put(InitFrame.PROCESS_NAME_KEY, "test-process"); } }); channel.writeInbound( MessageCodec.encode(MessageCodec.encode(PooledByteBufAllocator.DEFAULT, initRequestFrame))); TFrame tFrame = MessageCodec.decode((ByteBuf) channel.readOutbound()); ErrorFrame errorFrame = (ErrorFrame) MessageCodec.decode(tFrame); tFrame.release(); assertNotNull(errorFrame); assertThat(errorFrame.getErrorType(), is(ErrorType.FatalProtocolError)); assertThat(errorFrame.getMessage(), containsString("Expected Protocol version: 2, got version: 1")); channel.writeOutbound(); }
From source file:com.util.MyServer.java
License:Apache License
/** * Start the server/*w w w .ja v a 2 s. c o m*/ */ public void start() throws IllegalArgumentException { ServerBindListener serverBindListener = new ServerBindListener(logger, this); bootStrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); /* * if no binding address is specified, bind "Wildcard" IP address. * https://docs.oracle.com/javase/7/docs/api/java/net/InetSocketAddress.html */ if (bindAddress.length == 0) { bootStrap.bind(serverPort).addListener(serverBindListener); } else { /* * Looping through binding address array, bind it one by one. */ for (String address : bindAddress) { bootStrap.bind(address, serverPort).addListener(serverBindListener); } } }
From source file:com.wolfbe.configcenter.remoting.netty.NettyRemotingServer.java
License:Apache License
@Override public void start() { this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(// nettyServerConfig.getServerWorkerThreads(), // new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); @Override/*from ww w . j a v a2 s . c o m*/ public Thread newThread(Runnable r) { return new Thread(r, "NettyServerCodecThread_" + this.threadIndex.incrementAndGet()); } }); ServerBootstrap childHandler = // this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupSelector) .channel(NioServerSocketChannel.class) // .option(ChannelOption.SO_BACKLOG, 1024) // .option(ChannelOption.SO_REUSEADDR, true) // .option(ChannelOption.SO_KEEPALIVE, false) // .childOption(ChannelOption.TCP_NODELAY, true) // .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()) // .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()) // .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(defaultEventExecutorGroup, // new NettyEncoder(), // new NettyDecoder(), // new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), // new NettyConnetManageHandler(), // new NettyServerHandler()); } }); if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) { childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); } try { ChannelFuture sync = this.serverBootstrap.bind().sync(); InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress(); this.port = addr.getPort(); } catch (InterruptedException e1) { throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1); } if (this.channelEventListener != null) { this.nettyEventExecuter.start(); } this.timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { NettyRemotingServer.this.scanResponseTable(); } catch (Exception e) { log.error("scanResponseTable exception", e); } } }, 1000 * 3, 1000); }
From source file:com.xx_dev.apn.proxy.ApnProxyUserAgentForwardHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext uaChannelCtx, final Object msg) throws Exception { final Channel uaChannel = uaChannelCtx.channel(); final ApnProxyRemote apnProxyRemote = uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get() .getRemote();/* w w w . j a va 2s .c o m*/ if (msg instanceof HttpRequest) { HttpRequest httpRequest = (HttpRequest) msg; Channel remoteChannel = remoteChannelMap.get(apnProxyRemote.getRemoteAddr()); if (remoteChannel != null && remoteChannel.isActive()) { LoggerUtil.debug(logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Use old remote channel"); HttpRequest request = constructRequestForProxy(httpRequest, apnProxyRemote); remoteChannel.writeAndFlush(request); } else { LoggerUtil.debug(logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Create new remote channel"); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(uaChannel.eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.AUTO_READ, false) .handler(new ApnProxyRemoteForwardChannelInitializer(uaChannel, this)); // set local address if (StringUtils.isNotBlank(ApnProxyLocalAddressChooser.choose(apnProxyRemote.getRemoteHost()))) { bootstrap.localAddress(new InetSocketAddress( (ApnProxyLocalAddressChooser.choose(apnProxyRemote.getRemoteHost())), 0)); } ChannelFuture remoteConnectFuture = bootstrap.connect(apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort()); remoteChannel = remoteConnectFuture.channel(); remoteChannelMap.put(apnProxyRemote.getRemoteAddr(), remoteChannel); remoteConnectFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().write(constructRequestForProxy((HttpRequest) msg, apnProxyRemote)); for (HttpContent hc : httpContentBuffer) { future.channel().writeAndFlush(hc); if (hc instanceof LastHttpContent) { future.channel().writeAndFlush(Unpooled.EMPTY_BUFFER) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().read(); } } }); } } httpContentBuffer.clear(); } else { LoggerUtil.error(logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Remote channel create fail"); // send error response String errorMsg = "remote connect to " + uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote() .getRemoteAddr() + " fail"; HttpMessage errorResponseMsg = HttpErrorUtil .buildHttpErrorMessage(HttpResponseStatus.INTERNAL_SERVER_ERROR, errorMsg); uaChannel.writeAndFlush(errorResponseMsg); httpContentBuffer.clear(); future.channel().close(); } } }); } ReferenceCountUtil.release(msg); } else { Channel remoteChannel = remoteChannelMap.get(apnProxyRemote.getRemoteAddr()); HttpContent hc = ((HttpContent) msg); //hc.retain(); //HttpContent _hc = hc.copy(); if (remoteChannel != null && remoteChannel.isActive()) { remoteChannel.writeAndFlush(hc); if (hc instanceof LastHttpContent) { remoteChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.channel().read(); } } }); } } else { httpContentBuffer.add(hc); } } }
From source file:com.xx_dev.apn.proxy.ApnProxyUserAgentTunnelHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext uaChannelCtx, Object msg) throws Exception { if (msg instanceof HttpRequest) { final HttpRequest httpRequest = (HttpRequest) msg; //Channel uaChannel = uaChannelCtx.channel(); // connect remote Bootstrap bootstrap = new Bootstrap(); bootstrap.group(uaChannelCtx.channel().eventLoop()).channel(NioSocketChannel.class) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.AUTO_READ, false) .handler(new ApnProxyTunnelChannelInitializer(uaChannelCtx.channel())); final ApnProxyRemote apnProxyRemote = uaChannelCtx.channel() .attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY).get().getRemote(); // set local address if (StringUtils.isNotBlank(ApnProxyLocalAddressChooser.choose(apnProxyRemote.getRemoteHost()))) { bootstrap.localAddress(new InetSocketAddress( (ApnProxyLocalAddressChooser.choose(apnProxyRemote.getRemoteHost())), 0)); }/* w w w . j a v a 2 s .c o m*/ bootstrap.connect(apnProxyRemote.getRemoteHost(), apnProxyRemote.getRemotePort()) .addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture future1) throws Exception { if (future1.isSuccess()) { if (apnProxyRemote.isAppleyRemoteRule()) { uaChannelCtx.pipeline().remove("codec"); uaChannelCtx.pipeline().remove(ApnProxyPreHandler.HANDLER_NAME); uaChannelCtx.pipeline().remove(ApnProxyUserAgentTunnelHandler.HANDLER_NAME); // add relay handler uaChannelCtx.pipeline() .addLast(new ApnProxyRelayHandler("UA --> Remote", future1.channel())); future1.channel() .writeAndFlush(Unpooled.copiedBuffer( constructConnectRequestForProxy(httpRequest, apnProxyRemote), CharsetUtil.UTF_8)) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future2) throws Exception { if (!future2.channel().config() .getOption(ChannelOption.AUTO_READ)) { future2.channel().read(); } } }); } else { HttpResponse proxyConnectSuccessResponse = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, new HttpResponseStatus(200, "Connection established")); uaChannelCtx.writeAndFlush(proxyConnectSuccessResponse) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future2) throws Exception { // remove handlers uaChannelCtx.pipeline().remove("codec"); uaChannelCtx.pipeline().remove(ApnProxyPreHandler.HANDLER_NAME); uaChannelCtx.pipeline() .remove(ApnProxyUserAgentTunnelHandler.HANDLER_NAME); // add relay handler uaChannelCtx.pipeline() .addLast(new ApnProxyRelayHandler( "UA --> " + apnProxyRemote.getRemoteAddr(), future1.channel())); } }); } } else { if (uaChannelCtx.channel().isActive()) { uaChannelCtx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER) .addListener(ChannelFutureListener.CLOSE); } } } }); } ReferenceCountUtil.release(msg); }
From source file:com.yahoo.pulsar.broker.admin.PersistentTopics.java
License:Apache License
@GET @Path("/{property}/{cluster}/{namespace}/{destination}/subscription/{subName}/position/{messagePosition}") @ApiOperation(value = "Peek nth message on a topic subscription.") @ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Topic, subscription or the message position does not exist") }) public Response peekNthMessage(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("destination") @Encoded String destination, @PathParam("subName") String subName, @PathParam("messagePosition") int messagePosition, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) { destination = decode(destination);/* w w w . j av a 2 s . co m*/ DestinationName dn = DestinationName.get(domain(), property, cluster, namespace, destination); PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(property, cluster, namespace, destination, authoritative); if (partitionMetadata.partitions > 0) { throw new RestException(Status.METHOD_NOT_ALLOWED, "Peek messages on a partitioned topic is not allowed"); } validateAdminOperationOnDestination(dn, authoritative); PersistentTopic topic = getTopicReference(dn); PersistentReplicator repl = null; PersistentSubscription sub = null; Entry entry = null; if (subName.startsWith(topic.replicatorPrefix)) { repl = getReplicatorReference(subName, topic); } else { sub = getSubscriptionReference(subName, topic); } try { if (subName.startsWith(topic.replicatorPrefix)) { entry = repl.peekNthMessage(messagePosition).get(); } else { entry = sub.peekNthMessage(messagePosition).get(); } checkNotNull(entry); PositionImpl pos = (PositionImpl) entry.getPosition(); ByteBuf metadataAndPayload = entry.getDataBuffer(); // moves the readerIndex to the payload MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload); ResponseBuilder responseBuilder = Response.ok(); responseBuilder.header("X-Pulsar-Message-ID", pos.toString()); for (KeyValue keyValue : metadata.getPropertiesList()) { responseBuilder.header("X-Pulsar-PROPERTY-" + keyValue.getKey(), keyValue.getValue()); } if (metadata.hasPublishTime()) { responseBuilder.header("X-Pulsar-publish-time", DATE_FORMAT.format(Instant.ofEpochMilli(metadata.getPublishTime()))); } // Decode if needed CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(metadata.getCompression()); ByteBuf uncompressedPayload = codec.decode(metadataAndPayload, metadata.getUncompressedSize()); // Copy into a heap buffer for output stream compatibility ByteBuf data = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedPayload.readableBytes(), uncompressedPayload.readableBytes()); data.writeBytes(uncompressedPayload); uncompressedPayload.release(); StreamingOutput stream = new StreamingOutput() { @Override public void write(OutputStream output) throws IOException, WebApplicationException { output.write(data.array(), data.arrayOffset(), data.readableBytes()); data.release(); } }; return responseBuilder.entity(stream).build(); } catch (NullPointerException npe) { throw new RestException(Status.NOT_FOUND, "Message not found"); } catch (Exception exception) { log.error("[{}] Failed to get message at position {} from {} {}", clientAppId(), messagePosition, dn, subName, exception); throw new RestException(exception); } finally { if (entry != null) { entry.release(); } } }
From source file:com.yahoo.pulsar.broker.service.BrokerService.java
License:Apache License
public void start() throws Exception { this.producerNameGenerator = new DistributedIdGenerator(pulsar.getZkClient(), producerNameGeneratorPath, pulsar.getConfiguration().getClusterName()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); bootstrap.group(acceptorGroup, workerGroup); bootstrap.childOption(ChannelOption.TCP_NODELAY, true); bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024)); if (workerGroup instanceof EpollEventLoopGroup) { bootstrap.channel(EpollServerSocketChannel.class); bootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED); } else {// w ww . ja va 2 s . c o m bootstrap.channel(NioServerSocketChannel.class); } ServiceConfiguration serviceConfig = pulsar.getConfiguration(); bootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, false)); // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), port)).sync(); log.info("Started Pulsar Broker service on port {}", port); if (serviceConfig.isTlsEnabled()) { ServerBootstrap tlsBootstrap = bootstrap.clone(); tlsBootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, true)); tlsBootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), tlsPort)).sync(); log.info("Started Pulsar Broker TLS service on port {}", tlsPort); } // start other housekeeping functions this.startStatsUpdater(); this.startInactivityMonitor(); this.startMessageExpiryMonitor(); this.startBacklogQuotaChecker(); }