java.lang.NumberFormatException: multiple points

java.lang.NumberFormatException: multiple points

背景

在接收MQ消息,并且持久化到数据库的时候出现的报错

[2019-03-06 11:15:12:261 CST] ERROR com. .  .bridge.rmq.RocketMqMessageCommonServiceImpl.receiveFromScm(RocketMqMessageCommonServiceImpl.java:253) : rocket msgInfo:{"body":{"msgContent":"YKOZLzIhPNU8+jL75MfzOxYTRNpWLukccQgw2UYA0UoXi8JWJ+jUnfn/Ilyqwao1rbUwERkyBT+JWiP5sxlArGgI6gZZOaAJ9ZZrYsgtxtcIChaDhs49ycKcwmnW7HxdVuum1XqBp+LIVhZwpOQ="},"head":{"ifIdentify":"P1-3","ifVersion":"5.0","msgId":"125ce895-613e-41bf-a2c2-494cc8e1864c","msgSendTime":"2019-03-06 11:15:01","shop":"HKREEBOK","sign":"qCEq7DDevJ35e6ZoS/CNyYpvcEOieMsN+28aReDy0dzhTBlXd8a0wfdskrZ5CaV5dE2nrn3pXSsEVwe25TPUHw=="}}
java.lang.NumberFormatException: multiple points
  at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1110)
  at java.lang.Double.parseDouble(Double.java:540)
  at java.text.DigitList.getDouble(DigitList.java:168)
  at java.text.DecimalFormat.parse(DecimalFormat.java:1321)
  at java.text.SimpleDateFormat.subParse(SimpleDateFormat.java:2088)
  at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)
  at java.text.DateFormat.parse(DateFormat.java:355)
  at loxia.utils.DateUtil.parse(DateUtil.java:80)
  at com.bridge.rmq.RocketMqMessageCommonServiceImpl.receiveFromScm(RocketMqMessageCommonServiceImpl.java:223)
  at com.bridge.rmq.RocketMqMessageCommonServiceImpl.rocketMqReceiveFromScm(RocketMqMessageCommonServiceImpl.java:187)
  at sun.reflect.GeneratedMethodAccessor516.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:606)
  at com. .scm.baseservice.message.rocketmq.service.MsgTranscationManagerImpl.businessProcess(MsgTranscationManagerImpl.java:182)
  at com. .scm.baseservice.message.rocketmq.service.M 
    sgTranscationManagerImpl$$FastClassBySpringCGLIB$$6e604685.invoke(<generated>)
  at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
  at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:701)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
  at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
  at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
  at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
  at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:633)
  at com. .scm.baseservice.message.rocketmq.service.MsgTranscationManagerImpl$$EnhancerBySpringCGLIB$$bf38f53d.businessProcess(<generated>)
  at com. .scm.baseservice.message.rocketmq.service.handle.MessageHandler.excuteHandle(MessageHandler.java:271)
  at com. .scm.baseservice.message.rocketmq.service.handle.MessageHandler.handle(MessageHandler.java:185)
  at com. .scm.baseservice.message.rocketmq.service.init.RocketMQConcurrentlyConsumerInit$1.consumeMessage(RocketMQConcurrentlyConsumerInit.java:224)
  at org.apache.rocketmq.client.impl.consumer.ConsumeMessageConcurrentlyService$ConsumeRequest.run(ConsumeMessageConcurrentlyService.java:419)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:745)
发现
  • 总是出现数据在MQ客户端显示已经被消费了,但是并没有持久化到数据库的问题

  • 问后端要了MessageID去查log,最终发现,在处理数据的时候出现了这个错误

    查找解决方案

    查看报错,最开始觉得是数据格式出错,也是因为接受MQ这套机制很久就已经定义好的,基本不会有问题。

    然而发现是:

    at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1455)

    查代码:

        public static Date parse(String str, String pattern) throws ParseException {
            DateFormat df = dateFormatCache.get(pattern);
            if(df == null){
                df = new SimpleDateFormat(pattern);
                dateFormatCache.put(pattern, df);
            }
            return df.parse(str);
        }

    使用了new SimpleDateFormat(pattern)SimpleDateFormat(pattern)是线程不安全的,当有大量数据过来可能会出现,在处理多条数据的时候使用的同一个 simpleDateFormat导致报错

    最终解决方案

    1. 使用ThreadLocal

          //使用线程安全的ThreadLocal
          static ThreadLocal<DateFormat> testDateFormat = new ThreadLocal<DateFormat>(){
      
              @Override
              protected SimpleDateFormat initialValue(){
                  return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              }
          };
          private static Date dateParse(String dateStr) throws ParseException {
              return testDateFormat.get().parse(dateStr);
          }
    2. 使用java8的DateTimeFormatter

      private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

      ## 测试

使用SimpleDateFormat多试几次,或出现报错

1064870-20190311143555987-1804721384.png

使用ThreadLocal之后不会出现报错

1064870-20190311143409647-66790276.png

转载于:https://www.cnblogs.com/mengjie1001/p/10510233.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值