我目前有一个项目,它使用 jackson faster xml 使用自定义序列化器和反序列化器将 POJO 序列化/反序列化为 Json。据我了解,ObjectMapper 在创建和配置后是线程安全的。但是,在使用 JMeter 运行测试时,我注意到偶尔会发生以下情况 -
- 线程1进入CustomerSerializer,开始序列化
- 线程2进入CustomSerializer,打断线程1,从头到尾开始序列化
- 线程1恢复,最后被序列化的东西不见了
当第二个线程进入时,似乎 JsonGenerator 实例正在被重置——这肯定不应该发生吗?我已经检查了几个站点和线程以查看是否需要设置任何设置或功能,但据我了解 ObjectMapper 重用 JsonGenerator 实例,这可能是问题吗?
以下是我的自定义序列化程序的片段...
@Override
public final void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeStartObject();
... Code here ....
jsonGenerator.writeEndObject();
closeJsonGenerator(jsonGenerator);
}
以及使用它的示例
SimpleModule sm = new SimpleModule();
sm.addSerializer(new myCustomSerializer());
new ObjectMapper().registerModule(sm)
.writeValue(new myObject());