Caused by: com.alibaba.fastjson.JSONException: illegal input, offset 1, char 4
Caused by: com.alibaba.fastjson.JSONException: illegal input, offset 1, char 4
前言:
1,有个功能是接收对方kafka推送数据,我这边做解析,本地有个定时任务同步历史告警数据问题,发现历史数据没有同步
2,查看日志发现定时任务有执行日志,但是没有同步成功
3,手动调用补偿接口还是报错,最终报以下异常
{
"timestamp": "2024-12-23T02:29:55.455+0000",
"status": 500,
"error": "Internal Server Error",
"message": "GC overhead limit exceeded",
"path": "/platform/taskCompensation/initCurrentDateAlarmTask"
}
问题排查
1,GC异常报 GC overhead limit exceeded
GC overhead limit exceeded 是 Java 虚拟机(JVM)中发生的一种错误,通常表示 JVM 在进行垃圾回收时花费了过多的时间,而内存却没有得到有效回收。具体来说,当垃圾回收器已经进行了一段时间(通常超过 98% 的时间),但仍未能回收足够的内存,JVM 会抛出 GC overhead limit exceeded 错误。
具体原因:
1.内存不足:程序运行时需要的内存超过了可用内存,或者堆内存配置过低,导致垃圾回收无法有效地回收足够的内存。
2.内存泄漏:程序中存在内存泄漏,导致某些对象无法被垃圾回收器回收,从而造成内存不断积累。
3.垃圾回收效率低下:长时间的垃圾回收循环未能有效地清理内存,可能是因为对象的创建过于频繁,或者 JVM 配置不当。
4.JVM 堆内存设置不当:JVM 的堆内存设置过小,不能满足应用程序的需求,导致垃圾回收发生过于频繁,最终导致 GC 往返时间过长。
可以尝试增加堆内存大小,优化GC回收策略
-Xms 设置堆的初始大小。
-Xmx 设置堆的最大大小。
java -Xms512m -Xmx2g -jar yourapp.jar
2,打开项目错误日志报:
ConsumerRecord(topic = oss-ocp-alarm-zb1, partition = 9, leaderEpoch = 2, offset = 2302967910, CreateTime = 1734924604419, serialized key size = -1, serialized value size = 215, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {"C_Ip":"611.174.180.28","C_HostName":"ADF-JJD-XDL-DSSD-2.NMAN.90008E","C_ifAliasMessage":"0.2009","data_time":"2024-12-23 11:30:00","C_IfFluxOutIPv6Mesage":"","C_IfFluxOutIPv4Message":"0","C_IfFluxInIPv6Message":"","C_IfFluxInIPv4":"0"})org.springframework.kafka.listener.ListenerExecutionFailedException: Listener method 'public void com.at.xxx.taskCompensation.factory.ExportFlowConsumer.receiveIaas(java.util.List<java.lang.String>,org.apache.kafka.clients.consumer.Consumer)' threw excep
tion; nested exception is com.alibaba.fastjson.JSONException: illegal input, offset 1, char 1; nested exception is com.alibaba.fastjson.JSONException: illegal input, offset 1, char 1 at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.decorateException(KafkaMessageListenerContainer.java:1743)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeBatchErrorHandler(KafkaMessageListenerContainer.java:1477)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeBatchListener(KafkaMessageListenerContainer.java:1359)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeBatchListener(KafkaMessageListenerContainer.java:1250)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:1233)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:985)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:905)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.alibaba.fastjson.JSONException: illegal input, offset 1, char 1
at com.alibaba.fastjson.JSON.parseObject(JSON.java:185)
at com.dx.major.platform.kafka.consumer.factory.ExportFlowConsumer.receiveAlarmInfo(ExportFlowConsumer.java:78)
at com.dx.major.platform.kafka.consumer.factory.ExportFlowConsumer.receiveIaas(ExportFlowConsumer.java:65)
at sun.reflect.GeneratedMethodAccessor265.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120)
at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:323)
at org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter.invoke(BatchMessagingMessageListenerAdapter.java:141)
at org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter.onMessage(BatchMessagingMessageListenerAdapter.java:133)
at org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter.onMessage(BatchMessagingMessageListenerAdapter.java:58)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeBatchOnMessage(KafkaMessageListenerContainer.java:1451)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeBatchOnMessage(KafkaMessageListenerContainer.java:1414)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeBatchListener(KafkaMessageListenerContainer.java:1346)
... 7 common frames omitted
Caused by: com.alibaba.fastjson2.JSONException: illegal input, offset 1, char 1
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:1432)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:176)
... 22 common frames omitted
[yzygzt@yzw_icnoc_test_01 logs]$ vi error.2024-12-23.0.log
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:905)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.alibaba.fastjson.JSONException: illegal input, offset 1, char 4
at com.alibaba.fastjson.JSON.parseObject(JSON.java:185)
at com.xxx.major.xxx.kafka.consumer.factory.ExportFlowConsumer.receiveAlarmInfo(AlarmFlowConsumer.java:77)
at com.xxx.major.xxx.kafka.consumer.factory.ExportFlowConsumer.receiveIaas(AlarmFlowConsumer.java:65)
at sun.reflect.GeneratedMethodAccessor286.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:171)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:120)
at org.springframework.kafka.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:48)
at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:323)
at org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter.invoke(BatchMessagingMessageListenerAdapter.java:141)
at org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter.onMessage(BatchMessagingMessageListenerAdapter.java:133)
at org.springframework.kafka.listener.adapter.BatchMessagingMessageListenerAdapter.onMessage(BatchMessagingMessageListenerAdapter.java:58)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeBatchOnMessage(KafkaMessageListenerContainer.java:1451)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeBatchOnMessage(KafkaMessageListenerContainer.java:1414)
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeBatchListener(KafkaMessageListenerContainer.java:1346)
... 7 common frames omitted
Caused by: com.alibaba.fastjson2.JSONException: illegal input, offset 1, char 4
at com.alibaba.fastjson2.JSONReader.read(JSONReader.java:1432)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:176)
... 22 common frames omitted
2024-12-23 11:10:05.067 [alarm-1223-2-C-1] ERROR org.springframework.kafka.listener.BatchLoggingErrorHandler - Error while processing:
ConsumerRecord(topic = oss-ocp-alarm-zb1, partition = 5, leaderEpoch = 1, offset = 3281797924, CreateTime = 1734923403995, serialized key size = -1, serialized value size = 211, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = {"C_Ip":"611.174.180.28","C_HostName":"ADF-JJD-XDL-DSSD-2.NMAN.90008E","C_ifAliasMessage":"0.2009","data_time":"2024-12-23 11:30:00","C_IfFluxOutIPv6Mesage":"","C_IfFluxOutIPv4Message":"0","C_IfFluxInIPv6Message":"","C_IfFluxInIPv4":"0"})
1,大概错误说:
1,使用alibaba fastjson解析异常,尝试解析 Kafka 消息体中的 JSON 时,Fastjson 库(或 Fastjson2)在指定位置(offset 1, char 1 或 offset 1, char 4)遇到了非法的输入。
问题分析:
非法 JSON 格式:illegal input 错误通常意味着你正在尝试解析的 JSON 字符串存在语法问题。根据错误的字符位置(offset 1、char 1 和 char 4),可能是 JSON 数据的开头或者某个字段部分存在格式错误。
例如,可能的错误包括:
JSON 字符串中有多余的字符或无效的字符(如额外的逗号、未闭合的引号等)。
某些字段的值不符合预期格式,例如字段值为空,或者传入的值包含了不符合 JSON 格式的特殊字符。
2,在项目kafka接收数据时候添加日志
private void receiveAlarmInfo(List<String> records) {
// TODO:相关业务逻辑
List<ExportFlowResourceEntity> resourceEntityList = new ArrayList<>();
Date currentDate = new Date();
for (String record : records) {
JSONObject jsonObject = JSON.parseObject(record);
// TODO:相关业务逻辑 数据解析
}
// TODO:入库
}
3,测试环境错误日志跟踪
少一个"{" 对方推送数据格式不合法。
4,问题解决
添加try catch 如果JSON异常,直接打印错误日志,并保存到数据库用于后面排查问题。
private void receiveAlarmInfo(List<String> records) {
// TODO:相关业务逻辑
List<ExportFlowResourceEntity> resourceEntityList = new ArrayList<>();
Date currentDate = new Date();
for (String record : records) {
log.info("record:{}",record);
JSONObject jsonObject = null;
try {
jsonObject = JSON.parseObject(record);
} catch (Exception e) {
// TODO:记录异常 保存到数据库
log.error("xxx采集解析异常,原始record为:{}",record);
}
if (Objects.isNull(jsonObject)){
continue;
}
}
// TODO:入库
}
小节:
1,虽然调用补偿接口报 GC异常,GC可能是最终结果,需要查询异常日志
2,这次GC异常是因为,对方kafka推送数据不合法导致本地使用 JSON.parseObject(record);报错,会把本次拉取的kafka数据全部打印出来导致内存使用完GC清理内存不够
3,我们在解析JSON时候,要捕捉错误信息
喜欢我的文章的话,点个阅读或者点个点赞,是我编写博客的动力,持续更新中 ing…