Java Code Examples for com.esotericsoftware.kryo.Kryo#setRegistrationRequired()

The following examples show how to use com.esotericsoftware.kryo.Kryo#setRegistrationRequired() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KryoUtils.java    From RxCache with Apache License 2.0 6 votes vote down vote up
@Override
protected Kryo initialValue() {
    Kryo kryo = new Kryo();

    /**
     * 不要轻易改变这里的配置!更改之后,序列化的格式就会发生变化,
     * 上线的同时就必须清除 Redis 里的所有缓存,
     * 否则那些缓存再回来反序列化的时候,就会报错
     */
    //支持对象循环引用(否则会栈溢出)
    kryo.setReferences(true); //默认值就是 true,添加此行的目的是为了提醒维护者,不要改变这个配置

    //不强制要求注册类(注册行为无法保证多个 JVM 内同一个类的注册编号相同;而且业务系统中大量的 Class 也难以一一注册)
    kryo.setRegistrationRequired(false); //默认值就是 false,添加此行的目的是为了提醒维护者,不要改变这个配置

    //Fix the NPE bug when deserializing Collections.
    ((Kryo.DefaultInstantiatorStrategy) kryo.getInstantiatorStrategy())
            .setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());

    kryo.register(CacheHolder.class, new CacheHolderSerializer());

    return kryo;
}
 
Example 2
Source File: CubeUtils.java    From cubedb with GNU General Public License v3.0 6 votes vote down vote up
public static Kryo getKryoWithRegistrations() {
  // int ref = 0;
  Kryo kryo = new Kryo();
  kryo.setRegistrationRequired(false);
  /*
   * kryo.register(MultiCubeImpl.class, ref++);
   * kryo.register(HashMapLookup.class, ref++);
   * kryo.register(OffHeapPartition.class, ref++);
   * kryo.register(OffHeapColumn.class, ref++);
   * kryo.register(OffHeapMetric.class, ref++);
   * kryo.register(MultiBuffer.class, ref++);
   * kryo.register(TinyColumn.class, ref++);
   * kryo.register(TinyMetric.class, ref++);
   */
  return kryo;
}
 
Example 3
Source File: AbstractSerializer.java    From jea with Apache License 2.0 6 votes vote down vote up
/**
 * 根据所注册的Class,初始化Kryo实例
 * 
 * @return
 */
protected Kryo initKryo() {
	Kryo kryo = new Kryo();
	kryo.setReferences(true); 
	kryo.setRegistrationRequired(true); 
	kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
	Set<Class<?>> keys = propertyMap.keySet();
	for(Class<?> key : keys){
		if(propertyMap.get(key) != null){
			kryo.register(key, propertyMap.get(key));
		} else {
			kryo.register(key);
		}
	}
	
	return kryo;
}
 
Example 4
Source File: KryoUtils.java    From kafka-examples with Apache License 2.0 6 votes vote down vote up
public Kryo create() {
    Kryo kryo = new Kryo();
    try {
        kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
        kryo.register(Collections.EMPTY_LIST.getClass(), new CollectionsEmptyListSerializer());
        kryo.register(Collections.EMPTY_MAP.getClass(), new CollectionsEmptyMapSerializer());
        kryo.register(Collections.EMPTY_SET.getClass(), new CollectionsEmptySetSerializer());
        kryo.register(SINGLETON_LIST.getClass(), new CollectionsSingletonListSerializer());
        kryo.register(SINGLETON_SET.getClass(), new CollectionsSingletonSetSerializer());
        kryo.register(SINGLETON_MAP.getClass(), new CollectionsSingletonMapSerializer());
        kryo.setRegistrationRequired(false);

        UnmodifiableCollectionsSerializer.registerSerializers(kryo);
        SynchronizedCollectionsSerializer.registerSerializers(kryo);
        ((Kryo.DefaultInstantiatorStrategy) kryo.getInstantiatorStrategy()).setFallbackInstantiatorStrategy(new StdInstantiatorStrategy());
    } catch (Exception e) {
        logger.error("Exception occurred", e);
    }
    return kryo;
}
 
