有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

infinispan 10.1没有为Java类型Java注册封送拆收器。util。ArrayList

试图将列表对象存储在infinispan缓存中时,抛出了错误。我参考了一些论坛,也加入了白名单类,但仍然得到同样的错误

注意:远程托管服务器

进口

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.infinispan.client.hotrod.DefaultTemplate;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
import org.infinispan.commons.api.CacheContainerAdmin;

代码:

// Setup up a clustered cache manager
    ConfigurationBuilder builder = new ConfigurationBuilder();
builder.addServer().host("127.0.0.1").port(11322).addJavaSerialWhiteList("java.util.List","java.util.ArrayList");
    // Connect to the server
    RemoteCacheManager cacheManager = new RemoteCacheManager(builder.build());
    // Create test cache, if such does not exist
    cacheManager.administration().withFlags(CacheContainerAdmin.AdminFlag.VOLATILE).getOrCreateCache("test123", DefaultTemplate.DIST_SYNC);
    // Obtain the remote cache
    RemoteCache<String, List<String>> cache = cacheManager.getCache("test123");
    List<String> test = new ArrayList();
    cache.put("key", test);

例外情况

Exception in thread "main" java.lang.IllegalArgumentException: No marshaller registered for Java type java.util.ArrayList
at org.infinispan.protostream.impl.SerializationContextImpl.getMarshallerDelegate(SerializationContextImpl.java:279)
at org.infinispan.protostream.WrappedMessage.writeMessage(WrappedMessage.java:240)
at org.infinispan.protostream.ProtobufUtil.toWrappedByteArray(ProtobufUtil.java:181)
at org.infinispan.protostream.ProtobufUtil.toWrappedByteArray(ProtobufUtil.java:176)
at org.infinispan.commons.marshall.ProtoStreamMarshaller.objectToBuffer(ProtoStreamMarshaller.java:69)
at org.infinispan.commons.marshall.AbstractMarshaller.objectToByteBuffer(AbstractMarshaller.java:70)
at org.infinispan.client.hotrod.marshall.MarshallerUtil.obj2bytes(MarshallerUtil.java:99)

共 (2) 个答案

  1. # 1 楼答案

    在添加.marshaller(new JavaSerializationMarshaller())之后工作。希望这个改变是正确的,如果有更好的选择,请发布

    builder.addServer().host("127.0.0.1").port(ConfigurationProperties.DEFAULT_HOTROD_PORT).marshaller(new JavaSerializationMarshaller()).addJavaSerialWhiteList("java.util.List","java.util.ArrayList");
    
  2. # 2 楼答案

    不幸的是,ProtoStream marshaller目前不支持ArrayList的直接编组,但这是为后续的ProtoStream发行版IPROTO-118计划的

    要使用Infinispan的默认Protostream封送器封送ArrayList或任何Collection实现,需要将集合包装在用户定义的类中,并将其添加为字段。例如,以下类允许存储ArrayList:

    public class Book {
    
       @ProtoField(number = 1, collectionImplementation = ArrayList.class)
       final List<String> authors;
    
       @ProtoFactory
       Book(List<String> authors) {
          this.authors = authors;
       }
    }