/** * By default, kryo pool uses ConcurrentLinkedQueue which is unbounded. To facilitate reuse of * kryo object call releaseKryo() after done using the kryo instance. The class loader for the * kryo instance will be set to current thread's context class loader. * * @return kryo instance */ public static Kryo borrowKryo() { Kryo kryo = kryoPool.borrow(); kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); return kryo; }
@Override public Object doApply(byte[] buffer) { ByteArrayInputStream in; if (useIdentityNumber) { in = new ByteArrayInputStream(buffer, 4, buffer.length - 4); } else { in = new ByteArrayInputStream(buffer); } Input input = new Input(in); Kryo kryo = (Kryo) KryoValueEncoder.kryoThreadLocal.get()[0]; kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); return kryo.readClassAndObject(input); } }
/** * Sub classes can customize the Kryo instance by overriding this method * * @return create Kryo instance */ protected Kryo createInstance() { Kryo kryo = new Kryo(); if (classLoader != null) { kryo.setClassLoader(classLoader); } kryo.setReferences(false); for (Class<?> clazz : classes) { kryo.register(clazz); } return kryo; }
/** * Sub classes can customize the Kryo instance by overriding this method * * @return create Kryo instance */ protected Kryo createInstance() { Kryo kryo = new Kryo(); if (classLoader != null) { kryo.setClassLoader(classLoader); } kryo.setReferences(false); for (Class<?> clazz : classes) { kryo.register(clazz); } return kryo; }
public static <T> T deserialize(byte[] buffer, Class<T> clazz) { Kryo kryo = SerializationUtilities.borrowKryo(); kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); T result = null; try { result = kryo.readObject(new Input(new ByteArrayInputStream(buffer)), clazz); } finally { SerializationUtilities.releaseKryo(kryo); } return result; }
public static byte[] serialize(Object object) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); Output output = new Output(stream); Kryo kryo = SerializationUtilities.borrowKryo(); kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); try { kryo.writeObject(output, object); } finally { SerializationUtilities.releaseKryo(kryo); } output.close(); // close() also calls flush() return stream.toByteArray(); }
/** * @param plan Usually of type MapredWork, MapredLocalWork etc. * @param out stream in which serialized plan is written into */ private static void serializeObjectByKryo(Kryo kryo, Object plan, OutputStream out) { Output output = new Output(out); kryo.setClassLoader(Utilities.getSessionSpecifiedClassLoader()); kryo.writeObject(output, plan); output.close(); }
private static <T> T deserializeObjectByKryo(Kryo kryo, InputStream in, Class<T> clazz ) { Input inp = new Input(in); kryo.setClassLoader(Utilities.getSessionSpecifiedClassLoader()); T t = kryo.readObject(inp,clazz); inp.close(); return t; }
private void checkKryoInitialized() { if (this.kryo == null) { this.kryo = getKryoInstance(); // Enable reference tracking. kryo.setReferences(true); // Throwable and all subclasses should be serialized via java serialization // Note: the registered JavaSerializer is Flink's own implementation, and not Kryo's. // This is due to a know issue with Kryo's JavaSerializer. See FLINK-6025 for details. kryo.addDefaultSerializer(Throwable.class, new JavaSerializer()); // Add default serializers first, so that the type registrations without a serializer // are registered with a default serializer for (Map.Entry<Class<?>, ExecutionConfig.SerializableSerializer<?>> entry: defaultSerializers.entrySet()) { kryo.addDefaultSerializer(entry.getKey(), entry.getValue().getSerializer()); } for (Map.Entry<Class<?>, Class<? extends Serializer<?>>> entry: defaultSerializerClasses.entrySet()) { kryo.addDefaultSerializer(entry.getKey(), entry.getValue()); } KryoUtils.applyRegistrations(this.kryo, kryoRegistrations.values()); kryo.setRegistrationRequired(false); kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); } }
ClassLoader newLoader = addToClassPath(loader, addedJars.split(";")); Thread.currentThread().setContextClassLoader(newLoader); kryo.setClassLoader(newLoader);
/** * Creates a Kryo instance. * * @return Kryo instance */ @Override public Kryo create() { LOGGER.trace("Creating Kryo instance for {}", this); Kryo kryo = new Kryo(); kryo.setClassLoader(classLoader); kryo.setRegistrationRequired(registrationRequired); // If compatible serialization is enabled, override the default serializer. if (compatible) { kryo.setDefaultSerializer(CompatibleFieldSerializer::new); } // TODO rethink whether we want to use StdInstantiatorStrategy kryo.setInstantiatorStrategy( new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); for (RegistrationBlock block : registeredBlocks) { int id = block.begin(); if (id == FLOATING_ID) { id = kryo.getNextRegistrationId(); } for (Pair<Class<?>[], Serializer<?>> entry : block.types()) { register(kryo, entry.getLeft(), entry.getRight(), id++); } } return kryo; }
Kryo k = kryoFactory.getKryo(conf); if (WorkerClassLoader.getInstance() != null) k.setClassLoader(WorkerClassLoader.getInstance()); k.register(byte[].class);
kryo.setClassLoader( classLoader );
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); this.kryo = new Kryo(); this.kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); }
public KryoSerializableStreamCodec() { this.kryo = new Kryo(); this.kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); }
public DefaultKryoStreamCodec() { this.kryo = new Kryo(); this.kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); }
public static Object retrieve(InputStream stream) { synchronized (kryo) { kryo.setClassLoader(Thread.currentThread().getContextClassLoader()); Input input = new Input(stream); return kryo.readClassAndObject(input); } }
public Kryo newKryo() { Kryo k = KryoInstantiator.this.newKryo(); k.setClassLoader(Thread.currentThread().getContextClassLoader()); return k; } };
@Override protected Kryo initialValue() { Kryo kryo = new Kryo(); kryo.setRegistrationRequired(false); kryo.setClassLoader( Thread.currentThread().getContextClassLoader() ); return kryo; }; };
public Kryo newKryo() { Kryo k = KryoInstantiator.this.newKryo(); k.setClassLoader(cl); return k; } };