Example 5
Source File: KryoContentCodec.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
public KryoContentCodec(int dimension, CodecDefinition definition) {
    this.codecDefinition = definition;
    Kryo kryo = new Kryo();
    kryo.setReferences(true);
    kryo.setRegistrationRequired(true);
    for (ClassDefinition classDefinition : definition.getClassDefinitions()) {
        Class<?> clazz = classDefinition.getType();
        if (clazz == void.class || clazz == Void.class) {
            // TODO
            continue;
        }
        kryo.register(clazz);
        if (clazz.isPrimitive()) {
            for (int index = 0; index < dimension; index++) {
                Object array = Array.newInstance(clazz, 0);
                kryo.register(array.getClass());
                clazz = array.getClass();
            }
        } else {
            Type type = clazz;
            for (int index = 0; index < dimension; index++) {
                type = TypeUtility.genericArrayType(type);
                kryo.register(TypeUtility.getRawType(type, null));
            }
        }

    }
    this.kryo = kryo;
}
 
Example 6
Source File: KryoSerializer.java    From bitchat with Apache License 2.0 5 votes vote down vote up
@Override
protected Kryo create() {
    Kryo kryo = new Kryo();
    kryo.setRegistrationRequired(false);
    kryo.setReferences(true);
    return kryo;
}
 
Example 7
Source File: KryoSerializer.java    From twister2 with Apache License 2.0 5 votes vote down vote up
public KryoSerializer() {
  kryo = new Kryo();
  kryo.setReferences(false);
  kryo.setRegistrationRequired(false);
  kryoOut = new Output(2000, 2000000000);
  kryoIn = new Input(1);
}
 
Example 8
Source File: KryoNamespace.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a Kryo instance.
 *
 * @return Kryo instance
 */
@Override
public Kryo create() {
    log.trace("Creating Kryo instance for {}", this);
    Kryo kryo = new Kryo();
    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;
}
 
Example 9
Source File: SerializationUtil.java    From dubbox with Apache License 2.0 5 votes vote down vote up
protected Kryo initialValue() {
	Kryo kryo = new Kryo();
	kryo.setReferences(false);
	kryo.setRegistrationRequired(false);
	kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
	return kryo;
}
 
Example 10
Source File: SerializationUtils.java    From hudi with Apache License 2.0 5 votes vote down vote up
public Kryo newKryo() {

      Kryo kryo = new Kryo();
      // ensure that kryo doesn't fail if classes are not registered with kryo.
      kryo.setRegistrationRequired(false);
      // This would be used for object initialization if nothing else works out.
      kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
      // Handle cases where we may have an odd classloader setup like with libjars
      // for hadoop
      kryo.setClassLoader(Thread.currentThread().getContextClassLoader());
      return kryo;
    }
 
Example 11
Source File: KryoSerializer.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
@Override
protected Kryo initialValue() throws Exception {
    Kryo kryo = new Kryo();
    for (Class<?> type : useJavaSerializerTypes) {
        kryo.addDefaultSerializer(type, JavaSerializer.class);
    }
    kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());
    kryo.setRegistrationRequired(false);
    kryo.setReferences(false);
    return kryo;
}
 
Example 12
Source File: KryoSerializer.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance of Kryo serializer, for use with the given object type.
 * <p/>
 * Note: this method is public to allow end-users to validate compatibility of their POJOs,
 * with the Kryo serializer as used by CQEngine.
 *
 * @param objectType The type of object which the instance of Kryo will serialize
 * @return a new instance of Kryo serializer
 */
@SuppressWarnings({"ArraysAsListWithZeroOrOneArgument", "WeakerAccess"})
protected Kryo createKryo(Class<?> objectType) {
    Kryo kryo = new Kryo();
    // Instantiate serialized objects via a no-arg constructor when possible, falling back to Objenesis...
    kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    // Register the object which this index will persist...
    kryo.register(objectType);
    kryo.setRegistrationRequired(false);
    // Register additional serializers which are not built-in to Kryo 3.0...
    kryo.register(Arrays.asList().getClass(), new ArraysAsListSerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);
    return kryo;
}
 
