Skip to content

Commit f76ae21

Browse files
zhaoyuguangralf0131
authored andcommitted
Optimize the code: fix CallbackServiceCodec.java exportOrunexportCallbackService method issue. (#3199)
* Optimize the code: fix url to null, NullPointerException, change private variable to camel mode. * Optimize the code: exportOrUnexportCallbackService method camel mode. * Optimize the code: fix method:encodeInvocationArgument private callbackStatus is camel writing. * Optimize the code: fix name issue * Exporter is a noun, we should use a verb here, like Export. * The generics that can be inferred automatically are also deleted.
1 parent de204cf commit f76ae21

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/CallbackServiceCodec.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,49 +77,49 @@ private static byte isCallBack(URL url, String methodName, int argIndex) {
7777
* @throws IOException
7878
*/
7979
@SuppressWarnings({"unchecked", "rawtypes"})
80-
private static String exportOrunexportCallbackService(Channel channel, URL url, Class clazz, Object inst, Boolean export) throws IOException {
80+
private static String exportOrUnexportCallbackService(Channel channel, URL url, Class clazz, Object inst, Boolean export) throws IOException {
8181
int instid = System.identityHashCode(inst);
8282

83-
Map<String, String> params = new HashMap<String, String>(3);
83+
Map<String, String> params = new HashMap<>(3);
8484
// no need to new client again
8585
params.put(Constants.IS_SERVER_KEY, Boolean.FALSE.toString());
8686
// mark it's a callback, for troubleshooting
8787
params.put(Constants.IS_CALLBACK_SERVICE, Boolean.TRUE.toString());
88-
String group = url.getParameter(Constants.GROUP_KEY);
88+
String group = (url == null ? null : url.getParameter(Constants.GROUP_KEY));
8989
if (group != null && group.length() > 0) {
9090
params.put(Constants.GROUP_KEY, group);
9191
}
9292
// add method, for verifying against method, automatic fallback (see dubbo protocol)
9393
params.put(Constants.METHODS_KEY, StringUtils.join(Wrapper.getWrapper(clazz).getDeclaredMethodNames(), ","));
9494

95-
Map<String, String> tmpmap = new HashMap<String, String>(url.getParameters());
96-
tmpmap.putAll(params);
97-
tmpmap.remove(Constants.VERSION_KEY);// doesn't need to distinguish version for callback
98-
tmpmap.put(Constants.INTERFACE_KEY, clazz.getName());
99-
URL exporturl = new URL(DubboProtocol.NAME, channel.getLocalAddress().getAddress().getHostAddress(), channel.getLocalAddress().getPort(), clazz.getName() + "." + instid, tmpmap);
95+
Map<String, String> tmpMap = new HashMap<>(url.getParameters());
96+
tmpMap.putAll(params);
97+
tmpMap.remove(Constants.VERSION_KEY);// doesn't need to distinguish version for callback
98+
tmpMap.put(Constants.INTERFACE_KEY, clazz.getName());
99+
URL exportUrl = new URL(DubboProtocol.NAME, channel.getLocalAddress().getAddress().getHostAddress(), channel.getLocalAddress().getPort(), clazz.getName() + "." + instid, tmpMap);
100100

101101
// no need to generate multiple exporters for different channel in the same JVM, cache key cannot collide.
102102
String cacheKey = getClientSideCallbackServiceCacheKey(instid);
103-
String countkey = getClientSideCountKey(clazz.getName());
103+
String countKey = getClientSideCountKey(clazz.getName());
104104
if (export) {
105105
// one channel can have multiple callback instances, no need to re-export for different instance.
106106
if (!channel.hasAttribute(cacheKey)) {
107107
if (!isInstancesOverLimit(channel, url, clazz.getName(), instid, false)) {
108-
Invoker<?> invoker = proxyFactory.getInvoker(inst, clazz, exporturl);
108+
Invoker<?> invoker = proxyFactory.getInvoker(inst, clazz, exportUrl);
109109
// should destroy resource?
110110
Exporter<?> exporter = protocol.export(invoker);
111111
// this is used for tracing if instid has published service or not.
112112
channel.setAttribute(cacheKey, exporter);
113-
logger.info("export a callback service :" + exporturl + ", on " + channel + ", url is: " + url);
114-
increaseInstanceCount(channel, countkey);
113+
logger.info("Export a callback service :" + exportUrl + ", on " + channel + ", url is: " + url);
114+
increaseInstanceCount(channel, countKey);
115115
}
116116
}
117117
} else {
118118
if (channel.hasAttribute(cacheKey)) {
119119
Exporter<?> exporter = (Exporter<?>) channel.getAttribute(cacheKey);
120120
exporter.unexport();
121121
channel.removeAttribute(cacheKey);
122-
decreaseInstanceCount(channel, countkey);
122+
decreaseInstanceCount(channel, countKey);
123123
}
124124
}
125125
return String.valueOf(instid);
@@ -245,17 +245,17 @@ private static void decreaseInstanceCount(Channel channel, String countkey) {
245245
public static Object encodeInvocationArgument(Channel channel, RpcInvocation inv, int paraIndex) throws IOException {
246246
// get URL directly
247247
URL url = inv.getInvoker() == null ? null : inv.getInvoker().getUrl();
248-
byte callbackstatus = isCallBack(url, inv.getMethodName(), paraIndex);
248+
byte callbackStatus = isCallBack(url, inv.getMethodName(), paraIndex);
249249
Object[] args = inv.getArguments();
250250
Class<?>[] pts = inv.getParameterTypes();
251-
switch (callbackstatus) {
251+
switch (callbackStatus) {
252252
case CallbackServiceCodec.CALLBACK_NONE:
253253
return args[paraIndex];
254254
case CallbackServiceCodec.CALLBACK_CREATE:
255-
inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true));
255+
inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrUnexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], true));
256256
return null;
257257
case CallbackServiceCodec.CALLBACK_DESTROY:
258-
inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrunexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false));
258+
inv.setAttachment(INV_ATT_CALLBACK_KEY + paraIndex, exportOrUnexportCallbackService(channel, url, pts[paraIndex], args[paraIndex], false));
259259
return null;
260260
default:
261261
return args[paraIndex];

0 commit comments

Comments
 (0)