解决FontConfiguration.getVersion报空指针异常的问题
作者:又菜又爱玩٩( ö̆ ) و
FontConfiguration.getVersion报空指针异常
程序中使用到了图形验证码的功能,本地测试没问题,但是在服务器上就会报异常。
服务器中缺少FontConfig,是因为服务器用的是OpenJDK, 所以需要手动安装。
而本地用的则是正式JDK1.8,所以没有问题。
安装命令:
yum install fontconfig
JDK引发的bug(sun.awt.FontConfiguration.getVersion NullPointerException)
背景阐述
项目开发中有个数据导出的功能,采用的是异步队列来处理的。
开发使用的jdk为
本地测试完全没有问题,部署到线上,一直会报下面日志的错误信息。
2020-02-11 07:29:40.575 - INFO 1 ---
[nio-8080-exec-6] c.c.t.service.impl.TemplateServiceImpl : >>>开始向【导出数据队列】投递消息<<<
2020-02-11 07:29:40.592 - INFO 1 ---
[nio-8080-exec-6] c.c.t.service.impl.TemplateServiceImpl : >>>向【导出数据队列】投递消息完成<<<
2020-02-11 07:29:40.619 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>成功接收导出数据的消息,消息参数为:{"formId":1219112740443000874,"process":{"creator":1189805389533810109,"deleted":false,"description":"开始处理","editor":1189805389533810109,"id":1227132577916260352,"state":"处理中","tenantId":1197018942024060928},"grantId":1189805389533810109,"processId":1227132577916260352,"tenantId":1197018942024060928,"dataIds":"[\"1224868133840490496\"]"}<<<<<
2020-02-11 07:29:40.619 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>开始解析参数<<<<<
2020-02-11 07:29:40.620 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>解析参数结束<<<<<
2020-02-11 07:29:44.439 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>成功接收导出数据的消息,消息参数为:{"formId":1219112740443000874,"process":{"creator":1189805389533810109,"deleted":false,"description":"开始处理","editor":1189805389533810109,"id":1227132577916260352,"state":"处理中","tenantId":1197018942024060928},"grantId":1189805389533810109,"processId":1227132577916260352,"tenantId":1197018942024060928,"dataIds":"[\"1224868133840490496\"]"}<<<<<
2020-02-11 07:29:44.440 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>开始解析参数<<<<<
2020-02-11 07:29:44.441 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>解析参数结束<<<<<
2020-02-11 07:29:47.553 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>成功接收导出数据的消息,消息参数为:{"formId":1219112740443000874,"process":{"creator":1189805389533810109,"deleted":false,"description":"开始处理","editor":1189805389533810109,"id":1227132577916260352,"state":"处理中","tenantId":1197018942024060928},"grantId":1189805389533810109,"processId":1227132577916260352,"tenantId":1197018942024060928,"dataIds":"[\"1224868133840490496\"]"}<<<<<
2020-02-11 07:29:47.554 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>开始解析参数<<<<<
2020-02-11 07:29:47.554 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>解析参数结束<<<<<
2020-02-11 07:29:50.677 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>成功接收导出数据的消息,消息参数为:{"formId":1219112740443000874,"process":{"creator":1189805389533810109,"deleted":false,"description":"开始处理","editor":1189805389533810109,"id":1227132577916260352,"state":"处理中","tenantId":1197018942024060928},"grantId":1189805389533810109,"processId":1227132577916260352,"tenantId":1197018942024060928,"dataIds":"[\"1224868133840490496\"]"}<<<<<
2020-02-11 07:29:50.678 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>开始解析参数<<<<<
2020-02-11 07:29:50.678 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>解析参数结束<<<<<
2020-02-11 07:29:53.780 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>成功接收导出数据的消息,消息参数为:{"formId":1219112740443000874,"process":{"creator":1189805389533810109,"deleted":false,"description":"开始处理","editor":1189805389533810109,"id":1227132577916260352,"state":"处理中","tenantId":1197018942024060928},"grantId":1189805389533810109,"processId":1227132577916260352,"tenantId":1197018942024060928,"dataIds":"[\"1224868133840490496\"]"}<<<<<
2020-02-11 07:29:53.780 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>开始解析参数<<<<<
2020-02-11 07:29:53.780 - INFO 1 ---
[cTaskExecutor-1] c.c.t.rabbitmq.ExportDataReceiver : >>>>>解析参数结束<<<<<
2020-02-11 07:29:53.965 - WARN 1 ---
[cTaskExecutor-1] o.s.a.r.r.RejectAndDontRequeueRecoverer : Retries exhausted for message (Body:'[B@7dedff15(byte[753])' MessageProperties [headers={}, contentType=application/x-java-serialized-object, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=template_exchange, receivedRoutingKey=topic.template.exportData, deliveryTag=1, consumerTag=amq.ctag-80yarAv96_cA3nDTsNz8Hw, consumerQueue=template_exportData])
org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener method 'public void cn.cncommdata.template.rabbitmq.ExportDataReceiver.process(com.alibaba.fastjson.JSONObject)' threw exception
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:196)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:126)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1414)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1337)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:91)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:287)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:180)
at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:115)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at org.springframework.amqp.rabbit.listener.$Proxy156.invokeListener(Unknown Source)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1324)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1303)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:817)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:801)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:77)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1042)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: null
at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
at sun.font.SunFontManager$2.run(SunFontManager.java:431)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.SunFontManager.<init>(SunFontManager.java:376)
at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
at java.security.AccessController.doPrivileged(Native Method)
at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
at java.awt.Font.getFont2D(Font.java:491)
at java.awt.Font.canDisplayUpTo(Font.java:2060)
at java.awt.font.TextLayout.singleFont(TextLayout.java:470)
at java.awt.font.TextLayout.<init>(TextLayout.java:531)
at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275)
at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)
at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:82)
at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:658)
at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:679)
at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:90)
at com.alibaba.excel.util.WorkBookUtil.createSheet(WorkBookUtil.java:51)
at com.alibaba.excel.context.WriteContext.currentSheet(WriteContext.java:112)
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:69)
at com.alibaba.excel.ExcelWriter.write1(ExcelWriter.java:106)
at cn.cncommdata.template.utils.HeaderUtil.uploadWithData(HeaderUtil.java:311)
at cn.cncommdata.template.rabbitmq.ExportDataReceiver.handler(ExportDataReceiver.java:131)
at cn.cncommdata.template.rabbitmq.ExportDataReceiver.process(ExportDataReceiver.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
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:181)
at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114)
at org.springframework.amqp.rabbit.listener.adapter.DelegatingInvocableHandler.invoke(DelegatingInvocableHandler.java:128)
at org.springframework.amqp.rabbit.listener.adapter.HandlerAdapter.invoke(HandlerAdapter.java:61)
at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.invokeHandler(MessagingMessageListenerAdapter.java:188)
... 24 common frames omitted
2020-02-11 07:29:53.966 - WARN 1 ---
[cTaskExecutor-1] s.a.r.l.ConditionalRejectingErrorHandler : Execution of Rabbit message listener failed.
问题排查
经过测试沟通发现,项目部署人员使用的jdk是openjdk,导致报上面的问题。
解决方案
将openjdk换为sunjdk。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。