RegionsRoutingService implements RoutingService { @Override public SocketAddress getHost(String key, RoutingContext routingContext) throws NoBrokersAvailableException { for (RoutingService service : routingServices) { try { SocketAddress addr = service.getHost(key, routingContext); if (routingContext.hasUnavailableRegions()) { String region = regionResolver.resolveRegion(addr); if (routingContext.isUnavailableRegion(region)) { continue; } } if (!routingContext.isTriedHost(addr)) { return addr; } } catch (NoBrokersAvailableException nbae) { logger.debug("No brokers available in region {} : ", service, nbae); } } throw new NoBrokersAvailableException("No host found for " + key + ", routing context : " + routingContext); } private  RegionsRoutingService(RegionResolver resolver,
                                  RoutingService[] routingServices); @Deprecated static RegionsRoutingService of(RegionResolver regionResolver,
                                         RoutingService...services); static Builder newBuilder(); @Override Set<SocketAddress> getHosts(); @Override void startService(); @Override void stopService(); @Override RoutingService registerListener(RoutingListener listener); @Override RoutingService unregisterListener(RoutingListener listener); @Override SocketAddress getHost(String key, RoutingContext routingContext); @Override void removeHost(SocketAddress address, Throwable reason);  }
@Test(timeout = 60000) public void testGetHost() throws Exception { int numRoutingServices = 3; RoutingService.Builder[] routingServiceBuilders = new RoutingService.Builder[numRoutingServices]; Map<SocketAddress, String> regionMap = new HashMap<SocketAddress, String>(); for (int i = 0; i < numRoutingServices; i++) { String finagleNameStr = "inet!127.0.0.1:" + (3181 + i); routingServiceBuilders[i] = RoutingUtils.buildRoutingService(finagleNameStr); SocketAddress address = new InetSocketAddress("127.0.0.1", 3181 + i); regionMap.put(address, "region-" + i); } RegionsRoutingService regionsRoutingService = RegionsRoutingService.newBuilder() .resolver(new DefaultRegionResolver(regionMap)) .routingServiceBuilders(routingServiceBuilders) .build(); regionsRoutingService.startService(); RoutingService.RoutingContext routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver()) .addTriedHost(new InetSocketAddress("127.0.0.1", 3183), StatusCode.WRITE_EXCEPTION); assertEquals(new InetSocketAddress("127.0.0.1", 3181), regionsRoutingService.getHost("any", routingContext)); routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver()) .addTriedHost(new InetSocketAddress("127.0.0.1", 3181), StatusCode.WRITE_EXCEPTION); assertEquals(new InetSocketAddress("127.0.0.1", 3182), regionsRoutingService.getHost("any", routingContext)); routingContext.addTriedHost(new InetSocketAddress("127.0.0.1", 3182), StatusCode.WRITE_EXCEPTION); assertEquals(new InetSocketAddress("127.0.0.1", 3183), regionsRoutingService.getHost("any", routingContext)); routingContext.addTriedHost(new InetSocketAddress("127.0.0.1", 3183), StatusCode.WRITE_EXCEPTION); try { regionsRoutingService.getHost("any", routingContext); fail("Should fail to get host since all regions are tried."); } catch (NoBrokersAvailableException nbae) { } }