/** * Creates a new {@link ObjectInput}. * * @param in * the {@link InputStream} where the serialized form will be * read from * @param classLoader * the {@link ClassLoader} which will load the class of the * serialized object * @param maxObjectSize * the maximum byte length of the serialized object. if the length * of the received object is greater than this value, * a {@link StreamCorruptedException} will be raised. */ public ObjectDecoderInputStream(InputStream in, ClassLoader classLoader, int maxObjectSize) { if (in == null) { throw new NullPointerException("in"); } if (maxObjectSize <= 0) { throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize); } if (in instanceof DataInputStream) { this.in = (DataInputStream) in; } else { this.in = new DataInputStream(in); } classResolver = ClassResolvers.weakCachingResolver(classLoader); this.maxObjectSize = maxObjectSize; }
/** * cache disabled * @param classLoader - specific classLoader to use, or null if you want to revert to default * @return new instance of class resolver */ public static ClassResolver cacheDisabled(ClassLoader classLoader) { return new ClassLoaderClassResolver(defaultClassLoader(classLoader)); }
/** * Instantiates a new log files decoder. * * @param receiveDirectory the receive directory */ public LogFilesDecoder(String receiveDirectory) { super(314572800, ClassResolvers.cacheDisabled(null)); this.receiveDirectory = receiveDirectory; }
@Override public void initChannel(SocketChannel ch) { ch.pipeline() .addLast( new ObjectDecoder( 1024 * 1024, ClassResolvers .weakCachingConcurrentResolver(this .getClass() .getClassLoader()))); ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new SubReqServerHandler()); } });
/** * Instantiates a new archive decoder. * * @param receiveDirectory the receive directory */ public ArchiveDecoder(String receiveDirectory) { super(104857600, ClassResolvers.cacheDisabled(null)); this.receiveDirectory = receiveDirectory; }
@Override protected void initChannel(SocketChannel arg0) throws Exception { // 创建一个新的ObjectDecoder,它负责对实现Serializable的POJO对象进行解码,它有多个构造函数,支持不同的ClassResolver, // 在此我们使用weakCachingConcurrentResolver创建线程安全的WeakReferenceMap对类加载器进行缓存, // 它支持多线程并发访问,当虚拟机内存不足时,会释放缓存中的内存,防止内存泄露,为了防止异常码流和解码错位导致的内存溢出 // 这里将单个对象最大序列化后的字节数组长度设置为1M, arg0.pipeline().addLast(new ObjectDecoder(1024*1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader()))); // 新增一个ObjectEncoder,它可以在消息发送的时候自动将实现Serializable的POJO对象进行编码,因此用户无需亲自对对象进行手工序列化, // 只需要关注自己的业务逻辑处理即可,对象序列化和反序列化都由Netty的对象编码器搞定 arg0.pipeline().addLast(new ObjectEncoder()); arg0.pipeline().addLast(new SubReqServerHandler()); } });
/** * Instantiates a new log files decoder. * * @param receiveDirectory the receive directory */ public LogFilesDecoder(String receiveDirectory) { super(314572800, ClassResolvers.cacheDisabled(null)); this.receiveDirectory = receiveDirectory; }
/** * cache disabled * @param classLoader - specific classLoader to use, or null if you want to revert to default * @return new instance of class resolver */ public static ClassResolver cacheDisabled(ClassLoader classLoader) { return new ClassLoaderClassResolver(defaultClassLoader(classLoader)); }
/** * Creates a new {@link ObjectInput}. * * @param in * the {@link InputStream} where the serialized form will be * read from * @param classLoader * the {@link ClassLoader} which will load the class of the * serialized object * @param maxObjectSize * the maximum byte length of the serialized object. if the length * of the received object is greater than this value, * a {@link StreamCorruptedException} will be raised. */ public ObjectDecoderInputStream(InputStream in, ClassLoader classLoader, int maxObjectSize) { if (in == null) { throw new NullPointerException("in"); } if (maxObjectSize <= 0) { throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize); } if (in instanceof DataInputStream) { this.in = (DataInputStream) in; } else { this.in = new DataInputStream(in); } classResolver = ClassResolvers.weakCachingResolver(classLoader); this.maxObjectSize = maxObjectSize; }
ch.pipeline().addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.weakCachingConcurrentResolver(null))); ch.pipeline().addLast(new ObjectEncoder());
/** * Instantiates a new archive decoder. * * @param receiveDirectory the receive directory */ public ArchiveDecoder(String receiveDirectory) { super(104857600, ClassResolvers.cacheDisabled(null)); this.receiveDirectory = receiveDirectory; }
/** * cache disabled * @param classLoader - specific classLoader to use, or null if you want to revert to default * @return new instance of class resolver */ public static ClassResolver cacheDisabled(ClassLoader classLoader) { return new ClassLoaderClassResolver(defaultClassLoader(classLoader)); }
/** * Creates a new {@link ObjectInput}. * * @param in * the {@link InputStream} where the serialized form will be * read from * @param classLoader * the {@link ClassLoader} which will load the class of the * serialized object * @param maxObjectSize * the maximum byte length of the serialized object. if the length * of the received object is greater than this value, * a {@link StreamCorruptedException} will be raised. */ public ObjectDecoderInputStream(InputStream in, ClassLoader classLoader, int maxObjectSize) { if (in == null) { throw new NullPointerException("in"); } if (maxObjectSize <= 0) { throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize); } if (in instanceof DataInputStream) { this.in = (DataInputStream) in; } else { this.in = new DataInputStream(in); } classResolver = ClassResolvers.weakCachingResolver(classLoader); this.maxObjectSize = maxObjectSize; }
@Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addFirst("msghandler", m_rxSessionHandler); StreamMessageDecoder decoder = new StreamMessageDecoder(ClassResolvers .weakCachingConcurrentResolver(null)); ch.pipeline().addBefore( "msghandler", "msgdecoder", decoder); ch.pipeline().addBefore("msgdecoder", "kryoEcnoder", new KryoObjectEncoder()); ch.pipeline().addBefore("kryoEcnoder", "msgencoder", new NettyObjectEncoder()); if (m_enableCompression) { ch.pipeline().addBefore("msgencoder", "decompressor", new MessageDecompressionHandler(false, 250000)); ch.pipeline().addBefore("decompressor", "idleTimeoutHandler", new IdleStateHandler((int) m_readIdleTimeout, 0, 0)); } else ch.pipeline().addBefore("msgencoder", "idleTimeoutHandler", new IdleStateHandler((int) m_readIdleTimeout, 0, 0)); } })
public LogFilesDecoder(int maxObjectSize, String receiveDirectory){ super(maxObjectSize, ClassResolvers.cacheDisabled(null)); this.receiveDirectory = receiveDirectory; }
/** * non-aggressive non-concurrent cache * good for non-shared default cache * * @param classLoader - specific classLoader to use, or null if you want to revert to default * @return new instance of class resolver */ public static ClassResolver weakCachingResolver(ClassLoader classLoader) { return new CachingClassResolver( new ClassLoaderClassResolver(defaultClassLoader(classLoader)), new WeakReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>())); }
@Override public ChannelHandler newChannelHandler() { return new DatagramPacketObjectDecoder(ClassResolvers.weakCachingResolver(null)); } };
public LogFilesDecoder(int maxObjectSize, String receiveDirectory){ super(maxObjectSize, ClassResolvers.cacheDisabled(null)); this.receiveDirectory = receiveDirectory; }
/** * aggressive non-concurrent cache * good for non-shared cache, when we're not worried about class unloading * * @param classLoader - specific classLoader to use, or null if you want to revert to default * @return new instance of class resolver */ public static ClassResolver softCachingResolver(ClassLoader classLoader) { return new CachingClassResolver( new ClassLoaderClassResolver(defaultClassLoader(classLoader)), new SoftReferenceMap<String, Class<?>>(new HashMap<String, Reference<Class<?>>>())); }
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new ObjectDecoder(4096*4096,ClassResolvers.weakCachingResolver(this.getClass().getClassLoader()))); pipeline.addLast(ENCODER); // and then business logic. pipeline.addLast(SERVER_HANDLER); }