Example 13
Source File: KryoSerializerFactory.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public static Kryo createKryo(int maxDepth) {
    Kryo kryo = new Kryo();
    kryo.setRegistrationRequired(false);
    kryo.setAsmEnabled(true);
    kryo.setMaxDepth(maxDepth);
    kryo.setAutoReset(true);

    return kryo;
}
 
Example 14
Source File: Namespace.java    From atomix with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: Kryo5Codec.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected Kryo createKryo(ClassLoader classLoader) {
    Kryo kryo = new Kryo();
    if (classLoader != null) {
        kryo.setClassLoader(classLoader);
    }
    kryo.setRegistrationRequired(false);
    kryo.setReferences(false);
    kryo.addDefaultSerializer(Throwable.class, new JavaSerializer());
    return kryo;
}
 
Example 16
Source File: KryoTest.java    From RoaringBitmap with Apache License 2.0 4 votes vote down vote up
public static Kryo createKryo() {
    Kryo kryo = new Kryo();
    kryo.setRegistrationRequired(false);
    kryo.register(RoaringBitmap.class, new RoaringSerializer());
    return kryo;
}
 
Example 17
Source File: KryoCodec.java    From jvm-serializer with Apache License 2.0 4 votes vote down vote up
protected Kryo initialValue() {
    Kryo kryo = new Kryo();

    kryo.register(byte[].class);
    kryo.register(char[].class);
    kryo.register(short[].class);
    kryo.register(int[].class);
    kryo.register(long[].class);
    kryo.register(float[].class);
    kryo.register(double[].class);
    kryo.register(boolean[].class);
    kryo.register(String[].class);
    kryo.register(Object[].class);
    kryo.register(KryoSerializable.class);
    kryo.register(BigInteger.class);
    kryo.register(BigDecimal.class);
    kryo.register(Class.class);
    kryo.register(Date.class);
    //kryo.register(Enum.class);
    kryo.register(EnumSet.class);
    kryo.register(Currency.class);
    kryo.register(StringBuffer.class);
    kryo.register(StringBuilder.class);
    kryo.register(Collections.EMPTY_LIST.getClass());
    kryo.register(Collections.EMPTY_MAP.getClass());
    kryo.register(Collections.EMPTY_SET.getClass());
    kryo.register(Collections.singletonList(null).getClass());
    kryo.register(Collections.singletonMap(null, null).getClass());
    kryo.register(Collections.singleton(null).getClass());
    kryo.register(TreeSet.class);
    kryo.register(Collection.class);
    kryo.register(TreeMap.class);
    kryo.register(Map.class);
    try {
        kryo.register(Class.forName("sun.util.calendar.ZoneInfo"));
    } catch (ClassNotFoundException e) {
        //Noop
    }
    kryo.register(Calendar.class);
    kryo.register(Locale.class);

    kryo.register(BitSet.class);
    kryo.register(HashMap.class);
    kryo.register(Timestamp.class);
    kryo.register(ArrayList.class);

    int size = idList.size();
    for (int i = 0; i < size; i++) {
        kryo.register(classList
                        .get(i),
                serializerList
                        .get(i),
                idList.get(i));
    }
    kryo.setRegistrationRequired(true);
    kryo.setReferences(false);
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    return kryo;
}
 
