/** * Registers the client for native notifications with the specified tags * @param pnsHandle PNS specific identifier * @param tags Tags to use in the registration * @return The created registration * @throws Exception */ public Registration register(String pnsHandle, String... tags) throws Exception { if (isNullOrWhiteSpace(pnsHandle)) { throw new IllegalArgumentException("pnsHandle"); } Registration registration = PnsSpecificRegistrationFactory.getInstance().createNativeRegistration(mNotificationHubPath); registration.setPNSHandle(pnsHandle); registration.setName(Registration.DEFAULT_REGISTRATION_NAME); registration.addTags(tags); return registerInternal(registration); }
@Override public void notify(Map<HostAndPort, ServiceHealth> newValues) { Set<Registration> previousEntries = topologyManager.registrationsForService(this.name); Set<Registration> newEntries = newValues.values().stream() .map(e -> new Registration("consul", this.name, e.getService().getAddress(), e.getService().getPort()) .addTags(e.getService().getTags()) ) .collect(Collectors.toSet()); previousEntries.stream() .filter(e -> !newEntries.contains(e)) .forEach(e -> { this.topologyManager.unregister(e); }); newEntries.stream() .filter(e -> !previousEntries.contains(e)) .forEach(e -> { this.topologyManager.register(e); }); } }
private Set<Registration> registrationsForService(IService service) { Set<Registration> newEntries = new HashSet<>(); // Only expose the service's default port and anything running on the https port service.getPorts() .stream() .filter(servicePort -> servicePort.getPort() == service.getPort() || servicePort.getPort() == DEFAULT_HTTPS_PORT) .forEach(servicePort -> { Registration registration = new Registration(TOPOLOGY_SOURCE_KEY, service.getName(), service.getName(), servicePort.getPort()); if (servicePort.getPort() == DEFAULT_HTTPS_PORT) { registration.addTags(Collections.singletonList("https")); } else if (servicePort.getPort() == service.getPort()) { registration.addTags(Collections.singletonList("http")); } newEntries.add(registration); }); return newEntries; }