Java8 dubbo 调用 Collectors.toMap代码片发生的异常(IllegalStateException: Duplicate key)

2020-09-27 10:18:35 浏览数 (1)

事故现场代码

代码语言:javascript复制
        Map1 map = list.stream().collect(
                Collectors.toMap(item -> item.getAppId(), item -> item));

然后,这段代码是被dubbo中的线程执行的,所以,当时只抛了一句话,并没有堆栈信息,后面的日志都没有打印。以至于线程无缘无故后面的都不执行了,线程直接挂掉。

后来加了try catch块

代码语言:javascript复制
try{
        Map1 map = list.stream().collect(
                Collectors.toMap(item -> item.getAppId(), item -> item));
                }
      catch (Exception e){
      		logger.error("error occur",e);
      }

终于有日志了:

代码语言:javascript复制
java.lang.IllegalStateException: Duplicate key
at java.util.stream.Collectors.lambda$throwingMerger$142(Collectors.java:133)
        at java.util.stream.Collectors$$Lambda$938/166232894.apply(Unknown Source)
        at java.util.HashMap.merge(HashMap.java:1245)
        at java.util.stream.Collectors.lambda$toMap$200(Collectors.java:1320)
        at java.util.stream.Collectors$$Lambda$940/2096918380.accept(Unknown Source)
        at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
        at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
        at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
        at org.apache.dubbo.common.bytecode.Wrapper11.invokeMethod(Wrapper11.java)
        at org.apache.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:47)
        at org.apache.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:86)
        at org.apache.dubbo.config.invoker.DelegateProviderMetaDataInvoker.invoke(DelegateProviderMetaDataInvoker.java:56)
        at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)
        at org.apache.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:63)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:88)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:54)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:80)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at com.alibaba.dubbo.rpc.Invoker$CompatibleInvoker.invoke(Invoker.java:54)
        at com.alibaba.dubbo.rpc.Filter.invoke(Filter.java:29)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:79)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:137)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:39)
        at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
        at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:128)
        at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:103)
        at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:200)
        at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
        at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

得知,原来,这个toMap方法,遇到相同的key不会覆盖,而是直接抛错,也是有道理的。因为如果覆盖到时候,你排查问题发现最终结果和原来list里面的值竟然不一样,是不是更匪夷所思。 写一个测试方法

建议先转为set,再转为map

0 人点赞