- RpcRouter<T> router = rpcFactory.getRouterFor(type);
- router.registerRouteChangeListener(new RouteChangeForwarder(type));
- RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy());
- rpcRouters.put(type, router);
- return router;
+ synchronized (this) {
+ /**
+ * Potential Router could be instantiated by other thread while we
+ * were waiting for the lock.
+ */
+ potentialRouter = rpcRouters.get(type);
+ if (potentialRouter != null) {
+ return (RpcRouter<T>) potentialRouter;
+ }
+ RpcRouter<T> router = rpcFactory.getRouterFor(type, name);
+ router.registerRouteChangeListener(new RouteChangeForwarder(type));
+ LOG.debug("Registering router {} as global implementation of {} in {}", router, type.getSimpleName(), this);
+ RuntimeCodeHelper.setDelegate(getRpcService(type), router.getInvocationProxy());
+ rpcRouters.put(type, router);
+ notifyListenersRoutedCreated(router);
+ return router;
+ }