1

尝试将列表对象存储在 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)
4

2 回答 2

2

添加后工作.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");
于 2020-05-12T12:57:23.943 回答
2

不幸的是,ProtoStream 编组器当前不支持直接编组ArrayList,但计划在随后的 ProtoStream 版本IPROTO-118中进行编组。

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

public class Book {

   @ProtoField(number = 1, collectionImplementation = ArrayList.class)
   final List<String> authors;

   @ProtoFactory
   Book(List<String> authors) {
      this.authors = authors;
   }
}
于 2020-05-28T09:06:34.557 回答