[英]Spring Boot + Infinispan embedded - how to prevent ClassCastException when the object to be cached has been modified?
我有一個帶有 Spring Boot 2.5.5 和嵌入式 Infinispan 12.1.7 的 Web 應用程序。
我有一個帶有端點的控制器,可以通過 ID 獲取 Person 對象:
@RestController
public class PersonController {
private final PersonService service;
public PersonController(PersonService service) {
this.service = service;
}
@GetMapping("/person/{id}")
public ResponseEntity<Person> getPerson(@PathVariable("id") String id) {
Person person = this.service.getPerson(id);
return ResponseEntity.ok(person);
}
}
以下是在getPerson
方法上使用@Cacheable
注釋的PersonService
實現:
public interface PersonService {
Person getPerson(String id);
}
@Service
public class PersonServiceImpl implements PersonService {
private static final Logger LOG = LoggerFactory.getLogger(PersonServiceImpl.class);
@Override
@Cacheable("person")
public Person getPerson(String id) {
LOG.info("Get Person by ID {}", id);
Person person = new Person();
person.setId(id);
person.setFirstName("John");
person.setLastName("Doe");
person.setAge(35);
person.setGender(Gender.MALE);
person.setExtra("extra value");
return person;
}
}
這是 Person 類:
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String firstName;
private String lastName;
private Integer age;
private Gender gender;
private String extra;
/* Getters / Setters */
...
}
我將 infinispan 配置為使用基於文件系統的緩存存儲:
<?xml version="1.0" encoding="UTF-8"?>
<infinispan xmlns="urn:infinispan:config:12.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:12.1 https://infinispan.org/schemas/infinispan-config-12.1.xsd">
<cache-container default-cache="default">
<serialization marshaller="org.infinispan.commons.marshall.JavaSerializationMarshaller">
<allow-list>
<regex>com.example.*</regex>
</allow-list>
</serialization>
<local-cache-configuration name="mirrorFile">
<persistence passivation="false">
<file-store path="${infinispan.disk.store.dir}"
shared="false"
preload="false"
purge="false"
segmented="false">
</file-store>
</persistence>
</local-cache-configuration>
<local-cache name="person" statistics="true" configuration="mirrorFile">
<memory max-count="500"/>
<expiration lifespan="86400000"/>
</local-cache>
</cache-container>
</infinispan>
我請求端點獲取 ID 為“1”的人:http://localhost:8090/assets-webapp/person/1
第一次調用PersonService.getPerson(String)
並緩存結果。
我再次請求端點獲取 ID 為“1”的人,並在緩存中檢索結果。
我通過使用 getter/setter 刪除extra
字段來更新Person
對象,並添加一個extra2
字段:
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String firstName;
private String lastName;
private Integer age;
private Gender gender;
private String extra2;
...
public String getExtra2() {
return extra2;
}
public void setExtra2(String extra2) {
this.extra2 = extra2;
}
}
我再次請求端點獲取 ID 為“1”的人,但拋出了ClassCastException
:
java.lang.ClassCastException: com.example.controller.Person cannot be cast to com.example.controller.Person] with root cause java.lang.ClassCastException: com.example.controller.Person cannot be cast to com.example.controller.Person
at com.example.controller.PersonServiceImpl$$EnhancerBySpringCGLIB$$ec42b86.getPerson(<generated>) ~[classes/:?]
at com.example.controller.PersonController.getPerson(PersonController.java:19) ~[classes/:?]
我通過刪除extra2
字段並添加extra
字段來回滾 Person 對象修改。
我再次請求端點獲取 ID 為“1”的人,但始終拋出ClassCastException
infinispan 使用的編組器是JavaSerializationMarshaller 。
我猜如果類已經重新編譯,java 序列化不允許對緩存的數據進行解組。
但我想知道如何避免這種情況,尤其是能夠在訪問緩存數據時管理類的更新(添加/刪除字段)而不會出現異常。
有沒有人有辦法解決嗎?
最好的選擇是將緩存編碼更改為application/x-protostream
並使用 ProtoStream 庫序列化您的對象。
<local-cache-configuration name="mirrorFile">
<encoding>
<key media-type="application/x-protostream"/>
<value media-type="application/x-protostream"/>
</encoding>
</local-cache>
Infinispan 緩存默認將實際的 Java 對象保存在內存中,而不對其進行序列化。 配置的編組器僅用於將條目寫入磁盤。
當您修改類時,Spring 可能會在新的類加載器中創建一個同名的新類。 但是緩存中的對象仍然使用舊類加載器中的類,因此它們與新類不兼容。
配置除application/x-java-object
之外的編碼媒體類型會告訴 Infinispan 序列化保留在內存中的對象。
您還可以將緩存編碼更改為application/x-java-serialized-object
,以便您的對象使用JavaSerializationMarshaller
存儲在內存中,它已經用於在磁盤上存儲對象。 但是使用 Java 序列化保持與舊版本的兼容性需要大量工作,並且需要提前計划:您需要一個serialVersionUUID
字段,可能是一個版本字段,以及一個可以讀取舊格式的readExternal()
實現。 使用 ProtoStream,因為它基於 Protobuf 模式,您可以輕松添加新(可選)字段並忽略不再使用的字段,只要您不更改或重用字段編號。
我最終創建了自己的 Marshaller,在 JSON 中進行序列化/反序列化,靈感來自以下類: GenericJackson2JsonRedisSerializer.java
public class JsonMarshaller extends AbstractMarshaller {
private static final byte[] EMPTY_ARRAY = new byte[0];
private final ObjectMapper objectMapper;
public JsonMarshaller() {
this.objectMapper = objectMapper();
}
private ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
// Serialize/Deserialize objects from any fields or creators (constructors and (static) factory methods). Ignore getters/setters.
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
// Register support of other new Java 8 datatypes outside of date/time: most notably Optional, OptionalLong, OptionalDouble
objectMapper.registerModule(new Jdk8Module());
// Register support for Java 8 date/time types (specified in JSR-310 specification)
objectMapper.registerModule(new JavaTimeModule());
// simply setting {@code mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)} does not help here since we need
// the type hint embedded for deserialization using the default typing feature.
objectMapper.registerModule(new SimpleModule("NullValue Module").addSerializer(new NullValueSerializer(null)));
objectMapper.registerModule(
new SimpleModule("SimpleKey Module")
.addSerializer(new SimpleKeySerializer())
.addDeserializer(SimpleKey.class, new SimpleKeyDeserializer(objectMapper))
);
return objectMapper;
}
@Override
protected ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException, InterruptedException {
return ByteBufferImpl.create(objectToBytes(o));
}
private byte[] objectToBytes(Object o) throws JsonProcessingException {
if (o == null) {
return EMPTY_ARRAY;
}
return objectMapper.writeValueAsBytes(o);
}
@Override
public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException, ClassNotFoundException {
if (isEmpty(buf)) {
return null;
}
return objectMapper.readValue(buf, Object.class);
}
@Override
public boolean isMarshallable(Object o) throws Exception {
return true;
}
@Override
public MediaType mediaType() {
return MediaType.APPLICATION_JSON;
}
private static boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
/**
* {@link StdSerializer} adding class information required by default typing. This allows de-/serialization of {@link NullValue}.
*/
private static class NullValueSerializer extends StdSerializer<NullValue> {
private static final long serialVersionUID = 1999052150548658808L;
private final String classIdentifier;
/**
* @param classIdentifier can be {@literal null} and will be defaulted to {@code @class}.
*/
NullValueSerializer(String classIdentifier) {
super(NullValue.class);
this.classIdentifier = StringUtils.isNotBlank(classIdentifier) ? classIdentifier : "@class";
}
@Override
public void serialize(NullValue value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeStartObject();
jgen.writeStringField(classIdentifier, NullValue.class.getName());
jgen.writeEndObject();
}
}
}
SimpleKey 對象的序列化器/反序列化器:
public class SimpleKeySerializer extends StdSerializer<SimpleKey> {
private static final Logger LOG = LoggerFactory.getLogger(SimpleKeySerializer.class);
protected SimpleKeySerializer() {
super(SimpleKey.class);
}
@Override
public void serialize(SimpleKey simpleKey, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
serializeFields(simpleKey, gen, provider);
gen.writeEndObject();
}
@Override
public void serializeWithType(SimpleKey value, JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer) throws IOException {
WritableTypeId typeId = typeSer.typeId(value, JsonToken.START_OBJECT);
typeSer.writeTypePrefix(gen, typeId);
serializeFields(value, gen, provider);
typeSer.writeTypeSuffix(gen, typeId);
}
private void serializeFields(SimpleKey simpleKey, JsonGenerator gen, SerializerProvider provider) {
try {
Object[] params = (Object[]) FieldUtils.readField(simpleKey, "params", true);
gen.writeArrayFieldStart("params");
gen.writeObject(params);
gen.writeEndArray();
} catch (Exception e) {
LOG.warn("Could not read 'params' field from SimpleKey {}: {}", simpleKey, e.getMessage(), e);
}
}
}
public class SimpleKeyDeserializer extends StdDeserializer<SimpleKey> {
private final ObjectMapper objectMapper;
public SimpleKeyDeserializer(ObjectMapper objectMapper) {
super(SimpleKey.class);
this.objectMapper = objectMapper;
}
@Override
public SimpleKey deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
List<Object> params = new ArrayList<>();
TreeNode treeNode = jp.getCodec().readTree(jp);
TreeNode paramsNode = treeNode.get("params");
if (paramsNode.isArray()) {
for (JsonNode paramNode : (ArrayNode) paramsNode) {
Object[] values = this.objectMapper.treeToValue(paramNode, Object[].class);
params.addAll(Arrays.asList(values));
}
}
return new SimpleKey(params.toArray());
}
}
我將 infinispan 配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<infinispan xmlns="urn:infinispan:config:12.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:infinispan:config:12.1 https://infinispan.org/schemas/infinispan-config-12.1.xsd">
<cache-container default-cache="default">
<serialization marshaller="com.example.JsonMarshaller">
<allow-list>
<regex>com.example.*</regex>
</allow-list>
</serialization>
<local-cache-configuration name="mirrorFile">
<persistence passivation="false">
<file-store path="${infinispan.disk.store.dir}"
shared="false"
preload="false"
purge="false"
segmented="false">
</file-store>
</persistence>
</local-cache-configuration>
<local-cache name="person" statistics="true" configuration="mirrorFile">
<memory max-count="500"/>
<expiration lifespan="86400000"/>
</local-cache>
</cache-container>
</infinispan>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.