List of usage examples for io.netty.handler.codec.compression ZlibWrapper ZLIB
ZlibWrapper ZLIB
To view the source code for io.netty.handler.codec.compression ZlibWrapper ZLIB.
Click Source Link
From source file:com.ebay.jetstream.http.netty.client.HttpClient.java
License:MIT License
public void post(URI uri, Object content, Map<String, String> headers, ResponseFuture responsefuture) throws Exception { if (m_shutDown.get()) { throw new IOException("IO has been Shutdown = "); }//from w w w . j a v a2 s . c om if (uri == null) throw new NullPointerException("uri is null or incorrect"); if (!m_started.get()) { synchronized (this) { if (!m_started.get()) { start(); m_started.set(true); } } } ByteBuf byteBuf = Unpooled.buffer(m_config.getInitialRequestBufferSize()); ByteBufOutputStream outputStream = new ByteBufOutputStream(byteBuf); // transform to json try { m_mapper.writeValue(outputStream, content); } catch (JsonGenerationException e) { throw e; } catch (JsonMappingException e) { throw e; } catch (IOException e) { throw e; } finally { outputStream.close(); } EmbeddedChannel encoder; String contenteEncoding; if ("gzip".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP, 1)); contenteEncoding = "gzip"; } else if ("deflate".equals(m_config.getCompressEncoder())) { encoder = new EmbeddedChannel(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB, 1)); contenteEncoding = "deflate"; } else { encoder = null; contenteEncoding = null; } if (encoder != null) { encoder.config().setAllocator(UnpooledByteBufAllocator.DEFAULT); encoder.writeOutbound(byteBuf); encoder.finish(); byteBuf = (ByteBuf) encoder.readOutbound(); encoder.close(); } DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, uri.toString(), byteBuf); if (contenteEncoding != null) { HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_ENCODING, contenteEncoding); } HttpHeaders.setHeader(request, HttpHeaders.Names.ACCEPT_ENCODING, "gzip, deflate"); HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_TYPE, "application/json"); HttpHeaders.setContentLength(request, byteBuf.readableBytes()); if (isKeepAlive()) HttpHeaders.setHeader(request, HttpHeaders.Names.CONNECTION, "keep-alive"); if (headers != null) { @SuppressWarnings("rawtypes") Iterator it = headers.entrySet().iterator(); while (it.hasNext()) { @SuppressWarnings("rawtypes") Map.Entry pairs = (Map.Entry) it.next(); HttpHeaders.setHeader(request, (String) pairs.getKey(), pairs.getValue()); } } if (responsefuture != null) { RequestId reqid = RequestId.newRequestId(); m_responseDispatcher.add(reqid, responsefuture); HttpHeaders.setHeader(request, "X_EBAY_REQ_ID", reqid.toString()); // we expect this to be echoed in the response used for correlation. } if (m_dataQueue.size() < m_workQueueCapacity) { ProcessHttpWorkRequest workRequest = new ProcessHttpWorkRequest(this, uri, request); if (!m_dataQueue.offer(workRequest)) { if (responsefuture != null) { responsefuture.setFailure(); m_responseDispatcher.remove(request.headers().get("X_EBAY_REQ_ID")); } } } else { throw new IOException("downstream Queue is full "); } }
From source file:com.linecorp.armeria.client.encoding.DeflateStreamDecoderFactory.java
License:Apache License
@Override public StreamDecoder newDecoder() { return new ZlibStreamDecoder(ZlibWrapper.ZLIB); }
From source file:dbseer.middleware.client.MiddlewareClient.java
License:Apache License
public void run() { // debug info Log.debug(String.format("host = %s", host)); Log.debug(String.format("port = %d", port)); Log.debug(String.format("log path = %s", logPath)); // client needs to handle incoming messages from the middleware as well. EventLoopGroup group = new NioEventLoopGroup(4); try {//w ww . jav a 2 s.c om // attach shutdown hook. MiddlewareClientShutdown shutdownThread = new MiddlewareClientShutdown(this); Runtime.getRuntime().addShutdownHook(shutdownThread); File logDir = new File(logPath); if (!logDir.exists()) { logDir.mkdirs(); } final MiddlewareClient client = this; Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new IdleStateHandler(10, 0, 0)); p.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB)); p.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.ZLIB)); p.addLast(new MiddlewarePacketDecoder()); p.addLast(new MiddlewarePacketEncoder()); p.addLast(new MiddlewareClientHandler(client)); } }); ChannelFuture f = b.connect(host, port).sync(); channel = f.channel(); Log.debug("Connected to the middleware."); MiddlewarePacket checkPacket = new MiddlewarePacket(MiddlewareConstants.PACKET_CHECK_VERSION, MiddlewareConstants.PROTOCOL_VERSION); // ByteBuf buf = Unpooled.buffer(); // buf.writeInt(MiddlewareConstants.PACKET_CHECK_VERSION); // buf.writeInt(MiddlewareConstants.PROTOCOL_VERSION.getBytes("UTF-8").length); // buf.writeBytes(MiddlewareConstants.PROTOCOL_VERSION.getBytes("UTF-8")); // channel.writeAndFlush(buf); channel.writeAndFlush(checkPacket); channel.closeFuture().sync(); } catch (Exception e) { if (e instanceof InterruptedException) { } else { setChanged(); notifyObservers(new MiddlewareClientEvent(MiddlewareClientEvent.ERROR, e)); } Log.error(e.getMessage()); e.printStackTrace(); } finally { group.shutdownGracefully(); this.stopExecutors(); if (txLogFileRaw.exists()) { txLogFileRaw.delete(); } if (txZipOutputStream != null) { try { txZipOutputStream.closeEntry(); txZipOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } txZipOutputStream = null; } } }
From source file:dbseer.middleware.server.MiddlewareServer.java
License:Apache License
public void run() throws Exception { // basic log info. Log.info(String.format("Listening port = %d", port)); Log.info(String.format("DB log dir = %s", dbLogPath)); Log.info(String.format("System log dir = %s", sysLogPath)); // print server info. for (Server s : servers.values()) { s.printLogInfo();/*from w ww .jav a 2s .c o m*/ // test MySQL/MariaDB connection using JDBC before we start anything. if (!s.testConnection()) { throw new Exception("Unable to connect to the MySQL server with the given credential."); } else if (!s.testMonitoringDir()) { throw new Exception("Specified monitoring directory and script do not exist."); } } // open named pipe. File checkPipeFile = new File(this.namedPipePath); if (checkPipeFile == null || !checkPipeFile.exists() || checkPipeFile.isDirectory()) { throw new Exception("Cannot open the named pipe for communication with dbseerroute. " + "You must run Maxscale with dbseerroute with correct named pipe first."); } namedPipeFile = new RandomAccessFile(this.namedPipePath, "rwd"); if (namedPipeFile == null) { throw new Exception("Cannot open the named pipe for communication with dbseerroute. " + "You must run Maxscale with dbseerroute with correct named pipe first."); } // attach shutdown hook. MiddlewareServerShutdown shutdownThread = new MiddlewareServerShutdown(this); Runtime.getRuntime().addShutdownHook(shutdownThread); // let's start accepting connections. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(4); final MiddlewareServer server = this; try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.SO_KEEPALIVE, true) .handler(new MiddlewareServerConnectionHandler(server)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new IdleStateHandler(10, 0, 0)); p.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB)); p.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.ZLIB)); p.addLast(new MiddlewarePacketDecoder()); p.addLast(new MiddlewarePacketEncoder()); p.addLast(new MiddlewareServerHandler(server)); // p.addLast(new MiddlewarePacketDecoder(), new MiddlewareServerHandler(server)); } }); Log.info("Middleware is now accepting connections."); // bind and start accepting connections. ChannelFuture cf = b.bind(port).sync(); // shutdown the server. if (cf != null) { cf.channel().closeFuture().sync(); } } catch (Exception e) { Log.error(e.getMessage()); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); if (tailerExecutor != null && !tailerExecutor.isShutdown()) { tailerExecutor.shutdown(); } } }
From source file:org.skfiy.typhon.net.Netty4Connector.java
License:Apache License
@Override protected void startInternal() throws LifecycleException { setState(LifecycleState.STARTING);/*from w w w . j a va 2 s . c om*/ fireLifecycleListener(START_EVENT); System.setProperty("io.netty.noJdkZlibDecoder", "false"); final byte[] delimiters = new byte[] { '\n' }; final String compressionMode = Typhons.getProperty("typhon.spi.net.compressionMode"); final Netty4EndpointHandler handler = new Netty4EndpointHandler(); final Netty4ConnectionLimitHandler limitHandler = new Netty4ConnectionLimitHandler(); // ???? final LengthFieldPrepender lengthFieldPrepender; final DelimiterBasedFrameEncoder delimiterBasedFrameEncoder; if ("zlib".equals(compressionMode)) { lengthFieldPrepender = new LengthFieldPrepender(4); delimiterBasedFrameEncoder = null; } else { lengthFieldPrepender = null; delimiterBasedFrameEncoder = new DelimiterBasedFrameEncoder(delimiters); } // final LoggingHandler loggingHandler; if (isLogEnabled()) { loggingHandler = new LoggingHandler(LogLevel.DEBUG); } else { loggingHandler = null; } serverBootstrap = new ServerBootstrap(); serverBootstrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .childHandler(new ChannelInitializer() { @Override protected void initChannel(Channel c) throws Exception { ChannelPipeline pipeline = c.pipeline(); if ("zlib".equals(compressionMode)) { pipeline.addLast("lengthFieldPrepender", lengthFieldPrepender); pipeline.addLast("lengthFieldBasedFrameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("deflater", ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB)); } else { pipeline.addLast("delimiterBasedFrameDecoder", new DelimiterBasedFrameDecoder(65535, new ByteBuf[] { Unpooled.wrappedBuffer(delimiters) })); pipeline.addLast("delimiterBasedFrameEncoder", delimiterBasedFrameEncoder); } if (isLogEnabled()) { pipeline.addLast(loggingHandler); } pipeline.addLast(new IdleStateHandler(60 * 10, 60 * 10, 0)); pipeline.addLast(limitHandler, handler); } }); channel = serverBootstrap.bind(host, port).channel(); CLOG.debug("Netty4Connector started on port {}", port); MBeanServer mbs = MBeanUtils.REGISTRY.getMBeanServer(); Object obj = null; try { obj = mbs.invoke(Container.OBJECT_NAME, "getInstance", new Object[] { ProtocolHandler.class }, new String[] { Class.class.getName() }); } catch (Exception ex) { CLOG.error("ProtocolHandler", ex); throw new TyphonException(ex); } handler.setProtocolHandler((ProtocolHandler) obj); }
From source file:tp.MyJZLibDecoder.java
/** * Creates a new instance with the default wrapper ({@link ZlibWrapper#ZLIB}). * */ public MyJZLibDecoder() { this(ZlibWrapper.ZLIB); }
From source file:tp.MyJZLibEncoder.java
/** * Creates a new zlib encoder with the specified {@code compressionLevel} * and the default wrapper ({@link ZlibWrapper#ZLIB}). * * @param compressionLevel/*from w w w . ja v a2s . c om*/ * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. */ public MyJZLibEncoder(int compressionLevel) { this(ZlibWrapper.ZLIB, compressionLevel); }
From source file:tp.MyJZLibEncoder.java
/** * Creates a new zlib encoder with the specified {@code compressionLevel} * and the specified wrapper.//from w ww .j a v a 2 s . c o m * * @param wrapper * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. */ public MyJZLibEncoder(ZlibWrapper wrapper, int compressionLevel) { if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (wrapper == null) { throw new NullPointerException("wrapper"); } if (wrapper == ZlibWrapper.ZLIB_OR_NONE) { throw new IllegalArgumentException( "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " + "allowed for compression."); } gzip = wrapper == ZlibWrapper.GZIP; deflater = new Deflater(compressionLevel, wrapper != ZlibWrapper.ZLIB); }