Example 18
Source File: KryoCodec.java    From jvm-serializer with Apache License 2.0 4 votes vote down vote up
protected Kryo initialValue() {
    Kryo kryo = new Kryo();

    kryo.register(byte[].class);
    kryo.register(char[].class);
    kryo.register(short[].class);
    kryo.register(int[].class);
    kryo.register(long[].class);
    kryo.register(float[].class);
    kryo.register(double[].class);
    kryo.register(boolean[].class);
    kryo.register(String[].class);
    kryo.register(Object[].class);
    kryo.register(KryoSerializable.class);
    kryo.register(BigInteger.class);
    kryo.register(BigDecimal.class);
    kryo.register(Class.class);
    kryo.register(Date.class);
    //kryo.register(Enum.class);
    kryo.register(EnumSet.class);
    kryo.register(Currency.class);
    kryo.register(StringBuffer.class);
    kryo.register(StringBuilder.class);
    kryo.register(Collections.EMPTY_LIST.getClass());
    kryo.register(Collections.EMPTY_MAP.getClass());
    kryo.register(Collections.EMPTY_SET.getClass());
    kryo.register(Collections.singletonList(null).getClass());
    kryo.register(Collections.singletonMap(null, null).getClass());
    kryo.register(Collections.singleton(null).getClass());
    kryo.register(TreeSet.class);
    kryo.register(Collection.class);
    kryo.register(TreeMap.class);
    kryo.register(Map.class);
    try {
        kryo.register(Class.forName("sun.util.calendar.ZoneInfo"));
    } catch (ClassNotFoundException e) {
        //Noop
    }
    kryo.register(Calendar.class);
    kryo.register(Locale.class);

    kryo.register(BitSet.class);
    kryo.register(HashMap.class);
    kryo.register(Timestamp.class);
    kryo.register(ArrayList.class);

    int size = idList.size();
    for (int i = 0; i < size; i++) {
        kryo.register(classList
                        .get(i),
                serializerList
                        .get(i),
                idList.get(i));
    }
    kryo.setRegistrationRequired(true);
    kryo.setReferences(false);
    kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
    return kryo;
}
 
Example 19
Source File: LevelDbStore.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
		Kryo kryo = new Kryo();
		kryo.setReferences(false);
		kryo.setRegistrationRequired(false);
		kryo.setInstantiatorStrategy(new DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
//		kryo.register(Transaction.class);
//		kryo.register(Xid.class);
//		kryo.register(TransactionType.class);
//		kryo.register(TransactionStatus.class);
//		kryo.register(Participant.class);
		
//		Output output = new Output(new ByteArrayOutputStream());//new Output(new FileOutputStream("file.bin"));
//		
//		DefaultTransaction defaultTransaction = new DefaultTransaction();
//		defaultTransaction.setXid(new DefaultXid("1", "2", "3"));
//		
//		kryo.writeObject(output, defaultTransaction);
//		output.flush();
//		output.close();
		
		Options options = new Options();
		options.createIfMissing(true);
		options.cacheSize(100 * 1048576); // 100MB cache
		options.logger(new org.iq80.leveldb.Logger() {
			public void log(String message) {
				TransactionLog.REPOSITORY.info(message);
			}
		});
		DB db = Iq80DBFactory.factory.open(new File("txtreedb"), options);
		
//		String stats = db.getProperty("leveldb.stats");
//		System.out.println(stats);
		
		DBIterator iterator = null;
		try {
			iterator = db.iterator();
			for(iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
				String key = Iq80DBFactory.asString(iterator.peekNext().getKey());
				Transaction value = (Transaction) SerializationUtil.deserialize(iterator.peekNext().getValue());
				
				System.out.println(key+" = "+value);
			}
		} finally {
			if (iterator != null) {
				iterator.close();
			}
		}

//		String transactionId = "f282205a-e794-4bda-83a2-bb28b8839cad";
//		Input input = new Input(db.get(Iq80DBFactory.bytes(transactionId)));
//		Transaction transaction = (Transaction) kryo.readClassAndObject(input);
//		System.out.println(transaction);
	}
 
Example 20
Source File: MyKryoFactory.java    From lionrpc with Apache License 2.0 4 votes vote down vote up
public Kryo create () {
	Kryo kryo = new Kryo();
	kryo.setReferences(false);
       kryo.setRegistrationRequired(false);
	return kryo;
}