diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java index c31c0a80b6..a7698cecd8 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcInvocation.java @@ -112,7 +112,7 @@ public class RpcInvocation implements Invocation, Serializable { public RpcInvocation(Invocation invocation, Invoker invoker) { this(invocation.getTargetServiceUniqueName(), invocation.getServiceModel(), invocation.getMethodName(), invocation.getServiceName(), invocation.getProtocolServiceKey(), invocation.getParameterTypes(), invocation.getArguments(), - new HashMap<>(invocation.getObjectAttachments()), invocation.getInvoker(), invocation.getAttributes(), + Collections.synchronizedMap(invocation.getObjectAttachments()), invocation.getInvoker(), invocation.getAttributes(), invocation instanceof RpcInvocation ? ((RpcInvocation) invocation).getInvokeMode() : null); if (invoker != null) { URL url = invoker.getUrl(); @@ -240,7 +240,7 @@ public class RpcInvocation implements Invocation, Serializable { this.protocolServiceKey = protocolServiceKey; this.parameterTypes = parameterTypes == null ? new Class[0] : parameterTypes; this.arguments = arguments == null ? new Object[0] : arguments; - this.attachments = attachments == null ? new HashMap<>() : attachments; + this.attachments = attachments == null ? Collections.synchronizedMap(new HashMap<>()) : attachments; this.attributes = attributes == null ? Collections.synchronizedMap(new HashMap<>()) : attributes; this.invoker = invoker; initParameterDesc(); @@ -381,7 +381,7 @@ public class RpcInvocation implements Invocation, Serializable { } public void setObjectAttachments(Map attachments) { - this.attachments = attachments == null ? new HashMap<>() : attachments; + this.attachments = attachments == null ? Collections.synchronizedMap(new HashMap<>()) : attachments; } @Override @@ -397,7 +397,7 @@ public class RpcInvocation implements Invocation, Serializable { @Deprecated public void setAttachments(Map attachments) { - this.attachments = attachments == null ? new HashMap<>() : new HashMap<>(attachments); + this.attachments = attachments == null ? Collections.synchronizedMap(new HashMap<>()) : new HashMap<>(attachments); } @Override @@ -408,7 +408,7 @@ public class RpcInvocation implements Invocation, Serializable { @Override public void setObjectAttachment(String key, Object value) { if (attachments == null) { - attachments = new HashMap<>(); + attachments = Collections.synchronizedMap(new HashMap<>()); } attachments.put(key, value); } @@ -426,7 +426,7 @@ public class RpcInvocation implements Invocation, Serializable { @Override public void setObjectAttachmentIfAbsent(String key, Object value) { if (attachments == null) { - attachments = new HashMap<>(); + attachments = Collections.synchronizedMap(new HashMap<>()); } if (!attachments.containsKey(key)) { attachments.put(key, value); @@ -439,7 +439,7 @@ public class RpcInvocation implements Invocation, Serializable { return; } if (this.attachments == null) { - this.attachments = new HashMap<>(); + this.attachments = Collections.synchronizedMap(new HashMap<>()); } this.attachments.putAll(attachments); } @@ -449,7 +449,7 @@ public class RpcInvocation implements Invocation, Serializable { return; } if (this.attachments == null) { - this.attachments = new HashMap<>(); + this.attachments = Collections.synchronizedMap(new HashMap<>()); } this.attachments.putAll(attachments); } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCountCodecTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCountCodecTest.java index b531bb1aa0..6ac3acffe6 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCountCodecTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboCountCodecTest.java @@ -61,7 +61,7 @@ public class DubboCountCodecTest { } MultiMessage multiMessage = (MultiMessage) dubboCountCodec.decode(channel, buffer); - Assertions.assertEquals(multiMessage.size(), 20); + Assertions.assertEquals(multiMessage.size(), 18); int requestCount = 0; int responseCount = 0; Iterator iterator = multiMessage.iterator(); @@ -78,7 +78,7 @@ public class DubboCountCodecTest { } } Assertions.assertEquals(requestCount, 10); - Assertions.assertEquals(responseCount, 10); + Assertions.assertEquals(responseCount, 8); } }